在熊猫中创造新的假期

时间:2018-02-21 17:00:42

标签: pandas

我使用以下代码创建个性化假日日历:

class USTradingCalendar(AbstractHolidayCalendar):
    rules = [
        Holiday('NewYearsDay', month=1, day=1, observance=nearest_workday),
        USMartinLutherKingJr,
        USPresidentsDay,
        GoodFriday,
        USMemorialDay,
        Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
        USLaborDay,
        USThanksgivingDay,
        Holiday('Christmas', month=12, day=25, observance=nearest_workday)
    ]

我需要将USThanksgivingDayDayAfter作为新假期添加到上述日历中。 USThanksgivingDayDayAfter将是纪念感恩节后的第二天(星期五)。

我不确定如何将USThanksgivingDayDayAfter添加到上面的课程中。

1 个答案:

答案 0 :(得分:1)

这很难弄明白。一些功劳归于this answer

首先,看起来如何定义USThanksgivingDay一样简单。

USThanksgivingDay = Holiday('Thanksgiving', month=11, day=1,
                            offset=DateOffset(weekday=TH(4)))

TH(4)部分代表第四个星期四,并从dateutil.relativedelta导入。您可能认为只需导入FR(星期五)并使用相同的偏移规则。这不会起作用,因为如果11月的第四个星期四在本周末结束,则抵消将在下周五创造一周的时间间隔。

解决方案是使用您自己定义的自定义遵守规则。见下面的例子:

from pandas.tseries.offsets import DateOffset
from pandas.tseries import holiday
from dateutil.relativedelta import TH
import pandas as pd

def custom_observance_rule(dt):
    """ Adds one day to Thanksgiving date """
    dt = dt + pd.offsets.DateOffset(weekday=TH(4))
    dt = dt + pd.Timedelta(days=1)
    return dt


class ExampleCalendar(holiday.AbstractHolidayCalendar):
    """ Example Calendar """
    rules = [
        holiday.USThanksgivingDay,
        # fourth Thursday of November + 1 day 
        holiday.Holiday(
            'USThanksgivingDayDayAfter',
            month=11, day=1,
            observance=custom_observance_rule
            )
        ]

cal = ExampleCalendar()
print(cal.holidays(return_name=True).sort_index())

打印:

1970-11-26                 Thanksgiving
1970-11-27    USThanksgivingDayDayAfter
1971-11-25                 Thanksgiving
1971-11-26    USThanksgivingDayDayAfter
1972-11-23                 Thanksgiving
1972-11-24    USThanksgivingDayDayAfter
1973-11-22                 Thanksgiving
1973-11-23    USThanksgivingDayDayAfter
1974-11-28                 Thanksgiving
1974-11-29    USThanksgivingDayDayAfter
1975-11-27                 Thanksgiving
1975-11-28    USThanksgivingDayDayAfter
1976-11-25                 Thanksgiving
1976-11-26    USThanksgivingDayDayAfter
1977-11-24                 Thanksgiving
1977-11-25    USThanksgivingDayDayAfter
1978-11-23                 Thanksgiving
1978-11-24    USThanksgivingDayDayAfter
1979-11-22                 Thanksgiving
1979-11-23    USThanksgivingDayDayAfter
1980-11-27                 Thanksgiving
1980-11-28    USThanksgivingDayDayAfter
1981-11-26                 Thanksgiving
1981-11-27    USThanksgivingDayDayAfter
1982-11-25                 Thanksgiving
1982-11-26    USThanksgivingDayDayAfter
1983-11-24                 Thanksgiving
1983-11-25    USThanksgivingDayDayAfter
1984-11-22                 Thanksgiving
1984-11-23    USThanksgivingDayDayAfter
                        ...            
2016-11-24                 Thanksgiving
2016-11-25    USThanksgivingDayDayAfter
2017-11-23                 Thanksgiving
2017-11-24    USThanksgivingDayDayAfter
2018-11-22                 Thanksgiving
2018-11-23    USThanksgivingDayDayAfter
2019-11-28                 Thanksgiving
2019-11-29    USThanksgivingDayDayAfter
2020-11-26                 Thanksgiving
2020-11-27    USThanksgivingDayDayAfter
2021-11-25                 Thanksgiving
2021-11-26    USThanksgivingDayDayAfter
2022-11-24                 Thanksgiving
2022-11-25    USThanksgivingDayDayAfter
2023-11-23                 Thanksgiving
2023-11-24    USThanksgivingDayDayAfter
2024-11-28                 Thanksgiving
2024-11-29    USThanksgivingDayDayAfter
2025-11-27                 Thanksgiving
2025-11-28    USThanksgivingDayDayAfter
2026-11-26                 Thanksgiving
2026-11-27    USThanksgivingDayDayAfter
2027-11-25                 Thanksgiving
2027-11-26    USThanksgivingDayDayAfter
2028-11-23                 Thanksgiving
2028-11-24    USThanksgivingDayDayAfter
2029-11-22                 Thanksgiving
2029-11-23    USThanksgivingDayDayAfter
2030-11-28                 Thanksgiving
2030-11-29    USThanksgivingDayDayAfter
Length: 122, dtype: object