Python Pandas - Perform floor operation on the TimeDeltaIndex with seconds frequency

To perform floor operation on the TimeDeltaIndex with seconds frequency, use the TimeDeltaIndex.floor() method. For seconds frequency, use the freq parameter with value 'S'.

What is Floor Operation?

The floor operation rounds down each TimeDelta value to the nearest boundary of the specified frequency. When using seconds frequency ('S'), it removes all sub-second precision (microseconds and nanoseconds) from the TimeDelta values.

Syntax

TimeDeltaIndex.floor(freq)

Parameters:

  • freq: Frequency string. Use 'S' for seconds frequency.

Creating TimeDeltaIndex

First, let's create a TimeDeltaIndex object with various time delta values ?

import pandas as pd

# Create a TimeDeltaIndex object with timedelta-like data
tdIndex = pd.TimedeltaIndex(data=['5 day 8h 20min 35us 45ns', '+17:42:19.999999',
                                  '7 day 3h 08:16:02.000055', '+22:35:25.999999'])

print("TimedeltaIndex...")
print(tdIndex)
TimedeltaIndex...
TimedeltaIndex(['5 days 08:20:00.000035045', '0 days 17:42:19.999999',
                '7 days 11:16:02.000055', '0 days 22:35:25.999999'],
               dtype='timedelta64[ns]', freq=None)

Applying Floor Operation with Seconds Frequency

Now let's apply the floor operation with seconds frequency to round down to the nearest second ?

import pandas as pd

# Create a TimeDeltaIndex object
tdIndex = pd.TimedeltaIndex(data=['5 day 8h 20min 35us 45ns', '+17:42:19.999999',
                                  '7 day 3h 08:16:02.000055', '+22:35:25.999999'])

print("Original TimedeltaIndex:")
print(tdIndex)

# Floor operation with seconds frequency
floored_index = tdIndex.floor(freq='S')
print("\nAfter Floor operation with seconds frequency:")
print(floored_index)
Original TimedeltaIndex:
TimedeltaIndex(['5 days 08:20:00.000035045', '0 days 17:42:19.999999',
                '7 days 11:16:02.000055', '0 days 22:35:25.999999'],
               dtype='timedelta64[ns]', freq=None)

After Floor operation with seconds frequency:
TimedeltaIndex(['5 days 08:20:00', '0 days 17:42:19', '7 days 11:16:02',
                '0 days 22:35:25'],
               dtype='timedelta64[ns]', freq=None)

Viewing Components

You can also view the components breakdown to better understand the floor operation ?

import pandas as pd

tdIndex = pd.TimedeltaIndex(data=['5 day 8h 20min 35us 45ns', '+17:42:19.999999',
                                  '7 day 3h 08:16:02.000055', '+22:35:25.999999'])

print("Components of original TimedeltaIndex:")
print(tdIndex.components)

print("\nComponents after floor operation:")
floored_index = tdIndex.floor(freq='S')
print(floored_index.components)
Components of original TimedeltaIndex:
   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0     5      8       20        0             0            35           45
1     0     17       42       19           999           999            0
2     7     11       16        2             0            55            0
3     0     22       35       25           999           999            0

Components after floor operation:
   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0     5      8       20        0             0             0            0
1     0     17       42       19             0             0            0
2     7     11       16        2             0             0            0
3     0     22       35       25             0             0            0

Conclusion

The floor() method with 'S' frequency rounds down TimeDelta values to the nearest second, effectively removing all microseconds and nanoseconds. This is useful for standardizing time data to second precision.

Updated on: 2026-03-26T17:56:51+05:30

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements