[Python] Pandas is_year_end

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página