Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas - Perform floor operation on the TimeDeltaIndex with minute frequency
To perform floor operation on the TimeDeltaIndex with minute frequency, use the TimeDeltaIndex.floor() method. The floor operation rounds down each time delta to the nearest minute boundary, effectively removing seconds, microseconds, and nanoseconds.
Syntax
TimedeltaIndex.floor(freq)
Parameters:
-
freq ? The frequency level to floor to. Use
'T'or'min'for minute frequency.
Creating a TimeDeltaIndex
First, let's create a TimeDeltaIndex with various time delta values ?
import pandas as pd
# Create a TimeDeltaIndex object with different time delta formats
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)
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)
Floor Operation with Minute Frequency
Apply the floor operation to round down to the nearest minute ?
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'])
# Floor operation with minute frequency using 'T'
floored_index = tdIndex.floor(freq='T')
print("After floor operation with minute frequency:")
print(floored_index)
After floor operation with minute frequency:
TimedeltaIndex(['5 days 08:20:00', '0 days 17:42:00', '7 days 11:16:00',
'0 days 22:35:00'],
dtype='timedelta64[ns]', freq=None)
Complete Example
Here's a complete example showing the original values, their components, and the floored results ?
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'])
# Display original TimedeltaIndex
print("TimedeltaIndex...")
print(tdIndex)
# Show the components breakdown
print("\nThe Dataframe of the components of TimeDeltas...")
print(tdIndex.components)
# Floor operation with minute frequency
print("\nPerforming Floor operation with minute frequency...")
print(tdIndex.floor(freq='T'))
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 minute frequency...
TimedeltaIndex(['5 days 08:20:00', '0 days 17:42:00', '7 days 11:16:00',
'0 days 22:35:00'],
dtype='timedelta64[ns]', freq=None)
How It Works
The floor operation with minute frequency ('T') rounds down each time delta to the nearest minute boundary:
-
'5 days 08:20:00.000035045'becomes'5 days 08:20:00' -
'0 days 17:42:19.999999'becomes'0 days 17:42:00' -
'7 days 11:16:02.000055'becomes'7 days 11:16:00' -
'0 days 22:35:25.999999'becomes'0 days 22:35:00'
Conclusion
The floor() method with freq='T' effectively truncates time deltas to minute precision by removing seconds, milliseconds, microseconds, and nanoseconds. This is useful for time-based grouping and analysis at minute-level granularity.
