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
Selected Reading
Python Pandas - Perform floor operation on the TimeDeltaIndex with microseconds frequency
The TimeDeltaIndex.floor() method performs a floor operation on TimeDelta values, rounding down to the nearest specified frequency. When working with microseconds frequency, use freq='us' to round down to the nearest microsecond.
Syntax
TimeDeltaIndex.floor(freq)
Parameters
- freq ? The frequency to floor to. For microseconds, use 'us'
Creating a TimeDeltaIndex
First, create a TimeDeltaIndex with various time intervals including nanosecond precision ?
import pandas as pd
# Create a TimeDeltaIndex object with microseconds and nanoseconds
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)
Performing Floor Operation with Microseconds Frequency
Apply the floor operation to round down to the nearest microsecond ?
import pandas as pd
# Create TimeDeltaIndex with nanosecond precision
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'])
# Perform floor operation with microseconds frequency
floored_index = tdIndex.floor(freq='us')
print("After floor operation with microseconds frequency...")
print(floored_index)
After floor operation with microseconds frequency...
TimedeltaIndex(['5 days 08:20:00.000035', '0 days 17:42:19.999999',
'7 days 11:16:02.000055', '0 days 22:35:25.999999'],
dtype='timedelta64[ns]', freq=None)
Complete Example
Here's a complete example showing the components breakdown and floor operation ?
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 microseconds frequency
print("\nPerforming Floor operation with microseconds frequency...")
print(tdIndex.floor(freq='us'))
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 microseconds frequency...
TimedeltaIndex(['5 days 08:20:00.000035', '0 days 17:42:19.999999',
'7 days 11:16:02.000055', '0 days 22:35:25.999999'],
dtype='timedelta64[ns]', freq=None)
Key Points
- The floor operation rounds down to the nearest specified frequency unit
- Notice how the nanoseconds (45ns) in the first entry are removed after flooring to microseconds
- Values already at microsecond precision remain unchanged
- The operation returns a new TimeDeltaIndex object
Conclusion
Use TimeDeltaIndex.floor(freq='us') to round TimeDelta values down to the nearest microsecond. This operation removes any nanosecond precision while preserving all higher-level time components.
Advertisements
