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 libraryimport pandas as pd# creating a Timestamp objecta = pd.Timestamp(2020, 4, 19) # (Year, Month, Day)# getting the day of the weekprint(a.dayofweek)
Explanation
- Line 3: We import the
pandaslibrary. - Line 6: We create a
Timestampobject,a. We provide the year, month, and day forTimestamp. For example,2020:04:19. - Line 9: We obtain the day of the week for
ausing the.dayofweekattribute.