我最近开始使用Pandas,发现CustomBusinessDay 和BusinessHour类对于执行日历数学非常有用,同时考虑了特定的业务规则。但是,我想知道是否有可能将它们结合起来计算尊重两个类的时间值。
例如,我想在开始时间增加n个工作小时,让它跳过"非工作时间"由BusinessHour课程定义,以及" off-days"在CustomBusinessDay类中定义。
对这个图书馆有更多经验的人是否知道这是否可以轻松完成,或者如果没有,建议如何将此功能封装在另一个类中?
答案 0 :(得分:2)
从版本0.18.1开始,您可以使用CustomBusinessHour
:
CustomBusinessHour是BusinessHour和 CustomBusinessDay允许您指定任意假期。对于 详情请参阅Custom Business Hour(GH11514)
In [1]: from pandas.tseries.offsets import CustomBusinessHour In [2]: from pandas.tseries.holiday import USFederalHolidayCalendar In [3]: bhour_us = CustomBusinessHour(calendar=USFederalHolidayCalendar()) Friday before MLK Day In [4]: dt = datetime(2014, 1, 17, 15) In [5]: dt + bhour_us Out[5]: Timestamp('2014-01-17 16:00:00') Tuesday after MLK Day (Monday is skipped because it’s a holiday) In [6]: dt + bhour_us * 2 Out[6]: Timestamp('2014-01-21 09:00:00')
我正在使用的一个例子是
from pandas.tseries.offsets import CustomBusinessHour
from pandas.tseries.holiday import Holiday, AbstractHolidayCalendar
class MyCalendar(AbstractHolidayCalendar):
rules = [Holiday('my birthday', month=6, day=6)]
cbh = CustomBusinessHour(2, start='10:00', end='16:00', calendar=MyCalendar())
pd.date_range('20170602', periods=20, freq=cbh)
Out:
DatetimeIndex(['2017-06-02 10:00:00', '2017-06-02 12:00:00',
'2017-06-02 14:00:00', '2017-06-05 10:00:00',
'2017-06-05 12:00:00', '2017-06-05 14:00:00',
'2017-06-07 10:00:00', '2017-06-07 12:00:00',
'2017-06-07 14:00:00', '2017-06-08 10:00:00',
'2017-06-08 12:00:00', '2017-06-08 14:00:00',
'2017-06-09 10:00:00', '2017-06-09 12:00:00',
'2017-06-09 14:00:00', '2017-06-12 10:00:00',
'2017-06-12 12:00:00'],
dtype='datetime64[ns]', freq='2CBH')