Create trading holiday calendar with Pandas

后端 未结 3 894
忘了有多久
忘了有多久 2020-12-04 08:38

I\'m trying to create a Trading calendar using Pandas. I\'m able to create a cal instance based on the USFederalHolidayCalendar. The USFederalHolidayCalendar is not consiste

3条回答
  •  囚心锁ツ
    2020-12-04 08:59

    You have to create new instance of class: cal1 = tradingCal(). This works for me.

    from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
    from datetime import datetime
    
    cal = get_calendar('USFederalHolidayCalendar')  # Create calendar instance
    cal.rules.pop(7)                                # Remove Veteran's Day rule
    cal.rules.pop(6)                                # Remove Columbus Day rule
    tradingCal = HolidayCalendarFactory('TradingCalendar', cal, GoodFriday)
    print tradingCal.rules
    
    #new instance of class
    cal1 = tradingCal()
    
    print cal1.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
    
    #DatetimeIndex(['2015-01-01', '2015-01-19', '2015-02-16', '2015-04-03',
    #               '2015-05-25', '2015-07-03', '2015-09-07', '2015-11-26',
    #               '2015-12-25', '2016-01-01', '2016-01-18', '2016-02-15',
    #              '2016-03-25', '2016-05-30', '2016-07-04', '2016-09-05',
    #               '2016-11-24', '2016-12-26'],
    #              dtype='datetime64[ns]', freq=None, tz=None)
    

提交回复
热议问题