rsheftel / pandas_market_calendars

Exchange calendars to use with pandas for trading applications
MIT License
776 stars 172 forks source link

importing this package alters the behavior of pandas #301

Open erikhansenwong opened 10 months ago

erikhansenwong commented 10 months ago

This package modifies the behavior of core pandas features here in pandas_market_calendars/calendars/nyse.py

# Overwrite the default holiday calendar start_date of 1/1/70 
AbstractHolidayCalendar.start_date = '1885-01-01'

There are several other ways this can be handled without changing the behavior of classes in a third party library.

Here is an example of a test which should pass, but which currently fails with v4.3.1

def test_custom_business_day():
    import pandas as pd
    from pandas.tseries.holiday import MO, AbstractHolidayCalendar, Holiday
    from pandas.tseries.offsets import CustomBusinessDay

    USMemorialDay = Holiday(
        "Memorial Day", month=5, day=31, offset=pd.DateOffset(weekday=MO(-1))
    )

    class ExampleCalendar(AbstractHolidayCalendar):
        rules = [USMemorialDay]

    bday1 = CustomBusinessDay(calendar=ExampleCalendar())
    bday2 = CustomBusinessDay(calendar=ExampleCalendar())

    # this assertion passes
    assert bday1 == bday2

    # but then we import pandas_market_calendars and try the same thing ...
    import pandas_market_calendars

    bday3 = CustomBusinessDay(calendar=ExampleCalendar())

    # and now this assertion fails
    assert bday1 == bday3
rsheftel commented 10 months ago

Do you have specific suggestion on how to modify the code? Can you submit a PR?