Open In App

Pandas Series dt.normalize() | Normalize Time in Pandas Series

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:




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.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(pd.date_range('2019-1-1 12:30', periods = 5, freq = 'H',
                             tz = 'Asia / Calcutta'))
  
# 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 dt.normalize() function to convert the times to midnight.




# convert to midnight
result = sr.dt.normalize()
  
# print the result
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.


Article Tags :