如何为 Pandas 中的 CustomBusinessDay 假期指定不同的日期范围?
How do I specify different date ranges for CustomBusinessDays holidays in Pandas?
我有一个数据集,我必须根据上课日和非上课日进行分组。我能想到的最好的方法是使用 CustomBusinessDay
并指定假期(非授课日)。 (另一种选择是使用 bdate_range
并从中删除列表假期日期,但我也没有太多运气这样做。)
但是,当我在 CustomBusinessDay
中传递假期列表时,出现以下错误:
TypeError: dt must be datestring, datetime or datetime64
我似乎无法为 holidays
使用 DatetimeIndex
个对象的列表。有更好的方法吗?我的代码如下:
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)
nolectures
是日期时间索引和字符串的异构列表:
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)
我有一个数据集,我必须根据上课日和非上课日进行分组。我能想到的最好的方法是使用 CustomBusinessDay
并指定假期(非授课日)。 (另一种选择是使用 bdate_range
并从中删除列表假期日期,但我也没有太多运气这样做。)
但是,当我在 CustomBusinessDay
中传递假期列表时,出现以下错误:
TypeError: dt must be datestring, datetime or datetime64
我似乎无法为 holidays
使用 DatetimeIndex
个对象的列表。有更好的方法吗?我的代码如下:
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)
nolectures
是日期时间索引和字符串的异构列表:
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)