2015-09-16 51 views
0

我有一個數據集,我必須根據講座日和非講座日進行分組。我能想到的最好的方法就是使用CustomBusinessDay並指定節假日(非講座日)。 (另一種選擇是使用bdate_range並從中刪除列表中的假日日期,但我沒有太多運氣。)如何爲Pandas中的CustomBusinessDays假期指定不同的日期範圍?

但是,當我通過我的假期列表CustomBusinessDay時,我得到以下錯誤:

TypeError: dt must be datestring, datetime or 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

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) 
相關問題