Python Pandas - How to perform floor operation on the DateTimeIndex with seconds frequency

The DateTimeIndex.floor() method performs a floor operation on datetime values, rounding down to the nearest specified frequency. For seconds frequency, use 'S' as the frequency parameter.

Creating a DateTimeIndex

First, let's create a DateTimeIndex with microsecond precision ?

import pandas as pd

# Create DateTimeIndex with 40-second intervals
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='40S')

print("DateTimeIndex...\n", datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
               '2021-10-18 07:21:12.261811624+10:30',
               '2021-10-18 07:21:52.261811624+10:30',
               '2021-10-18 07:22:32.261811624+10:30',
               '2021-10-18 07:23:12.261811624+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='40S')

Performing Floor Operation with Seconds Frequency

The floor operation with 'S' frequency removes microseconds and rounds down to the nearest second ?

import pandas as pd

# Create DateTimeIndex with microsecond precision
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='40S')

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

# Floor operation with seconds frequency
floored_index = datetimeindex.floor(freq='S')
print("\nAfter floor operation with seconds frequency...\n", floored_index)

# Display frequency information
print("\nOriginal frequency:", datetimeindex.freq)
print("Floored frequency:", floored_index.freq)
Original DateTimeIndex...
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
               '2021-10-18 07:21:12.261811624+10:30',
               '2021-10-18 07:21:52.261811624+10:30',
               '2021-10-18 07:22:32.261811624+10:30',
               '2021-10-18 07:23:12.261811624+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='40S')

After floor operation with seconds frequency...
DatetimeIndex(['2021-10-18 07:20:32+10:30', '2021-10-18 07:21:12+10:30',
               '2021-10-18 07:21:52+10:30', '2021-10-18 07:22:32+10:30',
               '2021-10-18 07:23:12+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq=None)

Original frequency: <40 * Seconds>
Floored frequency: None

Understanding the Floor Operation

The floor operation truncates the fractional seconds (microseconds) and rounds down to the nearest whole second. Notice how the microseconds .261811624 are removed from each timestamp ?

import pandas as pd

# Create a single timestamp for comparison
original = pd.Timestamp('2021-10-18 07:20:32.750000')
floored = original.floor('S')

print(f"Original timestamp: {original}")
print(f"Floored timestamp:  {floored}")
print(f"Difference: {original - floored}")
Original timestamp: 2021-10-18 07:20:32.750000
Floored timestamp: 2021-10-18 07:20:32
Difference: 0 days 00:00:00.750000

Conclusion

Use DateTimeIndex.floor('S') to round datetime values down to the nearest second. This operation removes fractional seconds while preserving the timezone information.

Updated on: 2026-03-26T17:28:47+05:30

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements