pythonpandas

Pandas is_year_end


I am using Python 3.11.3 on Windows and Pandas 2.0.0.

I have this code with

from datetime import datetime
import pandas as pd

pd.__version__
'2.0.0'

rng = pd.date_range(start='2017-01-01', end='2018-01-01', freq='B')

rng[-10:]

DatetimeIndex(['2017-12-19', '2017-12-20', '2017-12-21', '2017-12-22',
               '2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28',
               '2017-12-29', '2018-01-01'],
              dtype='datetime64[ns]', freq='B')

rng[-10:].is_year_end

array([False, False, False, False, False, False, False, False,  True,
       False])

rng[rng.is_year_end]

DatetimeIndex(['2017-12-29'], dtype='datetime64[ns]', freq='B')

While it is true that '2017-12-29' was the last business day of the year, it was not the last day of the year.

However,

rng2 = pd.date_range(start='2017-01-01', end='2018-01-01')

rng2[-10:]

DatetimeIndex(['2017-12-23', '2017-12-24', '2017-12-25', '2017-12-26',
               '2017-12-27', '2017-12-28', '2017-12-29', '2017-12-30',
               '2017-12-31', '2018-01-01'],
              dtype='datetime64[ns]', freq='D')

rng2[-10:].is_year_end

array([False, False, False, False, False, False, False, False,  True,
       False])

rng2[rng2.is_year_end]

DatetimeIndex(['2017-12-31'], dtype='datetime64[ns]', freq='D')

Which makes more sense to me.

The documentation on pandas.DatetimeIndex.is_year_end or pandas.Series.dt.is_year_end does not specify anything about business days or any other constraint.


Solution

  • According to the documentation,

    is_year_end Logical indicating if last day of year (defined by frequency)

    With the help of other users in the community, I have understood that "defined by frequency" means that, for example, if the series contains only business days, the accessor .is_year_end is not going to test if each date in the series is 12/31, but it will test if each date in the series is the last business day of the year.