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 - Return a new Timedelta ceiled to this resolution
To return a new Timedelta ceiled to this resolution, use the timedelta.ceil() method. The ceil() method rounds up to the nearest specified frequency unit, similar to the mathematical ceiling function.
Syntax
timedelta.ceil(freq)
Parameters:
- freq − String representing the frequency to ceil to (e.g., 'D' for days, 'H' for hours, 'T' for minutes)
Basic Example
Let's create a Timedelta object and ceil it to days frequency ?
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)
# Return the ceiled Timedelta to days frequency
ceiled_timedelta = timedelta.ceil(freq='D')
# Display the ceiled Timedelta
print("\nTimedelta ceiled to days:")
print(ceiled_timedelta)
Original Timedelta: 6 days 00:01:30 Timedelta ceiled to days: 7 days 00:00:00
Different Frequency Examples
You can ceil to different frequency units ?
import pandas as pd
# Create a Timedelta object
td = pd.Timedelta('2 days 5 hours 30 minutes 45 seconds')
print("Original Timedelta:", td)
# Ceil to different frequencies
print("\nCeiled to hours (H):", td.ceil(freq='H'))
print("Ceiled to minutes (T):", td.ceil(freq='T'))
print("Ceiled to seconds (S):", td.ceil(freq='S'))
Original Timedelta: 2 days 05:30:45 Ceiled to hours (H): 2 days 06:00:00 Ceiled to minutes (T): 2 days 05:31:00 Ceiled to seconds (S): 2 days 05:30:45
Common Frequency Codes
| Code | Description | Example |
|---|---|---|
| D | Calendar day | 24 hours |
| H | Hour | 60 minutes |
| T | Minute | 60 seconds |
| S | Second | 1000 milliseconds |
How It Works
The ceil() method works by:
- Taking the current Timedelta value
- Rounding up to the next boundary of the specified frequency
- Returning a new Timedelta object with the ceiled value
For example, if you have 6 days 1 minute 30 seconds and ceil to days, it rounds up to 7 days since there's any time beyond 6 complete days.
Conclusion
The ceil() method rounds Timedelta values up to the nearest specified frequency boundary. Use frequency codes like 'D', 'H', 'T', or 'S' to control the ceiling resolution.
Advertisements
