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 minutely ceiling resolution
The timedelta.ceil() method returns a new Timedelta object ceiled to a specified resolution. For minutely ceiling resolution, use the frequency parameter freq='T' to round up to the nearest minute.
Syntax
The syntax for the ceil() method is ?
timedelta.ceil(freq)
Parameters
freq ? The frequency string representing the ceiling resolution. Use 'T' for minutes, 'H' for hours, 'D' for days, etc.
Creating a Timedelta Object
First, let's create a Timedelta object with various time components ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('2 days 10 hours 45 min 20 s')
# Display the original Timedelta
print("Original Timedelta:", timedelta)
Original Timedelta: 2 days 10:45:20
Applying Minutely Ceiling
Use ceil(freq='T') to round up to the nearest minute ?
import pandas as pd
# Create a Timedelta object
timedelta = pd.Timedelta('2 days 10 hours 45 min 20 s')
# Apply minutely ceiling resolution
ceiled_timedelta = timedelta.ceil(freq='T')
print("Original Timedelta:", timedelta)
print("Minutely Ceiled:", ceiled_timedelta)
Original Timedelta: 2 days 10:45:20 Minutely Ceiled: 2 days 10:46:00
How It Works
The ceil() method rounds up the time components. Since our original Timedelta has 20 seconds, it gets rounded up to the next minute (10:46:00). If there were no seconds, the time would remain unchanged.
Different Frequency Examples
Compare minutely ceiling with other frequency resolutions ?
import pandas as pd
# Create a Timedelta with precise time
timedelta = pd.Timedelta('1 day 5 hours 23 min 45 s')
print("Original:", timedelta)
print("Minutely ceiling (T):", timedelta.ceil(freq='T'))
print("Hourly ceiling (H):", timedelta.ceil(freq='H'))
print("Daily ceiling (D):", timedelta.ceil(freq='D'))
Original: 1 days 05:23:45 Minutely ceiling (T): 1 days 05:24:00 Hourly ceiling (H): 1 days 06:00:00 Daily ceiling (D): 2 days 00:00:00
Conclusion
The timedelta.ceil(freq='T') method rounds up time to the nearest minute, removing seconds and microseconds. This is useful for time-based data analysis where you need consistent minute-level precision.
