Open In App

Pandas Series dt.day | Extract Day Part from DateTime Series

Pandas dt.day attribute returns a NumPy array containing the day value of the DateTime in the underlying data of the given series object.

Example




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']
  
# set the index
sr.index = idx
sr = pd.to_datetime(sr)
print(sr)

Output



Syntax

Syntax: Series.dt.day 



Parameter : None 

Returns: NumPy array containing day values

How to Extract Day From DateTime Series

To extract the day value from the DateTime Series we use the dt.day attribute of the Pandas library in Python.

Let us understand it better with an example:

Example:

Use the Series.dt.day attribute to return the day of the DateTime in the underlying data of the given Series object.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(pd.date_range('2012-12-12 12:12'
                       periods = 5, freq = 'H'))
  
# Creating the index
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)

Output :

Now we will use the Series.dt.day attribute to return the day of the datetime in the underlying data of the given Series object.




# return the day
result = sr.dt.day
  
# print the result
print(result)

Output :

As we can see in the output, the Series.dt.day attribute has successfully accessed and returned the day of the DateTime in the underlying data of the given series object.


Article Tags :