Initial implementation of holiday and holiday calendar. by rockg · Pull Request #6719 · pandas-dev/pandas (original) (raw)
Implementation of holidays based on DateOffset objects and calendars that are collections of those holidays. These calendars can be passed into CustomBusinessDay. Also, add an Easter offset for use in holiday calendars.
Currently the default dates are hard-coded in the calendar class but this should move somewhere else and be calculated differently. I'm open to suggestions here. Also, the 4-day weekend observance needs to be incorporated (for example, if July 4th is on a Tuesday, perhaps Monday is also a holiday). This would require a little bit of tweaking, but not much.
from pandas.tseries.holiday import Holiday, USMemorialDay,\
AbstractHolidayCalendar, Nearest, MO, USFederalHolidayCalendar
from pandas.tseries.offsets import DateOffset, CustomBusinessDay
from datetime import datetime
class ExampleCalendar(AbstractHolidayCalendar):
_rule_table = [
USMemorialDay,
Holiday('July 4th', month=7, day=4, observance=Nearest),
Holiday('Columbus Day', month=10, day=1,
offset=DateOffset(weekday=MO(2))),
]
cal = ExampleCalendar()
cal.holidays()
print(datetime(2012, 5, 25) + CustomBusinessDay(calendar=cal))
print(datetime(2012, 5, 25) + CustomBusinessDay(calendar=USFederalHolidayCalendar()))