Pandas目前允许您将工作日添加到指定日期datetime.today() + 3*BDay()
。我想扩展一个工作日的想法,以排除给定的假期和周末的DateIndex。是否可以将DateIndex合并到偏移量中?
答案 0 :(得分:5)
CustomBusinessDay类现已合并到即将发布的Pandas 0.12版本中,您可以执行以下操作:
>>> 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
答案 1 :(得分:1)
目前我认为您需要创建自定义子类。您需要覆盖apply
和onOffset
方法,以考虑您的假期日历。
我们最终应该在business-X频率中添加一个可选的假日日历参数。我提出了一个github问题来跟踪它:https://github.com/pydata/pandas/issues/2301