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 - How to Round the TimeDeltaIndex with microseconds frequency
To round the TimeDeltaIndex with microseconds frequency, use the TimeDeltaIndex.round() method. For microseconds frequency, set the freq parameter to 'us'.
Syntax
TimeDeltaIndex.round(freq)
Where freq is the frequency string. Use 'us' for microseconds.
Creating a TimeDeltaIndex
First, import pandas and create a TimeDeltaIndex with various time formats ?
import pandas as pd
# Create a TimeDeltaIndex object with different time formats
tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
'2 day 4h 03:08:02.000045', '+07:20:32.261811624'])
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 07:20:32.261811624'],
dtype='timedelta64[ns]', freq=None)
Rounding to Microseconds
Apply the round() method with freq='us' to round to microseconds precision ?
import pandas as pd
# Create TimeDeltaIndex
tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
'2 day 4h 03:08:02.000045', '+07:20:32.261811624'])
# Round to microseconds frequency
rounded_tdIndex = tdIndex.round(freq='us')
print("Rounded to microseconds:")
print(rounded_tdIndex)
Rounded to microseconds:
TimedeltaIndex(['10 days 05:02:00.000003', '0 days 22:39:19.999999',
'2 days 07:08:02.000045', '0 days 07:20:32.261812'],
dtype='timedelta64[ns]', freq=None)
Complete Example
Here's a comprehensive example showing the original values, their components, and the rounded result ?
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', '+07:20:32.261811624'])
# Display original TimedeltaIndex
print("TimedeltaIndex...")
print(tdIndex)
# Show components breakdown
print("\nThe Dataframe of the components of TimeDeltas...")
print(tdIndex.components)
# Round operation with microseconds frequency
print("\nPerforming round operation with microseconds frequency...")
print(tdIndex.round(freq='us'))
TimedeltaIndex...
TimedeltaIndex(['10 days 05:02:00.000003010', '0 days 22:39:19.999999',
'2 days 07:08:02.000045', '0 days 07:20:32.261811624'],
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 7 20 32 261 811 624
Performing round operation with microseconds frequency...
TimedeltaIndex(['10 days 05:02:00.000003', '0 days 22:39:19.999999',
'2 days 07:08:02.000045', '0 days 07:20:32.261812'],
dtype='timedelta64[ns]', freq=None)
Key Points
- The
round()method rounds nanoseconds to the nearest microsecond - Original precision is maintained for values already at microsecond level
- Nanosecond values (like 10ns in the first entry) are rounded to the nearest microsecond
- The
componentsproperty helps visualize the time breakdown before rounding
Conclusion
Use TimeDeltaIndex.round(freq='us') to round TimeDelta values to microseconds precision. This removes nanosecond-level precision while preserving the overall time structure.
Advertisements
