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 - How to Round the TimeDeltaIndex with seconds frequency
To round the TimeDeltaIndex with seconds frequency, use the TimeDeltaIndex.round() method. For seconds frequency, use the freq parameter with value 'S'.
Syntax
TimedeltaIndex.round(freq)
Where freq is the frequency string, such as 'S' for seconds, 'T' for minutes, or 'H' for hours.
Creating a TimeDeltaIndex
First, import pandas and create a TimeDeltaIndex object with timedelta-like data ?
import pandas as pd
# Create a TimeDeltaIndex object with various time durations
tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
'2 day 4h 03:08:02.000045', '+21:15:45.999999'])
print("Original TimedeltaIndex:")
print(tdIndex)
Original TimedeltaIndex:
TimedeltaIndex(['10 days 05:02:00.000003010', '0 days 22:39:19.999999',
'2 days 07:08:02.000045', '0 days 21:15:45.999999'],
dtype='timedelta64[ns]', freq=None)
Rounding to Seconds Frequency
Use the round() method with freq='S' to round to the nearest second ?
import pandas as pd
tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
'2 day 4h 03:08:02.000045', '+21:15:45.999999'])
# Round to seconds frequency
rounded_index = tdIndex.round(freq='S')
print("Rounded to seconds:")
print(rounded_index)
Rounded to seconds:
TimedeltaIndex(['10 days 05:02:00', '0 days 22:39:20', '2 days 07:08:02',
'0 days 21:15:46'],
dtype='timedelta64[ns]', freq=None)
Complete Example
Here's a comprehensive example showing the components and rounding operation ?
import pandas as pd
# Create a TimeDeltaIndex object
tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
'2 day 4h 03:08:02.000045', '+21:15:45.999999'])
# Display original TimedeltaIndex
print("TimedeltaIndex...")
print(tdIndex)
# Show components breakdown
print("\nThe Dataframe of the components of TimeDeltas...")
print(tdIndex.components)
# Round operation with seconds frequency
print("\nPerforming round operation with seconds frequency...")
print(tdIndex.round(freq='S'))
TimedeltaIndex...
TimedeltaIndex(['10 days 05:02:00.000003010', '0 days 22:39:19.999999',
'2 days 07:08:02.000045', '0 days 21:15:45.999999'],
dtype='timedelta64[ns]', freq=None)
The Dataframe of the components of TimeDeltas...
days hours minutes seconds milliseconds microseconds nanoseconds
0 10 5 2 0 0 3 10
1 0 22 39 19 999 999 0
2 2 7 8 2 0 45 0
3 0 21 15 45 999 999 0
Performing round operation with seconds frequency...
TimedeltaIndex(['10 days 05:02:00', '0 days 22:39:20', '2 days 07:08:02',
'0 days 21:15:46'],
dtype='timedelta64[ns]', freq=None)
How Rounding Works
The rounding operation follows standard rounding rules:
- Microseconds and nanoseconds are rounded to the nearest second
- Values ? 0.5 seconds round up to the next second
- Values < 0.5 seconds round down to the current second
Conclusion
Use TimedeltaIndex.round(freq='S') to round time durations to the nearest second. This removes sub-second precision while following standard rounding rules for cleaner time representations.
