Knowing whether a date is a weekday or weekend can help us better understand patterns associated with temporal data. In this tutorial, we will look at how to check whether a date in pandas is a weekday or a weekend with the help of examples.
How to check if day is weekday or weekend in pandas?
If you’re working with a pandas datetime type date then you can simply call the .weekday()
function or if you’re working with a pandas series you can use the .dt.weekday
attribute to get the day of the week as a number with Monday=0, and Sunday=6. From here, you can check whether the weekday is greater than five or not, if yes, then the day is a weekend, if not, then it’s a weekday. The following is the syntax:
import pandas as pd # create a sample date sample_date = pd.Timestamp(2021, 12, 1) # get the weekday print(sample_date.weekday()) # check if it's a weekend or not print(sample_date.weekday() >= 5)
Output:
2 False
We get False
because the date “2021-12-01” is not a weekend as it’s a Wednesday.
Check if each date in a Pandas Column is a Weekend day or not
Let’s now look at an example where we determine for each date in a column whether it’s a weekend or not. Let’s look at this step wise. First, we will create a sample dataframe with a column containing date values.
# create a dataframe df = pd.DataFrame({ "Date": ["2021-12-01", "2021-12-02", "2021-12-03", "2021-12-04", "2021-12-05", "2021-12-06", "2021-12-07"] }) # display the dataframe print(df)
Output:
Date 0 2021-12-01 1 2021-12-02 2 2021-12-03 3 2021-12-04 4 2021-12-05 5 2021-12-06 6 2021-12-07
STEP 1 – Convert column to datetime
The first thing we have to do is to convert the “Date” column to pandas datetime.
# convert to datetime df["Date"] = pd.to_datetime(df["Date"])
STEP 2 – Determine the day of the week
We find the day of the week for each day in the column using the .dt.weekday
column property. This gives a number to each day starting with 0 for Monday and ending with 6 for Sunday.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# get the day of week df["DayOfWeek"] = df["Date"].dt.weekday # display the dataframe print(df)
Output:
Date DayOfWeek 0 2021-12-01 2 1 2021-12-02 3 2 2021-12-03 4 3 2021-12-04 5 4 2021-12-05 6 5 2021-12-06 0 6 2021-12-07 1
You can see that we now have the day of the week as integers in a separate column.
STEP 3 – Check if the day is weekend or not
We can use a simple boolean condition to check whether the day is weekend or not. Let’s create a new column “is_weekend” which stores these values.
# check if the date is weekend or not df["IsWeekend"] = df["DayOfWeek"] >= 5 # display the dataframe print(df)
Output:
Date DayOfWeek IsWeekend 0 2021-12-01 2 False 1 2021-12-02 3 False 2 2021-12-03 4 False 3 2021-12-04 5 True 4 2021-12-05 6 True 5 2021-12-06 0 False 6 2021-12-07 1 False
Now we have a column with information on whether a given date is a weekend or not.
Summary – Final Code
Instead of creating a new column for storing the weekday we can combine the two steps in a single line.
# create a dataframe df = pd.DataFrame({ "Date": ["2021-12-01", "2021-12-02", "2021-12-03", "2021-12-04", "2021-12-05", "2021-12-06", "2021-12-07"] }) # convert to datetime df["Date"] = pd.to_datetime(df["Date"]) # check if the day is weekday or weekend df["IsWeekend"] = df["Date"].dt.weekday >= 5 # display the dataframe print(df)
Output:
Date IsWeekend 0 2021-12-01 False 1 2021-12-02 False 2 2021-12-03 False 3 2021-12-04 True 4 2021-12-05 True 5 2021-12-06 False 6 2021-12-07 False
We get the same results as above.
For more on the pandas series .dt.weekday
property refer to its documentation.
Information on whether a day is a weekday or weekend can be very useful in exploratory analysis. For example, you find that certain queries on Google have a high search volume on weekdays as compared to weekends. Or, you see that sales for a particular product are comparatively higher on the weekends. Not only this, such information can also be a good feature for a predictive model.
You might also be interested in –
- Pandas – Check if Year is Leap Year or Not
- Get Quarter from Date in Pandas
- Remove Time from Date in Pandas
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.