- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Round a DateTimeIndex with frequency as multiples of a single unit
To round the DateTimeIndex with frequency as multiples of a single unit, use the DateTimeIndex.round() method. Set the freq parameter for frequency.
At first, import the required libraries −
import pandas as pd
DatetimeIndex with period 5 and frequency as H i.e. hours −
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='H')
Display DateTimeIndex −
print("DateTimeIndex...\n", datetimeindex)
Round a DateTimeIndex with 10 minutes frequency i.e. multiples of a single unit. For minutes frequency, we have used 'T −
print("\nPerforming round operation with multiples of a single unit frequency...\n", datetimeindex.round(freq='10T'))
Example
Following is the code −
import pandas as pd # DatetimeIndex with period 5 and frequency as H i.e. hours # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='H') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...\n", datetimeindex.freq) # Round a DateTimeIndex with 10 minutes frequency i.e. multiples of a single unit # For minutes frequency, we have used 'T' print("\nPerforming round operation with multiples of a single unit frequency...\n", datetimeindex.round(freq='10T'))
Output
This will produce the following code −
DateTimeIndex... DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30', '2021-09-29 08:20:32.261811624+09:30', '2021-09-29 09:20:32.261811624+09:30', '2021-09-29 10:20:32.261811624+09:30', '2021-09-29 11:20:32.261811624+09:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='H') DateTimeIndex frequency... <Hour> Performing round operation with multiples of a single unit frequency... DatetimeIndex(['2021-09-29 07:20:00+09:30', '2021-09-29 08:20:00+09:30', '2021-09-29 09:20:00+09:30', '2021-09-29 10:20:00+09:30', '2021-09-29 11:20:00+09:30'], dtype='datetime64[ns, Australia/Adelaide]', freq=None)
- Related Articles
- Python Pandas - How to Round the DateTimeIndex with hourly frequency
- Python Pandas - How to Round the DateTimeIndex with minute frequency
- Python Pandas - How to Round the DateTimeIndex with seconds frequency
- Python Pandas - How to Round the DateTimeIndex with milliseconds frequency
- Python Pandas - How to Round the DateTimeIndex with microseconds frequency
- Python Pandas - Extract the frequency object as a string from the DateTimeIndex
- Python Pandas - Round the Timedelta with hourly frequency
- Python Pandas - Round the Timedelta with minutely frequency
- Python Pandas - Round the Timedelta with seconds frequency
- Python Pandas - Round the Timedelta with daily frequency
- Python Pandas - Create a datetime with DateTimeIndex
- Python Pandas - Extract the frequency from the DateTimeIndex
- Python Pandas - How to Round the TimeDeltaIndex with hourly frequency
- Python Pandas - How to Round the TimeDeltaIndex with minute frequency
- Python Pandas - How to Round the TimeDeltaIndex with seconds frequency

Advertisements