Pandas Series dt.dayofweek | Get Day of Week from DateTime Series in Pandas
Last Updated :
07 Feb, 2024
Pandas dt.dayofweek attribute returns the day of the week from the given DateTime Series Object. It is assumed the week starts on Monday, which is denoted by 0, and ends on Sunday which is denoted by 6.
Example:
Python3
import pandas as pd
sr = pd.Series([ '2012-10-21 09:30' , '2019-7-18 12:30' , '2008-02-2 10:30' ,
'2010-4-22 09:25' , '2019-11-8 02:22' ])
idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ]
sr.index = idx
sr = pd.to_datetime(sr)
result = sr.dt.dayofweek
print (result)
|
Output:
Syntax
Syntax: Series.dt.dayofweekÂ
Parameter: NoneÂ
Returns: Series with integers indicating day of week
How to Extract Day of Week From Pandas Series
To extract the day of the week from the Pandas Series we use the dt.dayofweek attribute of the Pandas library in Python.
Let us understand it better with an example:
Example:
Use the Series.dt.dayofweek attribute to return the day of the week for the given DateTime in the underlying data of the given Series object.
Python3
import pandas as pd
sr = pd.Series(pd.date_range( '2012-12-12 12:12' , periods = 5 , freq = 'M' ))
idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ]
sr.index = idx
print (sr)
|
Output :
Now we will use the dt.dayofweek attribute to return the day of the week for the given DateTime in the underlying data of the given Series object.
Python3
result = sr.dt.dayofweek
print (result)
|
Output :
As we can see in the output, the dt.dayofweek attribute has successfully accessed and returned the day of week in the underlying data of the given series object.
Share your thoughts in the comments
Please Login to comment...