Python Pandas - Get the hour of the period from the PeriodIndex object

To get the hour of the period from the PeriodIndex object, use the PeriodIndex.hour property. This property extracts the hour component from each period in the index.

What is PeriodIndex?

PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time. It represents a specific time period with a defined frequency.

Syntax

PeriodIndex.hour

Creating a PeriodIndex Object

First, let's create a PeriodIndex object with datetime strings and frequency set to minutes ?

import pandas as pd

# Create a PeriodIndex object with minute frequency
periodIndex = pd.PeriodIndex(['2021-09-25 07:50:35', '2019-10-30 04:35:45',
                              '2021-07-15 02:25:15', '2022-06-25 09:20:55'], 
                              freq="T")

print("PeriodIndex...")
print(periodIndex)
PeriodIndex...
PeriodIndex(['2021-09-25 07:50', '2019-10-30 04:35', '2021-07-15 02:25', '2022-06-25 09:20'], dtype='period[T]')

Extracting Hour Component

Now let's extract the hour from each period using the hour property ?

import pandas as pd

# Create a PeriodIndex object
periodIndex = pd.PeriodIndex(['2021-09-25 07:50:35', '2019-10-30 04:35:45',
                              '2021-07-15 02:25:15', '2022-06-25 09:20:55'], 
                              freq="T")

# Extract hour from the PeriodIndex object
hours = periodIndex.hour
print("Hours from PeriodIndex:")
print(hours)
print(f"\nData type: {type(hours)}")
Hours from PeriodIndex:
Index([7, 4, 2, 9], dtype='int64')

Data type: <class 'pandas.core.indexes.base.Index'>

Complete Example

Here's a comprehensive example showing PeriodIndex creation and hour extraction ?

import pandas as pd

# Create a PeriodIndex object
periodIndex = pd.PeriodIndex(['2021-09-25 07:50:35', '2019-10-30 04:35:45',
                              '2021-07-15 02:25:15', '2022-06-25 09:20:55'], 
                              freq="T")

# Display PeriodIndex object
print("PeriodIndex...")
print(periodIndex)

# Display PeriodIndex frequency
print("\nPeriodIndex frequency object...")
print(periodIndex.freq)

# Display PeriodIndex frequency as string
print("\nPeriodIndex frequency as string...")
print(periodIndex.freqstr)

# Extract and display hours
print("\nHours from PeriodIndex...")
print(periodIndex.hour)
PeriodIndex...
PeriodIndex(['2021-09-25 07:50', '2019-10-30 04:35', '2021-07-15 02:25', '2022-06-25 09:20'], dtype='period[T]')

PeriodIndex frequency object...
<Minute>

PeriodIndex frequency as string...
T

Hours from PeriodIndex...
Index([7, 4, 2, 9], dtype='int64')

Key Points

  • The hour property returns an Index containing hour values (0-23)
  • Works with any PeriodIndex that has time components
  • Returns integer values representing hours in 24-hour format
  • Useful for time-based analysis and filtering operations

Conclusion

The PeriodIndex.hour property provides an efficient way to extract hour components from period data. This is particularly useful for time series analysis and data filtering based on specific hours of the day.

Updated on: 2026-03-26T18:01:26+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements