Python | Pandas DatetimeIndex.round ()

Python Methods and Functions

DatetimeIndex.round() Pandas DatetimeIndex.round() localizes tz-naive DatetimeIndex for tz- of an aware DatetimeIndex. This method uses a naive DatetimeIndex object for the time zone (tz) and reports that time zone. It does not move the time to a different time zone. Time zone localization helps to switch from time zone to invisible time zone objects.

Syntax: DatetimeIndex.round (freq, * args, ** kwargs)

Parameters:
freq: The frequency level to round the index to. Must be a fixed frequency like 'S' (second) not 'ME' (month end)

Return: Index of the same type for a DatetimeIndex or TimedeltaIndex, or a Series with the same index for a Series.

Example # 1: Use DatetimeIndex.round () to round the DatetimeIndex object data to a specified frequency.

# import pandas as pd

import pandas as pd

 
# Create DatetimeIndex
# Here & # 39; S & # 39; represents the second frequency

didx = pd. DatetimeIndex (start = '2000-01-15 08:00'

freq = 'S' , periods = 4 )

 
# Print DatetimeIndex

print (didx)

Output:

Now we want to convert the second based frequency of the DatetimeIndex object to a minute based frequency

# convert to the passed frequency
# & # 39; T & # 39; represents frequency based on minutes

didx. round (freq = ' T' )

Output:

As we can see in the output, the function has rounded the values ​​to the desired frequency.

Example # 2: Use DatetimeIndex.round () to round the DatetimeIndex object data to the specified frequency.

# import pandas as pd

import pandas as pd

 
# Create DatetimeIndex
# Here & # 39; T & # 39; represents frequency

didx = pd.DatetimeIndex (start = '2000-01-15 08:00'

freq = 'T' , periods = 4 )

 
# Print DatetimeIndex

print (didx)

Output:

Now we want to convert the minute-based frequency of the DatetimeIndex object to the clock-based frequency

# convert to passed frequency
# Convert minute frequency to hourly frequency

didx. round (freq = 'H' )

Output:

As we can see in the output, the function has rounded values ​​to the desired frequency.