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 Round the DateTimeIndex with minute frequency
To round the DateTimeIndex with minute frequency, use the DateTimeIndex.round() method. For minute frequency, use the freq parameter with value 'T'.
Creating a DateTimeIndex
First, let's create a DateTimeIndex with seconds frequency to demonstrate the rounding operation ?
import pandas as pd
# DatetimeIndex with period 5 and frequency as 45 seconds
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='45s')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
print("DateTimeIndex frequency...\n", datetimeindex.freq)
The output of the above code is ?
DateTimeIndex...
DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:00:45+09:30',
'2021-09-29 07:01:30+09:30', '2021-09-29 07:02:15+09:30',
'2021-09-29 07:03:00+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='45S')
DateTimeIndex frequency...
<45 * Seconds>
Extracting Minutes from DateTimeIndex
You can extract the minute component to see the current minute values before rounding ?
import pandas as pd
datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='45s')
# getting the minute component
minutes = datetimeindex.minute
print("The minute from DateTimeIndex...\n", minutes)
The output of the above code is ?
The minute from DateTimeIndex... Int64Index([0, 0, 1, 2, 3], dtype='int64')
Rounding to Minute Frequency
Now let's perform the round operation with minute frequency using 'T' as the frequency parameter ?
import pandas as pd
datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='45s')
print("Original DateTimeIndex...\n", datetimeindex)
# Round operation on DateTimeIndex with minute frequency
# For minute frequency, we use 'T'
rounded_index = datetimeindex.round(freq='T')
print("\nAfter rounding to minute frequency...\n", rounded_index)
The output of the above code is ?
Original DateTimeIndex...
DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:00:45+09:30',
'2021-09-29 07:01:30+09:30', '2021-09-29 07:02:15+09:30',
'2021-09-29 07:03:00+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='45S')
After rounding to minute frequency...
DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:01:00+09:30',
'2021-09-29 07:02:00+09:30', '2021-09-29 07:02:00+09:30',
'2021-09-29 07:03:00+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
How the Rounding Works
The rounding follows standard mathematical rounding rules:
- 07:00:00 remains 07:00:00 (no change needed)
- 07:00:45 rounds up to 07:01:00 (45 seconds > 30 seconds)
- 07:01:30 rounds up to 07:02:00 (30 seconds rounds up)
- 07:02:15 rounds down to 07:02:00 (15 seconds < 30 seconds)
- 07:03:00 remains 07:03:00 (already on the minute)
Conclusion
Use DateTimeIndex.round(freq='T') to round timestamps to the nearest minute. The rounding follows standard mathematical rules where 30+ seconds round up and <30 seconds round down.
