Pandas 美国大选日的假期日历规则
Pandas Holiday Calendar Rule for US Election day
在使用 Pandas 假期 class 创建假期日历时,我无法确定为美国选举日创建日历的正确规则。美国选举日被定义为 11 月第一个星期一之后的星期二,每偶数年出现一次。使用假期 class 定义:
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
returns
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 日历对象中删除奇数年?
您可以在制定假期时使用遵守而不是抵消,并在奇数年return 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
请注意,您不想在构造假日时同时使用偏移量和观察值,并且尝试这样做会在更新的 pandas 版本中引发异常。
在使用 Pandas 假期 class 创建假期日历时,我无法确定为美国选举日创建日历的正确规则。美国选举日被定义为 11 月第一个星期一之后的星期二,每偶数年出现一次。使用假期 class 定义:
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
returns
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 日历对象中删除奇数年?
您可以在制定假期时使用遵守而不是抵消,并在奇数年return 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
请注意,您不想在构造假日时同时使用偏移量和观察值,并且尝试这样做会在更新的 pandas 版本中引发异常。