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

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

What is Floor Operation?

The floor operation rounds down time values to the nearest specified frequency unit. When applied with milliseconds frequency, it removes precision below milliseconds (microseconds and nanoseconds).

Syntax

TimeDeltaIndex.floor(freq)

Parameters

  • freq: The frequency level to floor to. Use 'ms' for milliseconds.

Example

Let's create a TimeDeltaIndex and perform floor operation with milliseconds frequency ?

import pandas as pd

# Create a TimeDeltaIndex object
# We have set the timedelta-like data using the 'data' parameter
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'])

# Display TimedeltaIndex
print("TimedeltaIndex...\n", tdIndex)

# Return a dataframe of the components of TimeDeltas
print("\nThe Dataframe of the components of TimeDeltas...\n", tdIndex.components)

# Floor operation on TimeDeltaIndex date with milliseconds frequency
# For milliseconds frequency, we have used 'ms'
print("\nPerforming Floor operation with milliseconds frequency...\n",
      tdIndex.floor(freq='ms'))
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)

The Dataframe of the components of TimeDeltas...
   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

Performing Floor operation with milliseconds frequency...
 TimedeltaIndex(['5 days 08:20:00', '0 days 17:42:19.999000',
                '7 days 11:16:02', '0 days 22:35:25.999000'],
               dtype='timedelta64[ns]', freq=None)

How It Works

The floor operation removes precision below the specified frequency:

  • Original: '5 days 08:20:00.000035045' ? Floored: '5 days 08:20:00' (microseconds and nanoseconds removed)
  • Original: '0 days 17:42:19.999999' ? Floored: '0 days 17:42:19.999000' (microseconds rounded down)
  • Values already at millisecond precision remain unchanged

Conclusion

Use TimeDeltaIndex.floor(freq='ms') to round down time delta values to millisecond precision. This operation removes microseconds and nanoseconds components, providing cleaner time representations.

Updated on: 2026-03-26T17:57:30+05:30

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements