Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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)
Advertisements