check if day is weekday or weekend

Pandas – Check If a Day is Weekday or Weekend

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.

check if day is weekday or weekend

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.

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

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"])

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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.

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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top