Python Pandas: How to Check if Date is during Weekend or Weekday
Determining whether a given date falls on a weekend (Saturday or Sunday) or a weekday (Monday through Friday) is a common requirement in data analysis, especially when dealing with business days, scheduling, or time series data. Pandas provides convenient attributes and methods through its dt
accessor for datetime
Series to easily make this distinction.
This guide explains how to check if dates in a Pandas DataFrame column (or a single Timestamp) are weekends or weekdays using Series.dt.weekday
and Series.dt.day_name()
.
The Goal: Identifying Weekends vs. Weekdays
Given a date or a column of dates, we want to determine for each date if it's a Saturday/Sunday (weekend) or a Monday/Tuesday/Wednesday/Thursday/Friday (weekday).
Prerequisite: Ensuring Date Column is Datetime Type
For Pandas datetime-specific operations, the column containing dates must be of datetime64[ns]
type. If your dates are currently strings, convert them first.
import pandas as pd
df_sample = pd.DataFrame({'DateStr': ['2023-10-27', '2023-10-28', '2023-10-29']}) # Fri, Sat, Sun
print(f"Original Dtype: {df_sample['DateStr'].dtype}\n")
# ✅ Convert to datetime
df_sample['DateDT'] = pd.to_datetime(df_sample['DateStr'])
print(f"Converted Dtype: {df_sample['DateDT'].dtype}") # datetime64[ns]
Output:
Original Dtype: object
Converted Dtype: datetime64[ns]
Example DataFrame and Single Timestamp
import pandas as pd
# DataFrame Example
data = {
'Event': ['Meeting', 'Workshop', 'Holiday', 'Conference', 'Review'],
'EventDate': [
'2023-10-27', # Friday
'2023-10-28', # Saturday
'2023-10-29', # Sunday
'2023-10-30', # Monday
'2023-11-03' # Friday
]
}
df = pd.DataFrame(data)
df['EventDate'] = pd.to_datetime(df['EventDate']) # Ensure datetime type
print("Original DataFrame:")
print(df)
print()
print("Data types:")
print(df.dtypes)
# Single Timestamp Example
single_date_weekend = pd.Timestamp('2023-10-28') # Saturday
single_date_weekday = pd.Timestamp('2023-10-27') # Friday
Output:
Original DataFrame:
Event EventDate
0 Meeting 2023-10-27
1 Workshop 2023-10-28
2 Holiday 2023-10-29
3 Conference 2023-10-30
4 Review 2023-11-03
Data types:
Event object
EventDate datetime64[ns]
dtype: object
Method 1: Using Series.dt.weekday
(Numeric Day of Week)
The Series.dt.weekday
attribute (or Timestamp.weekday()
method for single objects) returns the day of the week as an integer, where:
- Monday = 0
- Tuesday = 1
- Wednesday = 2
- Thursday = 3
- Friday = 4
- Saturday = 5
- Sunday = 6
Therefore, a date is a weekend if its .dt.weekday
is greater than 4 (i.e., 5 or 6).
Checking a DataFrame Column
import pandas as pd
df_example = pd.DataFrame({
'EventDate': pd.to_datetime(['2023-10-27', '2023-10-28', '2023-10-29', '2023-10-30'])
})
# ✅ Create a boolean column 'IsWeekend'
# df_example['EventDate'].dt.weekday returns a Series of integers (0-6)
df_example['IsWeekend_dt_weekday'] = df_example['EventDate'].dt.weekday > 4
print("DataFrame with 'IsWeekend' column (using .dt.weekday):")
print(df_example)
Output:
DataFrame with 'IsWeekend' column (using .dt.weekday):
EventDate IsWeekend_dt_weekday
0 2023-10-27 False
1 2023-10-28 True
2 2023-10-29 True
3 2023-10-30 False
Checking a Single pd.Timestamp
Object
For a single Timestamp
object, use the .weekday()
method (note the parentheses).
import pandas as pd
date_saturday = pd.Timestamp('2023-10-28') # Saturday
date_friday = pd.Timestamp('2023-10-27') # Friday
is_saturday_weekend = date_saturday.weekday() > 4
print(f"Is {date_saturday.date()} a weekend? {is_saturday_weekend}")
is_friday_weekend = date_friday.weekday() > 4
print(f"Is {date_friday.date()} a weekend? {is_friday_weekend}")
Output:
Is 2023-10-28 a weekend? True
Is 2023-10-27 a weekend? False
Method 2: Using Series.dt.day_name()
and isin()
The Series.dt.day_name()
method returns the name of the day of the week (e.g., "Monday", "Saturday"). You can then check if this name is in a list of weekend day names.
Checking a DataFrame Column
This method is often more readable as it deals with explicit day names.
import pandas as pd
df_example = pd.DataFrame({
'EventDate': pd.to_datetime(['2023-10-27', '2023-10-28', '2023-10-29', '2023-10-30'])
})
# Get the names of the days
day_names_series = df_example['EventDate'].dt.day_name()
print("Day names:")
print(day_names_series)
print()
# ✅ Check if the day name is 'Saturday' or 'Sunday'
weekend_days = ['Saturday', 'Sunday']
df_example['IsWeekend_day_name'] = day_names_series.isin(weekend_days)
print("DataFrame with 'IsWeekend' column (using .dt.day_name()):")
print(df_example)
Output:
Day names:
0 Friday
1 Saturday
2 Sunday
3 Monday
Name: EventDate, dtype: object
DataFrame with 'IsWeekend' column (using .dt.day_name()):
EventDate IsWeekend_day_name
0 2023-10-27 False
1 2023-10-28 True
2 2023-10-29 True
3 2023-10-30 False
Adding a Column for Day of Week Name or Number (Optional)
It can be useful to add a column for the day of the week number or name for other analyses or easier filtering later.
import pandas as pd
df = pd.DataFrame({
'EventDate': pd.to_datetime(['2023-10-27', '2023-10-28', '2023-10-29', '2023-10-30'])
})
# Add day of week number (0=Mon, 6=Sun)
df['DayOfWeek_Num'] = df['EventDate'].dt.weekday
# Add day of week name
df['DayOfWeek_Name'] = df['EventDate'].dt.day_name()
# Add IsWeekend flag
df['IsWeekend'] = df['DayOfWeek_Num'] > 4 # Or use the .dt.day_name().isin(...) method
print("DataFrame with additional day of week columns:")
print(df)
Output:
DataFrame with additional day of week columns:
EventDate DayOfWeek_Num DayOfWeek_Name IsWeekend
0 2023-10-27 4 Friday False
1 2023-10-28 5 Saturday True
2 2023-10-29 6 Sunday True
3 2023-10-30 0 Monday False
Conclusion
Pandas makes it easy to determine if a date falls on a weekend or a weekday:
- Ensure your date column is of
datetime64[ns]
type (usepd.to_datetime()
if necessary). - Using
Series.dt.weekday
:your_datetime_series.dt.weekday > 4
returns a boolean Series (True
for weekends).- For a single
Timestamp
object:timestamp_obj.weekday() > 4
.
- Using
Series.dt.day_name()
:your_datetime_series.dt.day_name().isin(['Saturday', 'Sunday'])
returns a boolean Series.
The .dt.weekday
method is generally more concise for direct boolean checking, while .dt.day_name()
can be more readable if you prefer working with day names. Both are effective for categorizing your dates.