Pandas currently allows you to add business days to a given date datetime.today() + 3*BDay()
. I would like to extend the idea of a business day to exclude a given DateIndex of holidays as well as weekends. Is that possible incorporate a DateIndex into an offset?
The CustomBusinessDay class has now been merged into the upcoming 0.12 release of Pandas where you will be able to do something like the following:
>>> from pandas.tseries.offsets import CustomBusinessDay
>>>
>>> # As an interesting example, let's look at Egypt where
>>> # a Friday-Saturday weekend is observed.
>>> weekmask_egypt = 'Sun Mon Tue Wed Thu'
>>>
>>> # They also observe International Workers' Day so let's
>>> # add that as a holiday for a couple of years
>>> holidays = ['2012-05-01', datetime(2013, 5, 1), np.datetime64('2014-05-01')]
>>>
>>> bday_egypt = CustomBusinessDay(holidays=holidays, weekmask=weekmask_egypt)
>>> dt = datetime(2013, 4, 30)
>>> print dt + 2 * bday_egypt
2013-05-05 00:00:00
>>>
>>> dts = date_range(dt, periods=5, freq=bday_egypt).to_series()
>>> print dts
2013-04-30 2013-04-30 00:00:00
2013-05-02 2013-05-02 00:00:00
2013-05-05 2013-05-05 00:00:00
2013-05-06 2013-05-06 00:00:00
2013-05-07 2013-05-07 00:00:00
Freq: C, dtype: datetime64[ns]
>>>
>>> print Series(dts.weekday, dts).map(Series('Mon Tue Wed Thu Fri Sat Sun'.split()))
2013-04-30 Tue
2013-05-02 Thu
2013-05-05 Sun
2013-05-06 Mon
2013-05-07 Tue
dtype: object
HTH
Currently I think you need to create a custom subclass. You'd need to override the apply
and onOffset
methods to take into account your holiday calendar.
We should add an optional holiday calendar parameter in the business-X frequencies eventually though. I made a github issue to keep track of it: https://github.com/pydata/pandas/issues/2301
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With