我有一个数据集,我必须根据讲座日和非讲座日分组。我能想到的最佳方法是使用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)
答案 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)