Skip to Content

Pandas – Get Day of Week from Date

In this tutorial, we’ll try to solve 2 problems:

  1. Get the day of the week (the weekday) from a specific cell of a pandas dataframe.
  2. Get the day of the week (the weekday) from all the cells in the column of a pandas dataframe

Datatype conversion:

Before going through the below solutions for the problem, we first need to make sure that the datatype of the date_column for which we need the weekday has to be pandas datetime. In case you have strings in the column then try to use pandas.to_datetime method to convert the datatype of the column to pandas datetime.

code:

df['date_column'] = pd.to_datetime(df['date_column'])

The above line of code changes the datatype of the ‘date_column’ column to pandas datetime.

Problem – 1: Extract the weekday from a specific cell of dataframe

Here, we’re trying to extract the weekday from a specific cell of a pandas dataframe object. To achieve this we need to follow the below steps.

  1. Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in Datatype conversion section
  2. Once you make sure the datatype of the column is pandas datetime, then select the specific cell from which you need to extract the weekday.
  3. Then use the .weekday() method over the selected cell which gives the weekday of the date specified by the cell.
  4. This will result in an integer between 0 – 6 where 0 represents Monday, 1 represents Tuesday, and so on until 6 which represents Sunday.

Basic Syntax:

df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column'][0].weekday()

The above code, first converts the data type of the column to pandas datetime and then extracts the the day of the week of the specified cell using the .weekday() method which is an inbuilt method for the timestamp datatype.

Examples

Now, let us have a look at some of the examples to demonstrate the above solution.

Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all below examples.

import pandas as pd
dates = {'date_column':['03 - 10 - 2022', '2017', '07 - 09 - 2019']}
df = pd.DataFrame(dates)
df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True)
df

Output:

pandas dataframe with a datetime column

Example – 1

code:

df['date_column'][0].weekday()

Output:

0

Example – 2

code:

df['date_column'][2].weekday()

Output:

3

Problem – 2: Extract the weekday from all the cells in the column of a dataframe

get day of the week from date of a pandas dataframe

Here, we’re trying to extract the weekday from all the cells in the column of a dataframe object. To achieve this we need to follow the below steps.

  1. Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in Datatype conversion section
  2. Once you make sure the datatype of the column is pandas datetime, then select the specific column cell from which you need to extract the weekday.
  3. Then use the .dt.weekday attribute over the selected column which gives the weekdays of the all dates in the column.
  4. This will return a series of integers, each integer value between 0 – 6 where 0 represents Monday, 1 represents Tuesday, and so on until 6 represents Sunday.

Basic Syntax:

df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column'].dt.weekday

The above code first converts the data type of the column to pandas datetime and then extracts the weekday of all the cells of the column using the .dt.weekday attribute.

Examples

Now, let us have a look at some of the examples to demonstrate the above solution.

Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all the below examples.

import pandas as pd
dates = {'date_column':['14 - 05 - 2017', '2017', '07 - 09 - 2019']}
df = pd.DataFrame(dates)
df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True)
df

Output:

pandas dataframe with a datetime column

Example – 1

code:

df['date_column'].dt.weekday

Output:

0    6
1    6
2    3
Name: date_column, dtype: int64

Summary

In this we looked at 2 different problems and tried to solve each one separately

  1. Extract the day of the week from a specific cell of a pandas dataframe.

    Solution: Using .weekday() method

    Result: interger ranges between 0-6 ( Monday – Sunday )

  2. Extract the day of the week from all the cells of a pandas dataframe column.

    Solution: Using .dt.weekday attribute

    Result: Series of interger values specifying weekday of each date in the cells

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.