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 - How to perform ceil operation on the DateTimeIndex with seconds frequency
To perform ceil operation on the DateTimeIndex with seconds frequency, use the DateTimeIndex.ceil() method. The ceil operation rounds up datetime values to the nearest second, removing fractional parts. For seconds frequency, use the freq parameter with value 'S'.
Creating DateTimeIndex
First, create a DateTimeIndex with microseconds to demonstrate the ceil operation ?
import pandas as pd
# Create DatetimeIndex with period 5 and frequency as 40 seconds
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
print("DateTimeIndex...")
print(datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
'2021-10-18 07:21:12.261811624+10:30',
'2021-10-18 07:21:52.261811624+10:30',
'2021-10-18 07:22:32.261811624+10:30',
'2021-10-18 07:23:12.261811624+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='40S')
Performing Ceil Operation
Apply ceil operation with seconds frequency to round up to the nearest second ?
import pandas as pd
# Create DatetimeIndex with microseconds
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
# Perform ceil operation with seconds frequency
ceil_result = datetimeindex.ceil(freq='S')
print("Original DateTimeIndex...")
print(datetimeindex)
print("\nAfter ceil operation with seconds frequency...")
print(ceil_result)
Original DateTimeIndex...
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
'2021-10-18 07:21:12.261811624+10:30',
'2021-10-18 07:21:52.261811624+10:30',
'2021-10-18 07:22:32.261811624+10:30',
'2021-10-18 07:23:12.261811624+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='40S')
After ceil operation with seconds frequency...
DatetimeIndex(['2021-10-18 07:20:33+10:30', '2021-10-18 07:21:13+10:30',
'2021-10-18 07:21:53+10:30', '2021-10-18 07:22:33+10:30',
'2021-10-18 07:23:13+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Complete Example
Here's a complete example showing the ceil operation and extracting seconds ?
import pandas as pd
# Create DatetimeIndex with period 5 and frequency as 40 seconds
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
print("DateTimeIndex...")
print(datetimeindex)
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
# Extract seconds from the DateTimeIndex
seconds = datetimeindex.second
print("\nThe second from DateTimeIndex...")
print(seconds)
# Perform ceil operation with seconds frequency
print("\nPerforming ceil operation with seconds frequency...")
print(datetimeindex.ceil(freq='S'))
DateTimeIndex...
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
'2021-10-18 07:21:12.261811624+10:30',
'2021-10-18 07:21:52.261811624+10:30',
'2021-10-18 07:22:32.261811624+10:30',
'2021-10-18 07:23:12.261811624+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='40S')
DateTimeIndex frequency...
<40 * Seconds>
The second from DateTimeIndex...
Index([32, 12, 52, 32, 12], dtype='int64')
Performing ceil operation with seconds frequency...
DatetimeIndex(['2021-10-18 07:20:33+10:30', '2021-10-18 07:21:13+10:30',
'2021-10-18 07:21:53+10:30', '2021-10-18 07:22:33+10:30',
'2021-10-18 07:23:13+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
How It Works
The ceil() method rounds up datetime values to the specified frequency. In this case, freq='S' rounds up to the nearest second, removing microseconds. Notice how timestamps like 07:20:32.261811624 become 07:20:33, effectively adding 1 second and removing fractional parts.
Conclusion
Use DateTimeIndex.ceil(freq='S') to round up datetime values to the nearest second. This operation removes fractional seconds and ensures all timestamps align to whole seconds.
