在使用Pandas Holiday类创建假日日历时,我无法确定为美国选举日创建日历的正确规则。美国选举日定义为11月第一个星期一之后的星期二,每个偶数年份都会发生。使用定义的Holiday类:
class USElectionCalendar(AbstractHolidayCalendar):
"""
Federal Presidential and Congressional election day.
Tuesday following the first Monday, 2 to 8 November every two even numbered years.
Election Days can only occur from November 2nd through 8th inclusive.
"""
rules = [
Holiday("Election Day",month=11, day=2, offset=pd.DateOffset(weekday=TU(1))),
]
与
start_date = '20160108'
end_date = '20261231'
进入功能
def holidays_between_dates(calendar, start_date, end_date):
cal = calendar
dates = cal.holidays(start_date, end_date, return_name=True)
return dates
返回
2016-11-08 Election Day
2017-11-07 Election Day
2018-11-06 Election Day
2019-11-05 Election Day
2020-11-03 Election Day
2021-11-02 Election Day
2022-11-08 Election Day
2023-11-07 Election Day
2024-11-05 Election Day
2025-11-04 Election Day
2026-11-03 Election Day
除了奇数年以外,一切都很好。我已尝试合并两个偏移量,如issue中所述。将2年偏移量添加到规则
Holiday("Election Day", month=11, day=2, offset=[ pd.DateOffset(weekday=TU(1)), pd.DateOffset(years=2) ])
简单地将第一次出现移动到未来2年。我不确定所需的时间序列是否可行。所以对于这个问题:
是否可以直接构建此日历,还是需要使用第二个函数从Pandas日历对象中删除奇数年份?
答案 0 :(得分:2)
在制作假日时可以使用observance而不是offset,并在奇数年时返回None:
def election_observance(dt):
if dt.year % 2 == 1:
return None
else:
return dt + pd.DateOffset(weekday=TU(1))
class USElectionCalendar(AbstractHolidayCalendar):
"""
Federal Presidential and Congressional election day.
Tuesday following the first Monday, 2 to 8 November every two even numbered years.
Election Days can only occur from November 2nd through 8th inclusive.
"""
rules = [
Holiday('Election Day', month=11, day=2, observance=election_observance)
]
cal = USElectionCalendar()
start_date = '20160108'
end_date = '20261231'
print cal.holidays(start_date, end_date, return_name=True)
输出:
2016-11-08 Election Day
2018-11-06 Election Day
2020-11-03 Election Day
2022-11-08 Election Day
2024-11-05 Election Day
2026-11-03 Election Day
dtype: object
请注意,您在构建Holiday时不希望使用BOTH偏移和遵守,并且尝试这样做会在最近的pandas版本中引发异常。