
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
Perform Floor Operation on DatetimeIndex with Hourly Frequency in Python Pandas
To perform floor operation on the DateTimeIndex with hourly frequency, use the DateTimeIndex.floor() method. For hourly frequency, use the freq parameter with value ‘H’.
At first, import the required libraries −
import pandas as pd
Create a DatetimeIndex with period 5 and frequency as min i.e. minutes −
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='20min')
Display DateTimeInde −
print("DateTimeIndex...\n", datetimeindex)
Floor operation on DateTimeIndex date with hourly frequency, For hourly frequency, we have used 'H' −
print("\nPerforming floor operation with hourly frequency...\n", datetimeindex.floor(freq='H'))
Example
Following is the code −
import pandas as pd # DatetimeIndex with period 5 and frequency as min i.e. minutes # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='20min') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...\n", datetimeindex.freq) # Floor operation on DateTimeIndex date with hourly frequency # For hourly frequency, we have used 'H' print("\nPerforming floor operation with hourly frequency...\n", datetimeindex.floor(freq='H'))
Output
This will produce the following code −
DateTimeIndex... DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30', '2021-09-29 07:40:32.261811624+09:30', '2021-09-29 08:00:32.261811624+09:30', '2021-09-29 08:20:32.261811624+09:30', '2021-09-29 08:40:32.261811624+09:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='20T') DateTimeIndex frequency... <20 * Minutes> Performing floor operation with hourly frequency... DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:00:00+09:30', '2021-09-29 08:00:00+09:30', '2021-09-29 08:00:00+09:30', '2021-09-29 08:00:00+09:30'], dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Advertisements