Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether the day is a weekday or not using Pandas in Python?
Python pandas library provides several functions to check whether a day is a weekday or weekend, including weekday(), day_name(), and isoweekday(). These functions work with pandas datetime objects to identify the day of the week.
Using the weekday() Function
The weekday() function returns the day of the week as an integer, where 0 represents Monday, 1 represents Tuesday, and so on up to 6 for Sunday ?
import pandas as pd
date = pd.to_datetime('2023-03-25')
day_number = date.weekday()
print(f"Day number: {day_number}")
if day_number < 5:
print(f"{date.date()} is a Weekday")
else:
print(f"{date.date()} is a Weekend")
Day number: 5 2023-03-25 is a Weekend
Example with Function
Here's a reusable function to check any date ?
import pandas as pd
def check_weekday(date_string):
date = pd.to_datetime(date_string)
day_number = date.weekday()
if day_number < 5:
return f"{date.date()} is a Weekday (Day {day_number})"
else:
return f"{date.date()} is a Weekend (Day {day_number})"
# Test with different dates
print(check_weekday('2023-03-23')) # Thursday
print(check_weekday('2023-03-25')) # Saturday
2023-03-23 is a Weekday (Day 3) 2023-03-25 is a Weekend (Day 5)
Using the day_name() Function
The day_name() function returns the actual name of the day as a string, making it more readable ?
import pandas as pd
date = pd.to_datetime('2023-03-25')
day_name = date.day_name()
print(f"Day name: {day_name}")
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
if day_name in weekdays:
print(f"{date.date()} is a Weekday")
else:
print(f"{date.date()} is a Weekend")
Day name: Saturday 2023-03-25 is a Weekend
Example with Multiple Dates
Checking multiple dates using a DataFrame ?
import pandas as pd
dates = ['2023-03-20', '2023-03-21', '2023-03-22', '2023-03-23', '2023-03-24']
df = pd.DataFrame({'date': pd.to_datetime(dates)})
df['day_name'] = df['date'].dt.day_name()
df['is_weekday'] = df['day_name'].isin(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])
print(df)
date day_name is_weekday
0 2023-03-20 Monday True
1 2023-03-21 Tuesday True
2 2023-03-22 Wednesday True
3 2023-03-23 Thursday True
4 2023-03-24 Friday True
Using isoweekday() Function
The isoweekday() function follows the ISO standard where Monday is 1, Tuesday is 2, and Sunday is 7 ?
import pandas as pd
date = pd.to_datetime('2023-03-25')
iso_day = date.isoweekday()
print(f"ISO day number: {iso_day}")
if iso_day < 6: # Monday(1) to Friday(5)
print(f"{date.date()} is a Weekday")
else: # Saturday(6) or Sunday(7)
print(f"{date.date()} is a Weekend")
ISO day number: 6 2023-03-25 is a Weekend
Example with Monday Date
import pandas as pd
date = pd.to_datetime('2022-11-21') # Monday
iso_day = date.isoweekday()
print(f"ISO day number: {iso_day}")
if iso_day < 6:
print(f"{date.date()} is a Weekday")
else:
print(f"{date.date()} is a Weekend")
ISO day number: 1 2022-11-21 is a Weekday
Comparison of Methods
| Method | Monday Value | Sunday Value | Return Type | Weekday Condition |
|---|---|---|---|---|
weekday() |
0 | 6 | Integer | < 5 |
day_name() |
'Monday' | 'Sunday' | String | in weekday_names |
isoweekday() |
1 | 7 | Integer | < 6 |
Conclusion
Use weekday() for simple integer-based checks, day_name() for readable string comparisons, and isoweekday() when following ISO standards. All three methods effectively identify weekdays vs weekends in pandas datetime objects.
