Python Pandas - Get the year from the PeriodIndex object

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

What is PeriodIndex?

A PeriodIndex is an immutable array holding ordinal values that represent regular periods in time. Each period has a specific frequency like daily, monthly, or minutely intervals.

Creating a PeriodIndex Object

First, let's create a PeriodIndex with datetime strings and set the frequency using the "freq" parameter ?

import pandas as pd

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

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

Extracting Years from PeriodIndex

Use the .year property to extract the year component from each period ?

import pandas as pd

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

# Extract years from the PeriodIndex
years = periodIndex.year
print("Years from PeriodIndex:")
print(years)
print("Type:", type(years))
Years from PeriodIndex:
Index([2021, 2019, 2021, 2022], dtype='int64')
Type: <class 'pandas.core.indexes.base.Index'>

Complete Example

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

import pandas as pd

# Create a PeriodIndex object
periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
                             '2021-07-15 02:55:15', '2022-06-25 09:40: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 years
print("\nYears from the PeriodIndex object...")
print(periodIndex.year)
PeriodIndex...
PeriodIndex(['2021-09-25 07:30', '2019-10-30 04:15', '2021-07-15 02:55', '2022-06-25 09:40'], dtype='period[T]')

PeriodIndex frequency object...
<Minute>

PeriodIndex frequency as string...
T

Years from the PeriodIndex object...
Index([2021, 2019, 2021, 2022], dtype='int64')

Key Points

  • The .year property returns an Index object containing integer years
  • The frequency ("T" for minute) doesn't affect year extraction
  • Works with any datetime format that pandas can parse
  • Returns the same number of years as periods in the original index

Conclusion

The PeriodIndex.year property provides a simple way to extract year components from period data. This is useful for time-based analysis and grouping operations in pandas.

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

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements