Python Pandas - Indicate whether the date in DateTimeIndex is the last day of the year

To check whether dates in a DateTimeIndex fall on the last day of the year (December 31st), use the DateTimeIndex.is_year_end property. This returns a boolean array indicating which dates are year-end dates.

Syntax

DateTimeIndex.is_year_end

Creating a DateTimeIndex

First, let's create a DateTimeIndex with dates around year-end ?

import pandas as pd

# Create DateTimeIndex with period 6 and frequency as 2 days
datetimeindex = pd.date_range('2021-12-25 02:30:50', periods=6, tz='Australia/Adelaide', freq='2D')

print("DateTimeIndex...")
print(datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-12-25 02:30:50+10:30', '2021-12-27 02:30:50+10:30',
               '2021-12-29 02:30:50+10:30', '2021-12-31 02:30:50+10:30',
               '2022-01-02 02:30:50+10:30', '2022-01-04 02:30:50+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='2D')

Using is_year_end Property

The is_year_end property returns True for December 31st dates and False for all other dates ?

import pandas as pd

# Create DateTimeIndex spanning year-end
datetimeindex = pd.date_range('2021-12-25 02:30:50', periods=6, tz='Australia/Adelaide', freq='2D')

# Check which dates are the last day of the year
print("DateTimeIndex...")
print(datetimeindex)
print()

print("DateTimeIndex frequency...")
print(datetimeindex.freq)
print()

print("Check whether the date in DateTimeIndex is the last day of the year...")
print(datetimeindex.is_year_end)
DateTimeIndex...
DatetimeIndex(['2021-12-25 02:30:50+10:30', '2021-12-27 02:30:50+10:30',
               '2021-12-29 02:30:50+10:30', '2021-12-31 02:30:50+10:30',
               '2022-01-02 02:30:50+10:30', '2022-01-04 02:30:50+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='2D')

DateTimeIndex frequency...
<2 * Days>

Check whether the date in DateTimeIndex is the last day of the year...
[False False False  True False False]

Filtering Year-End Dates

You can use the boolean array to filter only the year-end dates ?

import pandas as pd

datetimeindex = pd.date_range('2021-12-25', periods=6, freq='2D')

# Get only year-end dates
year_end_dates = datetimeindex[datetimeindex.is_year_end]
print("Year-end dates:")
print(year_end_dates)
Year-end dates:
DatetimeIndex(['2021-12-31'], dtype='datetime64[ns]', freq=None)

Conclusion

The is_year_end property efficiently identifies December 31st dates in a DateTimeIndex. It returns a boolean array that can be used for filtering or conditional operations on year-end dates.

Updated on: 2026-03-26T17:12:04+05:30

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements