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 - How to Round the DateTimeIndex with seconds frequency
To round the DateTimeIndex with seconds frequency, use the DateTimeIndex.round() method. For seconds frequency, use the freq parameter with value 'S'.
Creating a DateTimeIndex
First, let's create a DateTimeIndex with nanosecond precision ?
import pandas as pd
# Create DatetimeIndex with period 5 and frequency as 28 seconds
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='28s')
print("Original DateTimeIndex...")
print(datetimeindex)
print("\nDateTimeIndex frequency:", datetimeindex.freq)
Original DateTimeIndex...
DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30',
'2021-09-29 07:21:00.261811624+09:30',
'2021-09-29 07:21:28.261811624+09:30',
'2021-09-29 07:21:56.261811624+09:30',
'2021-09-29 07:22:24.261811624+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='28S')
DateTimeIndex frequency: <28 * Seconds>
Rounding to Seconds Frequency
Use the round() method with freq='S' to round timestamps to the nearest second ?
import pandas as pd
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='28s')
# Round operation on DateTimeIndex with seconds frequency
rounded_index = datetimeindex.round(freq='S')
print("After rounding to seconds frequency...")
print(rounded_index)
After rounding to seconds frequency...
DatetimeIndex(['2021-09-29 07:20:32+09:30', '2021-09-29 07:21:00+09:30',
'2021-09-29 07:21:28+09:30', '2021-09-29 07:21:56+09:30',
'2021-09-29 07:22:24+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Extracting Seconds Component
You can extract the seconds component from the DateTimeIndex to see the values before rounding ?
import pandas as pd
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='28s')
# Extract the second component
seconds = datetimeindex.second
print("Seconds from DateTimeIndex:", seconds)
# Round and compare
rounded_index = datetimeindex.round(freq='S')
print("\nOriginal timestamps:")
for ts in datetimeindex:
print(ts)
print("\nRounded timestamps:")
for ts in rounded_index:
print(ts)
Seconds from DateTimeIndex: Int64Index([32, 0, 28, 56, 24], dtype='int64') Original timestamps: 2021-09-29 07:20:32.261811624+09:30 2021-09-29 07:21:00.261811624+09:30 2021-09-29 07:21:28.261811624+09:30 2021-09-29 07:21:56.261811624+09:30 2021-09-29 07:22:24.261811624+09:30 Rounded timestamps: 2021-09-29 07:20:32+09:30 2021-09-29 07:21:00+09:30 2021-09-29 07:21:28+09:30 2021-09-29 07:21:56+09:30 2021-09-29 07:22:24+09:30
Other Rounding Frequencies
You can round to different time frequencies using various freq parameter values ?
import pandas as pd
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=3,
tz='Australia/Adelaide', freq='28s')
print("Original:", datetimeindex[0])
print("Round to second:", datetimeindex.round('S')[0])
print("Round to minute:", datetimeindex.round('T')[0])
print("Round to hour:", datetimeindex.round('H')[0])
Original: 2021-09-29 07:20:32.261811624+09:30 Round to second: 2021-09-29 07:20:32+09:30 Round to minute: 2021-09-29 07:21:00+09:30 Round to hour: 2021-09-29 07:00:00+09:30
Conclusion
Use DateTimeIndex.round(freq='S') to round timestamps to the nearest second, removing sub-second precision. The method preserves timezone information while eliminating nanosecond and microsecond components.
Advertisements
