Python Pandas - How to Round the DateTimeIndex with hourly frequency

To round the DateTimeIndex with hourly frequency, use the DateTimeIndex.round() method. For hourly frequency, use the freq parameter with value 'H'.

Creating a DateTimeIndex

At first, import the required libraries and create a DateTimeIndex with period 5 and frequency as 35 minutes ?

import pandas as pd

# Create DatetimeIndex with period 5 and frequency as 35 minutes
datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='35T')

# Display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:35:00+09:30',
               '2021-09-29 08:10:00+09:30', '2021-09-29 08:45:00+09:30',
               '2021-09-29 09:20:00+09:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='35T')

Rounding with Hourly Frequency

The round() method rounds timestamps to the nearest specified frequency. When using 'H' for hourly frequency, timestamps are rounded to the nearest hour ?

import pandas as pd

# Create DatetimeIndex with 35-minute intervals
datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='35T')

print("Original DateTimeIndex...\n", datetimeindex)

# Round operation with hourly frequency
rounded_index = datetimeindex.round(freq='H')
print("\nRounded to nearest hour...\n", rounded_index)
Original DateTimeIndex...
DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:35:00+09:30',
               '2021-09-29 08:10:00+09:30', '2021-09-29 08:45:00+09:30',
               '2021-09-29 09:20:00+09:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='35T')

Rounded to nearest hour...
DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 08:00:00+09:30',
               '2021-09-29 08:00:00+09:30', '2021-09-29 09:00:00+09:30',
               '2021-09-29 09:00:00+09:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq=None)

How It Works

The rounding logic follows these rules:

  • 07:00 remains 07:00 (exact hour)
  • 07:35 rounds to 08:00 (closer to 8 than 7)
  • 08:10 rounds to 08:00 (closer to 8 than 9)
  • 08:45 rounds to 09:00 (closer to 9 than 8)
  • 09:20 rounds to 09:00 (closer to 9 than 10)

Other Frequency Options

You can use different frequency aliases for rounding ?

import pandas as pd

datetimeindex = pd.date_range('2021-09-29 07:22:33', periods=3, freq='17T')
print("Original:", datetimeindex)

# Round to different frequencies
print("30T (30 min):", datetimeindex.round('30T'))
print("15T (15 min):", datetimeindex.round('15T'))
print("H (hour):", datetimeindex.round('H'))
Original: DatetimeIndex(['2021-09-29 07:22:33', '2021-09-29 07:39:33',
               '2021-09-29 07:56:33'],
              dtype='datetime64[ns]', freq='17T')
30T (30 min): DatetimeIndex(['2021-09-29 07:30:00', '2021-09-29 07:30:00',
               '2021-09-29 08:00:00'],
              dtype='datetime64[ns]', freq=None)
15T (15 min): DatetimeIndex(['2021-09-29 07:30:00', '2021-09-29 07:45:00',
               '2021-09-29 08:00:00'],
              dtype='datetime64[ns]', freq=None)
H (hour): DatetimeIndex(['2021-09-29 07:00:00', '2021-09-29 08:00:00',
               '2021-09-29 08:00:00'],
              dtype='datetime64[ns]', freq=None)

Conclusion

Use DateTimeIndex.round(freq='H') to round timestamps to the nearest hour. The method preserves timezone information and follows standard rounding rules where values at exactly 30 minutes round up to the next hour.

Updated on: 2026-03-26T17:17:15+05:30

943 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements