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 - Return a new Timedelta with daily ceiling resolution
To return a new Timedelta ceiled to this resolution, use the timedelta.ceil() method. For daily ceiling resolution, set the freq parameter to the value 'D'.
What is Ceiling Resolution?
Ceiling resolution rounds up a Timedelta to the nearest specified unit. For daily ceiling, any fractional day (hours, minutes, seconds) rounds up to the next full day.
Syntax
timedelta.ceil(freq)
Parameters:
- freq − The frequency string. Use 'D' for daily resolution.
Example
Let's create a Timedelta and apply daily ceiling resolution ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('6 days 1 min 30 s')
# Display the original Timedelta
print("Original Timedelta...")
print(timedelta)
# Apply daily ceiling resolution
ceiled_timedelta = timedelta.ceil(freq='D')
# Display the ceiled Timedelta
print("\nTimedelta (daily ceiled)...")
print(ceiled_timedelta)
Original Timedelta... 6 days 00:01:30 Timedelta (daily ceiled)... 7 days 00:00:00
How It Works
The original Timedelta of "6 days 1 min 30 s" contains fractional time beyond 6 complete days. The ceil('D') method rounds up to the next complete day, resulting in exactly 7 days.
Multiple Examples
import pandas as pd
# Different Timedelta examples
examples = [
'2 days 5 hours',
'10 days 0 hours',
'1 day 23 hours 59 min'
]
for example in examples:
td = pd.Timedelta(example)
ceiled = td.ceil(freq='D')
print(f"Original: {td}")
print(f"Ceiled: {ceiled}")
print("-" * 30)
Original: 2 days 05:00:00 Ceiled: 3 days 00:00:00 ------------------------------ Original: 10 days 00:00:00 Ceiled: 10 days 00:00:00 ------------------------------ Original: 1 days 23:59:00 Ceiled: 2 days 00:00:00 ------------------------------
Conclusion
The ceil('D') method rounds up any Timedelta to the next complete day. Use this when you need to ensure Timedeltas represent full day units without fractional time components.
