Pandas: Check if a Date is during the Weekend or Weekday

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
5 min

banner

# Table of Contents

  1. Pandas: Check if a Date is during the Weekend or Weekday
  2. Check if each date in a DataFrame is during the weekend or is a weekday
  3. Using the day_name() method instead

# Pandas: Check if a Date is during the Weekend or Weekday

To check if a Date is during the weekend or is a weekday in Pandas:

  1. Call the weekday() method to get the day of the week, where Monday is 0 and Sunday is 6.
  2. If the method returns a value greater than 4, then the date is during the weekend.
  3. Otherwise, the date is a weekday.
main.py
import pandas as pd # Saturday, August 5th, 2023 a_date = pd.Timestamp('2023-08-05') if a_date.weekday() > 4: # 👇️ this runs print('The date is during the weekend') else: print('The date is a weekday')

check if date is weekend or weekday

The code for this article is available on GitHub

The pandas.DatetimeIndex() method returns the zero-based day of the week with Monday=0 and Sunday=6.

If the method returns an integer greater than 4, then the date is during the weekend.

Otherwise, the date is a weekday.

# Check if each date in a DataFrame is during the weekend or is a weekday

The same approach can be used to check if each date in a DataFrame is during the weekend or is a weekday.

  1. Use the pandas.to-datetime() method to convert the strings in the DataFrame to datetime objects.
  2. Access the dt.weekday attribute on each datetime object and check if each value is greater than 4.
main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12', '2023-08-05', '2023-09-21' ] }) print(df) print('-' * 50) df['date'] = pd.to_datetime(df['date']) df['weekend'] = df['date'].dt.weekday > 4 print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name date 0 Alice 2023-01-12 1 Bobby 2023-08-05 2 Carl 2023-09-21 -------------------------------------------------- name date weekend 0 Alice 2023-01-12 False 1 Bobby 2023-08-05 True 2 Carl 2023-09-21 False

check if each date in dataframe is during weekend in pandas

We used the pandas.to_datetime method to convert the strings in the date column to datetime objects.

The next step is to access the dt.weekday attribute on each row and check if the day of the week is greater than 4.

main.py
df['weekend'] = df['date'].dt.weekday > 4 # name date weekend # 0 Alice 2023-01-12 False # 1 Bobby 2023-08-05 True # 2 Carl 2023-09-21 False print(df)

The weekend column stores True for dates that are during the weekend and False otherwise.

You can also create a column that stores the day of the week of each date.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12', '2023-08-05', '2023-09-21' ] }) print(df) print('-' * 50) df['date'] = pd.to_datetime(df['date']) df['day_of_week'] = df['date'].dt.weekday df['weekend'] = df['date'].dt.weekday > 4 print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name date 0 Alice 2023-01-12 1 Bobby 2023-08-05 2 Carl 2023-09-21 -------------------------------------------------- name date day_of_week weekend 0 Alice 2023-01-12 3 False 1 Bobby 2023-08-05 5 True 2 Carl 2023-09-21 3 False

Note that the day_of_week column stores zero-based values, so Monday is 0 and Saturday is 6.

# Using the day_name() method instead

You can also use the dt.day_name method to check if each date in a DataFrame is during the weekend or is a weekday.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12', '2023-08-05', '2023-09-21' ] }) print(df) print('-' * 50) df['date'] = pd.to_datetime(df['date']) df['weekend'] = df['date'].dt.day_name().isin(['Saturday', 'Sunday']) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name date 0 Alice 2023-01-12 1 Bobby 2023-08-05 2 Carl 2023-09-21 -------------------------------------------------- name date weekend 0 Alice 2023-01-12 False 1 Bobby 2023-08-05 True 2 Carl 2023-09-21 False

check if date is during the weekend using day name

We used the pandas.to_datetime() method to convert the strings in the date column to datetime objects.

main.py
df['weekend'] = df['date'].dt.day_name().isin( ['Saturday', 'Sunday'] )

This enables us to use the dt.day_name method to get the day names of the dates.

If the returned value is one of "Saturday" or "Sunday", then the date is during the weekend.

You can also use the day_name method if you need to add a column that stores the name of the day of the week.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12', '2023-08-05', '2023-09-21' ] }) print(df) print('-' * 50) df['date'] = pd.to_datetime(df['date']) df['day_of_week'] = df['date'].dt.day_name() df['weekend'] = df['date'].dt.day_name().isin( ['Saturday', 'Sunday'] ) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name date 0 Alice 2023-01-12 1 Bobby 2023-08-05 2 Carl 2023-09-21 -------------------------------------------------- name date day_of_week weekend 0 Alice 2023-01-12 Thursday False 1 Bobby 2023-08-05 Saturday True 2 Carl 2023-09-21 Thursday False

add day name column to dataframe

The day_of_week column stores the name of the day of the week of each date.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.