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

To perform floor operation on the DateTimeIndex with minutely frequency, use the DateTimeIndex.floor() method. For minutely frequency, use the freq parameter with value 'T'.

What is Floor Operation?

The floor operation rounds down datetime values to the nearest specified frequency boundary. For minute frequency, it rounds down to the beginning of the minute (sets seconds and microseconds to zero).

Syntax

DateTimeIndex.floor(freq)

Parameters:

  • freq: Frequency string. Use 'T' or 'min' for minutely frequency

Creating DateTimeIndex

Let's create a DateTimeIndex with seconds frequency to demonstrate the floor operation ?

import pandas as pd

# Create DatetimeIndex with period 5 and frequency as 40 seconds
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='40S')

print("Original DateTimeIndex...")
print(datetimeindex)
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')

Performing Floor Operation

Now let's apply the floor operation with minutely frequency using 'T' ?

import pandas as pd

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

# Floor operation with minute frequency
floored_index = datetimeindex.floor(freq='T')

print("After floor operation with minute frequency:")
print(floored_index)
After floor operation with minute frequency:
DatetimeIndex(['2021-10-18 07:20:00+10:30', '2021-10-18 07:21:00+10:30',
               '2021-10-18 07:21:00+10:30', '2021-10-18 07:22:00+10:30',
               '2021-10-18 07:23:00+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq=None)

Complete Example

Here's a complete example showing the original DateTimeIndex, frequency information, and floor operation ?

import pandas as pd

# Create DatetimeIndex with period 5 and frequency as 40 seconds
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
                             tz='Australia/Adelaide', freq='40S')

# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)

# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)

# Getting the minute values
res = datetimeindex.minute
print("\nThe minute from DateTimeIndex...")
print(res)

# Floor operation on DateTimeIndex with minute frequency
print("\nPerforming floor operation with minute frequency...")
print(datetimeindex.floor(freq='T'))
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')

DateTimeIndex frequency...
<40 * Seconds>

The minute from DateTimeIndex...
Int64Index([20, 21, 21, 22, 23], dtype='int64')

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

Key Points

  • Use freq='T' or freq='min' for minutely frequency
  • Floor operation rounds down to the nearest minute boundary
  • Seconds and microseconds are set to zero in the result
  • The original timezone information is preserved

Conclusion

The DateTimeIndex.floor() method with freq='T' effectively rounds down datetime values to the beginning of each minute. This is useful for time-based grouping and analysis in data science applications.

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

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements