如何更改假期模块中观察到的假期?

How to change observed holidays in holidays module?

我是这个 python 假期图书馆的新手,所以对于我可能会问的任何琐碎问题,我提前表示歉意。

我的主要问题是我不确定如何在节假日修改 'observed' 值。只有在星期天,我才需要在星期一放假。我不需要周六放假。

文档似乎只有在我想完全而不是部分删除 'observed' 假期时才有解决方案。我的解决方法是创建一个 NewCountrysHolidays class 并用我想要的假期和观察到的状态填充它。我在下面附上了新 class 的代码。

我的新问题是当我执行 1-1-2015 in us_electric_holidays 时收到错误代码:'datetime.date' object is not callable,但它确实适用于我的数据的时间戳。当我使用我的数据时,我收到一个新错误 name 'SUN' is not defined。它似乎不承认 Weekdays 前。太阳和几个月前。十二月。我不知道为什么。

如果这太冗长,我深表歉意,但我很感激并感谢您对此问题的任何帮助。

问题背景:(如有兴趣) 我是一名电气工程 Ph.D。电网专业的学生。我的任务之一是分析电力公司的数据。电力公司将假期定义为:

Holidays are New Year’s Day, President’s Day, Memorial Day, Independence Day, Labor Day, Veterans Day, Thanksgiving Day, and Christmas. When any holiday falls on a Sunday, the following Monday will be recognized as a holiday. However, no change will be made for holidays falling on a Saturday.

为了正确标记所有数据,我需要删除马丁路德金纪念日和哥伦布纪念日。然后从星期五假期中删除观察到的星期六。

我在下面的代码中实现了所有这些,但它不会运行。

假期代码class(很长,但 Whosebug 不允许我附加文件):

import holidays
from datetime import date
from dateutil.relativedelta import relativedelta as rd


 class UsElectricHolidays(holidays.HolidayBase):
     def _populate(self, year):
         # New Year's Day
        if year > 1870:
            name = "New Year's Day"
            self[date(year, JAN, 1)] = name
            if self.observed and date(year, JAN, 1).weekday() == SUN:
                self[date(year, JAN, 1) + rd(days=+1)] = name + \
                    " (Observed)"

        # Washington's Birthday
        name = "Washington's Birthday"
        if year > 1970:
            self[date(year, FEB, 1) + rd(weekday=MO(+3))] = name
        elif year >= 1879:
            self[date(year, FEB, 22)] = name

        # Memorial Day
        if year > 1970:
            self[date(year, MAY, 31) + rd(weekday=MO(-1))] = "Memorial Day"
        elif year >= 1888:
            self[date(year, MAY, 30)] = "Memorial Day"

        # Independence Day
        if year > 1870:
            name = "Independence Day"
            self[date(year, JUL, 4)] = name
            if self.observed and date(year, JUL, 4).weekday() == SUN:
                self[date(year, JUL, 4) + rd(days=+1)] = name + " (Observed)"

        # Labor Day
        if year >= 1894:
            self[date(year, SEP, 1) + rd(weekday=MO)] = "Labor Day"

        # Veterans Day
        if year > 1953:
            name = "Veterans Day"
        else:
            name = "Armistice Day"
        if 1978 > year > 1970:
            self[date(year, OCT, 1) + rd(weekday=MO(+4))] = name
        elif year >= 1938:
            self[date(year, NOV, 11)] = name
            if self.observed \
                    and date(year, NOV, 11).weekday() == SUN:
                self[date(year, NOV, 11) + rd(days=+1)] = name + \
                    " (Observed)"

        # Thanksgiving
        if year > 1870:
            self[date(year, NOV, 1) + rd(weekday=TH(+4))] = "Thanksgiving"

        # Christmas Day
        if year > 1870:
            name = "Christmas Day"
            self[date(year, DEC, 25)] = "Christmas Day"
            if self.observed \
                    and date(year, DEC, 25).weekday() == SUN:
                self[date(year, DEC, 25) + rd(days=+1)] = name + \
                    " (Observed)"

 us_electric_holidays = UsElectricHolidays()

我正在处理同样的问题(电力的 NERC 假期)并看到了你的问题。 到一个相关问题让我找到了一个更快的解决方案:只需将 nearest_workday 替换为 sunday_to_monday 就可以得到你需要的东西。

像这样:

class NERCHolidayCalendar(AbstractHolidayCalendar):
    rules = [
        Holiday('NewYearsDay', month=1, day=1, observance=sunday_to_monday),
        USMemorialDay,
        Holiday('USIndependenceDay', month=7, day=4, observance=sunday_to_monday),
        USLaborDay,
        USThanksgivingDay,
        Holiday('Christmas', month=12, day=25, observance=sunday_to_monday)
    ]