如何在Pandas中为CustomBusinessDays假期指定不同的日期范围?

时间:2015-09-16 12:54:00

标签: python pandas datetimeoffset

我有一个数据集,我必须根据讲座日和非讲座日分组。我能想到的最佳方法是使用CustomBusinessDay并指定假期(非讲座日)。 (另一种选择是使用bdate_range并从中删除列表假日日期,但我也没有这么多运气。)

但是,当我在CustomBusinessDay中传递假期列表时,出现以下错误:

  

TypeError:dt必须是datestring,datetime或datetime64

似乎我无法为DatetimeIndex使用holidays个对象列表。有没有更好的方法来做到这一点?我的代码如下:

import pandas as pd
from pandas.tseries.offsets import CustomBusinessDay
nolectures=[pd.date_range(start='2015-03-28',end='2015-04-12'),  '2015-05-01', pd.date_range(start='2015-11-09',end='2015-12-31'),]
calendar=CustomBusinessDay(holidays=nolectures)

1 个答案:

答案 0 :(得分:0)

nolectures是DatetimeIndexes和字符串的异构列表:

nolectures=[pd.date_range(start='2015-03-28',end='2015-04-12'),  '2015-05-01', pd.date_range(start='2015-11-09',end='2015-12-31'),]

您需要nolectures作为日期的数组:

import pandas as pd
from pandas.tseries.offsets import CustomBusinessDay

nolectures = pd.date_range(start='2015-03-28',end='2015-04-12').union_many(
    [['2015-05-01'], pd.date_range(start='2015-11-09',end='2015-12-31')])
calendar = CustomBusinessDay(holidays=nolectures)
print(calendar.holidays)