Pandas Series dt.normalize() | Normalize Time in Pandas Series
Last Updated :
08 Feb, 2024
The dt.normalize() method converts times to midnight. The time component of the date-time is converted to midnight i.e. 00:00:00.
This is useful in cases when the time does not matter. Length is unaltered. The time zones are unaffected.
Example:
Python3
import pandas as pd
sr = pd.Series(pd.date_range( '2012-12-31 09:45' , periods = 5 , freq = 'M' ,
tz = 'Europe / Berlin' ))
idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ]
sr.index = idx
result = sr.dt.normalize()
print (result)
|
Output :
Syntax
Synatx: Series.dt.normalize()Â
Parameter: NoneÂ
Returns: DatetimeArray, DatetimeIndex or Series
How to Normalize Time in Pandas Series?
To normalize time component of the DateTime object we use the dt.normalize() method of the Pandas library in Python.
Let us understand it better with an example:
Example:
Use the Series.dt.normalize() function to convert the times in the given series object to midnight.
Python3
import pandas as pd
sr = pd.Series(pd.date_range( '2019-1-1 12:30' , periods = 5 , freq = 'H' ,
tz = 'Asia / Calcutta' ))
idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ]
sr.index = idx
print (sr)
|
Output :
Now we will use the dt.normalize() function to convert the times to midnight.
Python3
result = sr.dt.normalize()
print (result)
|
Output :
As we can see in the output, the Series.dt.normalize() function has successfully converted the times in the given series object to midnight.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...