How to find day of week in pandas

Overview

To find the day of the week from the pandas' Timestamp object, we use the attribute, .dayofweek. The Timestamp object is similar to the Python's datetime object. Let's see how to find the day of the week.

Syntax

Timestamp.dayofweek
Syntax for the .dayofweek attribute in Pandas

Parameters

This attribute does not require any parameter value. It just requires the Timestamp object to find day of the week for it.

Return value

It returns an integer from 0 to 6 representing the day of the week. 0 represents Monday, 1 represents Tuesday, and so on.

Example

# A code to illustrate the .day_of_week attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a Timestamp object
a = pd.Timestamp(2020, 4, 19) # (Year, Month, Day)
# getting the day of the week
print(a.dayofweek)

Explanation

  • Line 3: We import the pandas library.
  • Line 6: We create a Timestamp object, a. We provide the year, month, and day for Timestamp. For example, 2020:04:19.
  • Line 9: We obtain the day of the week for a using the .dayofweek attribute.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources