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 ceil operation on the TimeDeltaIndex object with microseconds frequency
To perform ceil operation on the TimeDeltaIndex with microseconds frequency, use the TimeDeltaIndex.ceil() method. For microseconds frequency, use the freq parameter with value 'us'.
What is the Ceil Operation?
The ceil operation rounds up time deltas to the nearest specified frequency unit. When applied with microseconds frequency, it rounds up any nanosecond components to the next microsecond.
Syntax
TimeDeltaIndex.ceil(freq)
Parameters:
- freq ? The frequency to round up to. Use 'us' for microseconds.
Creating a TimeDeltaIndex
At first, import the required libraries ?
import pandas as pd
# Create a TimeDeltaIndex object with microseconds and nanoseconds
tdIndex = pd.TimedeltaIndex(data=['4 day 8h 20min 35us 45ns', '+17:42:19.999999',
'9 day 3h 08:16:02.000055', '+22:35:25.000075'])
# Display TimedeltaIndex
print("TimedeltaIndex...")
print(tdIndex)
TimedeltaIndex...
TimedeltaIndex(['4 days 08:20:00.000035045', '0 days 17:42:19.999999',
'9 days 11:16:02.000055', '0 days 22:35:25.000075'],
dtype='timedelta64[ns]', freq=None)
Performing Ceil Operation with Microseconds Frequency
Apply the ceil operation to round up to the nearest microsecond ?
import pandas as pd
# Create a TimeDeltaIndex object
tdIndex = pd.TimedeltaIndex(data=['4 day 8h 20min 35us 45ns', '+17:42:19.999999',
'9 day 3h 08:16:02.000055', '+22:35:25.000075'])
print("Original TimedeltaIndex:")
print(tdIndex)
# Ceil operation with microseconds frequency
result = tdIndex.ceil(freq='us')
print("\nAfter Ceil operation with microseconds frequency:")
print(result)
Original TimedeltaIndex:
TimedeltaIndex(['4 days 08:20:00.000035045', '0 days 17:42:19.999999',
'9 days 11:16:02.000055', '0 days 22:35:25.000075'],
dtype='timedelta64[ns]', freq=None)
After Ceil operation with microseconds frequency:
TimedeltaIndex(['4 days 08:20:00.000036', '0 days 17:42:19.999999',
'9 days 11:16:02.000055', '0 days 22:35:25.000075'],
dtype='timedelta64[ns]', freq=None)
Complete Example with Components Analysis
import pandas as pd
# Create a TimeDeltaIndex object
tdIndex = pd.TimedeltaIndex(data=['4 day 8h 20min 35us 45ns', '+17:42:19.999999',
'9 day 3h 08:16:02.000055', '+22:35:25.000075'])
# Display TimedeltaIndex
print("TimedeltaIndex...")
print(tdIndex)
# Return a dataframe of the components of TimeDeltas
print("\nThe Dataframe of the components of TimeDeltas...")
print(tdIndex.components)
# Ceil operation on TimeDeltaIndex with microseconds frequency
print("\nPerforming Ceil operation with microseconds frequency...")
print(tdIndex.ceil(freq='us'))
TimedeltaIndex...
TimedeltaIndex(['4 days 08:20:00.000035045', '0 days 17:42:19.999999',
'9 days 11:16:02.000055', '0 days 22:35:25.000075'],
dtype='timedelta64[ns]', freq=None)
The Dataframe of the components of TimeDeltas...
days hours minutes seconds milliseconds microseconds nanoseconds
0 4 8 20 0 0 35 45
1 0 17 42 19 999 999 0
2 9 11 16 2 0 55 0
3 0 22 35 25 0 75 0
Performing Ceil operation with microseconds frequency...
TimedeltaIndex(['4 days 08:20:00.000036', '0 days 17:42:19.999999',
'9 days 11:16:02.000055', '0 days 22:35:25.000075'],
dtype='timedelta64[ns]', freq=None)
Key Points
- The first timedelta had 45 nanoseconds, which got rounded up to the next microsecond (35 ? 36)
- Other timedeltas with no nanosecond components remained unchanged
- The 'us' frequency parameter specifically targets microsecond precision
Conclusion
Use TimeDeltaIndex.ceil(freq='us') to round up time deltas to the nearest microsecond. This operation is useful when you need to normalize time precision and round up any nanosecond components.
Advertisements
