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 perform floor operation on the DateTimeIndex with hourly frequency
To perform floor operation on the DateTimeIndex with hourly frequency, use the DateTimeIndex.floor() method. The floor operation rounds down datetime values to the nearest specified frequency boundary.
What is Floor Operation?
The floor operation rounds down datetime values to the beginning of the specified time period. For hourly frequency, it rounds down to the start of the hour (minutes and seconds become 00:00).
Syntax
DateTimeIndex.floor(freq)
Parameters
- freq − Frequency string like 'H' for hourly, 'D' for daily, 'T' for minutes
Example
Let's create a DateTimeIndex and perform floor operation with hourly frequency ?
import pandas as pd
# Create DatetimeIndex with period 5 and frequency as 20 minutes
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='20min')
# Display original DateTimeIndex
print("Original DateTimeIndex...")
print(datetimeindex)
# Display frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
# Floor operation with hourly frequency
print("\nPerforming floor operation with hourly frequency...")
result = datetimeindex.floor(freq='H')
print(result)
Original 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)
How It Works
In the example above:
- Times like 07:20:32 and 07:40:32 are floored to 07:00:00
- Times like 08:00:32, 08:20:32, and 08:40:32 are floored to 08:00:00
- The seconds and microseconds are reset to zero
- The timezone information is preserved
Other Frequency Options
import pandas as pd
datetimeindex = pd.date_range('2021-09-29 07:25:45', periods=3, freq='15min')
print("Original DateTimeIndex...")
print(datetimeindex)
print("\nFloor to 30 minutes...")
print(datetimeindex.floor('30T'))
print("\nFloor to daily...")
print(datetimeindex.floor('D'))
Original DateTimeIndex...
DatetimeIndex(['2021-09-29 07:25:45', '2021-09-29 07:40:45',
'2021-09-29 07:55:45'],
dtype='datetime64[ns]', freq='15T')
Floor to 30 minutes...
DatetimeIndex(['2021-09-29 07:00:00', '2021-09-29 07:30:00',
'2021-09-29 07:30:00'],
dtype='datetime64[ns]', freq=None)
Floor to daily...
DatetimeIndex(['2021-09-29 00:00:00', '2021-09-29 00:00:00',
'2021-09-29 00:00:00'],
dtype='datetime64[ns]', freq=None)
Conclusion
Use DateTimeIndex.floor('H') to round down datetime values to the nearest hour boundary. This is useful for time series analysis and data aggregation operations.
Advertisements
