Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas Calendar issue

This code for CustomBusinessDay() works fine:

from datetime import datetime
from pandas.tseries.offsets import CustomBusinessDay

runday = datetime(2021,12,30).date()
nextday = (runday + CustomBusinessDay()).date()

output 1:

In [26]: nextday
Out[26]: datetime.date(2021, 12, 31)

However, when adding an optional calendar as in date functionality , it produces the next business day even though today’s date (Dec 31, 2021) is not a holiday according to a specified calendar below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, \
    USMemorialDay,  USMartinLutherKingJr, USPresidentsDay, GoodFriday, \
    USLaborDay, USThanksgivingDay, nearest_workday


class NYSECalendar(AbstractHolidayCalendar):
    ''' NYSE holiday calendar via pandas '''
    rules = [
        Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
        USMartinLutherKingJr,
        USPresidentsDay,
        GoodFriday,
        USMemorialDay,
        Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
        USLaborDay,
        USThanksgivingDay,
        Holiday('Christmas', month=12, day=25, observance=nearest_workday),
        ]
nextday = (runday + CustomBusinessDay(calendar=NYSECalendar())).date()  

output2:

In [27]: nextday
Out[27]: datetime.date(2022, 1, 3)

>Solution :

This line is the location of your problem:

Holiday('New Years Day', month=1, day=1, observance=nearest_workday)

If you take a look at the source code, nearest_workday means that the holiday is observed on a Friday if it falls on a Saturday, and on a Monday if the holiday falls on a Sunday. Since New Year’s Day 2022 falls on a Saturday, it is observed today (12/31/2021) according to your calendar.

Removing the observance parameter will lead to an output of 2021-12-31.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading