Python Pandas - How to Round the DateTimeIndex with microseconds frequency

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

Creating a DateTimeIndex

First, let's create a DateTimeIndex with nanosecond precision timestamps ?

import pandas as pd

# Create DatetimeIndex with period 5 and frequency as seconds
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='28s')

print("Original DateTimeIndex:")
print(datetimeindex)
print("\nDateTimeIndex frequency:")
print(datetimeindex.freq)
Original DateTimeIndex:
DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30',
               '2021-09-29 07:21:00.261811624+09:30',
               '2021-09-29 07:21:28.261811624+09:30',
               '2021-09-29 07:21:56.261811624+09:30',
               '2021-09-29 07:22:24.261811624+09:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='28S')

DateTimeIndex frequency:
<28 * Seconds>

Rounding to Microseconds Frequency

Now, let's round the DateTimeIndex to microseconds precision using freq='us' ?

import pandas as pd

# Create DatetimeIndex with nanosecond precision
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='28s')

# Round operation to microseconds frequency
rounded_index = datetimeindex.round(freq='us')

print("Rounded to microseconds frequency:")
print(rounded_index)
Rounded to microseconds frequency:
DatetimeIndex(['2021-09-29 07:20:32.261812+09:30',
               '2021-09-29 07:21:00.261812+09:30',
               '2021-09-29 07:21:28.261812+09:30',
               '2021-09-29 07:21:56.261812+09:30',
               '2021-09-29 07:22:24.261812+09:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq=None)

How It Works

The round() method rounds the datetime values to the nearest specified frequency. When using 'us' (microseconds), the nanosecond precision (.261811624) gets rounded to microsecond precision (.261812), effectively removing the last 3 digits.

Complete Example

import pandas as pd

# Create DatetimeIndex with nanosecond precision timestamps
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='28s')

print("Original DateTimeIndex:")
print(datetimeindex)
print(f"\nOriginal frequency: {datetimeindex.freq}")

# Round to microseconds frequency
rounded_index = datetimeindex.round(freq='us')

print("\nRounded to microseconds frequency:")
print(rounded_index)
print(f"Frequency after rounding: {rounded_index.freq}")
Original DateTimeIndex:
DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30',
               '2021-09-29 07:21:00.261811624+09:30',
               '2021-09-29 07:21:28.261811624+09:30',
               '2021-09-29 07:21:56.261811624+09:30',
               '2021-09-29 07:22:24.261811624+09:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='28S')

Original frequency: <28 * Seconds>

Rounded to microseconds frequency:
DatetimeIndex(['2021-09-29 07:20:32.261812+09:30',
               '2021-09-29 07:21:00.261812+09:30',
               '2021-09-29 07:21:28.261812+09:30',
               '2021-09-29 07:21:56.261812+09:30',
               '2021-09-29 07:22:24.261812+09:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq=None)

Frequency after rounding: None

Conclusion

Use DateTimeIndex.round(freq='us') to round datetime values to microsecond precision. This removes nanosecond-level detail and can be useful for data alignment and precision control in time series analysis.

Updated on: 2026-03-26T17:27:49+05:30

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements