Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateOffset in Pandas with a Holiday Calendar

Tags:

pandas

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?

like image 385
rhaskett Avatar asked Nov 19 '12 21:11

rhaskett


2 Answers

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

like image 134
snth Avatar answered Nov 10 '22 02:11

snth


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

like image 36
Chang She Avatar answered Nov 10 '22 04:11

Chang She