Open In App

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

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 :

dt.normalize() 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




# 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 :

datetime series created

Now we will use the dt.normalize() function to convert the times to midnight.

Python3




# convert to midnight
result = sr.dt.normalize()
  
# print the result
print(result)


Output :

normalized time returned

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
Previous
Next
Share your thoughts in the comments

Similar Reads