Python Pandas - Extract year from the DateTimeIndex with specific time series frequency

To extract years from a DateTimeIndex with specific time series frequency, use the DateTimeIndex.year property. This is particularly useful when working with time series data that has yearly frequency patterns.

Syntax

DateTimeIndex.year

Creating DateTimeIndex with Yearly Frequency

First, let's create a DateTimeIndex with yearly frequency and timezone ?

import pandas as pd

# DatetimeIndex with period 6 and frequency as Y i.e. years
# timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='Y')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-12-31 02:35:55+11:00', '2022-12-31 02:35:55+11:00',
               '2023-12-31 02:35:55+11:00', '2024-12-31 02:35:55+11:00',
               '2025-12-31 02:35:55+11:00', '2026-12-31 02:35:55+11:00'],
               dtype='datetime64[ns, Australia/Sydney]', freq='A-DEC')

Extracting Years

Use the year property to extract year values from the DateTimeIndex ?

import pandas as pd

# DatetimeIndex with period 6 and frequency as Y i.e. years
# timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='Y')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)

# get the year
print("\nGetting the year values..\n", datetimeindex.year)
DateTimeIndex...
DatetimeIndex(['2021-12-31 02:35:55+11:00', '2022-12-31 02:35:55+11:00',
               '2023-12-31 02:35:55+11:00', '2024-12-31 02:35:55+11:00',
               '2025-12-31 02:35:55+11:00', '2026-12-31 02:35:55+11:00'],
               dtype='datetime64[ns, Australia/Sydney]', freq='A-DEC')
DateTimeIndex frequency...
   <YearEnd: month=12>

Getting the year values..
   Int64Index([2021, 2022, 2023, 2024, 2025, 2026], dtype='int64')

Key Points

  • The year property returns an Int64Index containing year values
  • Works with any DateTimeIndex regardless of frequency or timezone
  • Year-end frequency ('Y') creates dates at December 31st of each year
  • Timezone information is preserved in the original DateTimeIndex but not in the extracted years

Conclusion

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

---
Updated on: 2026-03-26T17:25:55+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements