Pandas Series dt.date | Extract Date From DateTime Objects
Last Updated :
06 Feb, 2024
The dt.date attribute extracts the date part of the DateTime objects in a Pandas Series.
It returns the NumPy array of Python datetime.date objects, mainly the date part of timestamps without information about the time and timezone.
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.date
print (result)
|
Output:
Syntax
Syntax: Series.dt.dateÂ
Parameter : NoneÂ
Returns :NumPy array with datetime.date objects
How to Extract Date from a DateTime object in Pandas Series
To extract the date from a DateTime object in Pandas Series we use the dt.date attribute of the Pandas library in Python.
Let us understand it better with an example:
Example:
Python3
import pandas as pd
sr = pd.Series(pd.date_range( '2012-12-12 12:12' ,
periods = 5 , freq = 'H' ))
idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ]
sr.index = idx
print (sr)
|
Output :
Now we will use the dt.date attribute to return the date property of the underlying data of the given Series object.
Python3
result = sr.dt.date
print (result)
|
Output :
As we can see in the output, the dt.date attribute has successfully accessed and returned the date property of the underlying data in the given series object.
Share your thoughts in the comments
Please Login to comment...