Last updated: Apr 12, 2024
Reading time·5 min
day_name()
method insteadTo check if a Date is during the weekend or is a weekday in Pandas:
weekday()
method to get the day of the week, where Monday is 0
and Sunday is 6
.4
, then the date is during the
weekend.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')
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.
The same approach can be used to check if each date in a DataFrame
is during
the weekend or is a weekday.
pandas.to-datetime()
method to convert the strings in the
DataFrame
to datetime
objects.dt.weekday
attribute on each datetime
object and check if each
value is greater than 4
.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)
Running the code sample produces the following output.
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
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
.
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.
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)
Running the code sample produces the following output.
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
.
day_name()
method insteadYou can also use the
dt.day_name
method to check if each date in a DataFrame
is during the weekend or is a
weekday.
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)
Running the code sample produces the following output.
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
We used the pandas.to_datetime()
method to convert the strings in the date
column to datetime
objects.
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.
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)
Running the code sample produces the following output.
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
The day_of_week
column stores the name of the day of the week of each date.
You can learn more about the related topics by checking out the following tutorials: