gpt4 book ai didi

python - 在 Pandas 中结合 CustomBusinessDay 和 BusinessHour 类

转载 作者:行者123 更新时间:2023-11-28 17:32:20 24 4
gpt4 key购买 nike

我最近开始使用 Pandas 并找到了 CustomBusinessDay BusinessHour类对于在处理特定业务规则时进行日历数学运算非常有用。但是,我想知道是否可以将它们结合起来计算同时满足这两个类的时间增量。

例如,我想将 n 个营业时间添加到开始时间,并让它跳过 BusinessHour 类定义的“下类时间”以及 CustomBusinessDay 中定义的“下类时间”中的所有时间类。

对这个库有更多经验的人是否知道这是否可以轻松完成,或者,如果不能,对如何将此功能封装在另一个类中有建议吗?

最佳答案

从0.18.1版本开始,可以使用CustomBusinessHour :

The CustomBusinessHour is a mixture of BusinessHour and CustomBusinessDay which allows you to specify arbitrary holidays. For details, see 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')

关于python - 在 Pandas 中结合 CustomBusinessDay 和 BusinessHour 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33502224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com