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 minute frequency
To round the TimeDeltaIndex with minute frequency, use the TimeDeltaIndex.round() method. For minute frequency, use the freq parameter with value 'T'.
Syntax
TimedeltaIndex.round(freq)
Parameters:
- freq ? Frequency string. For minute frequency, use 'T'
Creating a TimeDeltaIndex
At first, import the required libraries and create a TimeDeltaIndex object ?
import pandas as pd
# Create a TimeDeltaIndex object
# We have set the timedelta-like data using the 'data' parameter
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 TimedeltaIndex
print("TimedeltaIndex...\n", tdIndex)
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)
Viewing Components
You can view the individual components of the TimeDeltaIndex using the components property ?
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'])
# Return a dataframe of the components of TimeDeltas
print("The Dataframe of the components of TimeDeltas...\n", tdIndex.components)
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
Rounding with Minute Frequency
Round operation on TimeDeltaIndex with minute frequency. For minute frequency, we use 'T' ?
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'])
print("Original TimedeltaIndex...")
print(tdIndex)
# Round operation on TimeDeltaIndex with minute frequency
# For minute frequency, we use 'T'
print("\nPerforming round operation with minute frequency...")
rounded_tdIndex = tdIndex.round(freq='T')
print(rounded_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)
Performing round operation with minute frequency...
TimedeltaIndex(['10 days 05:02:00', '0 days 22:39:00', '2 days 07:08:00',
'0 days 21:16:00'],
dtype='timedelta64[ns]', freq=None)
How It Works
The round() method rounds each TimeDelta to the nearest minute:
- '10 days 05:02:00.000003010' ? '10 days 05:02:00' (seconds rounded down)
- '0 days 22:39:19.999999' ? '0 days 22:39:00' (19 seconds rounded down)
- '2 days 07:08:02.000045' ? '2 days 07:08:00' (2 seconds rounded down)
- '0 days 21:15:45.999999' ? '0 days 21:16:00' (45 seconds rounded up)
Conclusion
Use TimedeltaIndex.round(freq='T') to round TimeDelta values to the nearest minute. Values with seconds ? 30 are rounded up, while values with seconds < 30 are rounded down to the nearest minute.
Advertisements
