gpt4 book ai didi

pandas - 带有假日日历的 Pandas 中的 DateOffset

转载 作者:行者123 更新时间:2023-12-04 04:30:11 25 4
gpt4 key购买 nike

Pandas 目前允许您将工作日添加到给定日期 datetime.today() + 3*BDay() .我想扩展工作日的想法,以排除给定的假日和周末 DateIndex。是否可以将 DateIndex 合并到偏移量中?

最佳答案

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

关于pandas - 带有假日日历的 Pandas 中的 DateOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13462802/

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