Pandas Series dt.ceil | Ceil DateTime To Specified Frequency
The dt.ceil() method performs ceil operation on the data to the specified frequency.
Example
Python3
import pandas as pd sr = pd.Series([ '2012-12-31 08:45' , '2019-1-1 12:30' , '2008-02-2 10:30' , '2010-1-1 09:25' , '2019-12-31 00:00' ]) idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ] sr.index = idx sr = pd.to_datetime(sr) result = sr.dt.ceil(freq = 'D' ) print (result) |
Output:
Syntax
Syntax: Series.dt.ceil(freq, ambiguous=’raise’, nonexistent=’raise’)
Parameter:
- freq: The frequency level to ceil the index to
- ambiguous: Only relevant for DatetimeIndex. ‘infer’ will attempt to infer fall dst-transition hours based on order
- nonexistent: A nonexistent time does not exist in a particular timezone where clocks moved forward due to DST.
Returns: DatetimeIndex, TimedeltaIndex, or Series
How to Round Up DateTime to Nearest Specified Frequency
To round up the DateTime to the nearest specified frequency we use the dt.ceil method of the Pandas library in Python.
Let us understand it better with an example:
Example:
Use the dt.ceil() function to ceil the DateTime data of the given series object to the specified frequency.
Python3
# importing pandas as pd import pandas as pd # Creating the Series sr = pd.Series(pd.date_range( '2012-12-31 09:45' , periods = 5 , freq = 'T' , tz = 'Asia / Calcutta' )) # Creating the index idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ] # set the index sr.index = idx # Print the series print (sr) |
Output :
Now we will use the dt.ceil() function to ceil the datetime values in the given series object to Hourly frequency.
Python3
# ceil to hourly frequency result = sr.dt.ceil(freq = 'H' ) # print the result print (result) |
Output :
As we can see in the output, the Pandas dt.ceil() function has successfully ceiled the DateTime values in the given series object to the specified frequency.
Contact Us