熊猫CustomBusinessDay抵消仅在某些假期有效

时间:2019-04-16 19:23:24

标签: python pandas datetime time-series offset

我正在尝试根据略微定制的日历来计算未来的第5个工作日。但是,“劳动节”(9月的美国假期)的值计算错误,而正确计算了1月和7月(可能受到假期影响的其他月份)的月份。

我已经尝试使用dts + 5 * custom_bday-但这只是将问题从9月的值不正确转移到1月和7月的值减少一天。

import pandas as pd
from datetime import datetime

start = datetime(2019,1,1)
end = datetime(2024,8,1)
dtrange = pd.date_range(start, end, freq='MS')
dts = pd.Series(dtrange)

hols = holidays.UnitedStates(state='MD', years=[2019,2020,2021,2022,2023,2024])
notholidays = ['American Indian Heritage Day', 'Columbus Day', 'Veterans Day']
realhols = [i for i in sorted(hols.items()) if i[1] not in notholidays]
realholsdt = [i[0] for i in realhols]

custom_bday = pd.offsets.CustomBusinessDay(holidays=realholsdt)

dts + 4 * custom_bday

预期结果是每月第5个工作日的系列,不包括周末和节假日。但是,例如,所有的9月劳动节假期都关闭了

目睹:8 2019-09-06

期望:8 2019-09-09

1 个答案:

答案 0 :(得分:0)

您可以通过CustomBusinessMonthBegin获得每月的第一个工作日,同时占CustomUSHolidayCalendar。在您的自定义日历中,请确保您正确地抵消了LaborDay(如果需要的话),并添加/删除了任何假期。通过将其添加到date_range参数中,在freq中使用它。

import pandas as pd
from datetime import datetime, timedelta

from pandas.tseries.offsets import CustomBusinessMonthBegin, BDay
from pandas.tseries.holiday import (
    AbstractHolidayCalendar, DateOffset,
    Holiday, MO, nearest_workday,
    USLaborDay, USMemorialDay, USColumbusDay, USThanksgivingDay,
    USPresidentsDay, EasterMonday, USMartinLutherKingJr
)

class CustomUSHolidayCalendar(AbstractHolidayCalendar):
    rules = [
        Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
        USMartinLutherKingJr,
        USPresidentsDay,
        USMemorialDay,
        Holiday('July 4th', month=7, day=4, observance=nearest_workday),

        # offset labor day
        Holiday('Labor Day', month=9, day=5, offset=DateOffset(weekday=MO(1))),

        # Holiday('Veterans Day', month=11, day=11, observance=nearest_workday),
        USThanksgivingDay,
        Holiday('Christmas', month=12, day=25, observance=nearest_workday),
        Holiday('Columbus Day', month=10, day=1, offset = pd.DateOffset(weekday=MO(2))),
    ]

start_date = datetime(2019, 1, 1)
end_date = datetime(2024, 8, 1)

usholidays = CustomUSHolidayCalendar()
# US Labor day as 2019-09-09

print(usholidays.holidays(start_date, datetime(2019, 12, 1)))

custom_bday_us = CustomBusinessMonthBegin(calendar=CustomUSHolidayCalendar())
bday_over_df = pd.date_range(start=start_date,
                             end=end_date,
                             freq=custom_bday_us)

最后,将第一个工作日偏移4个工作日。

print(bday_over_df + 4*BDay())

DatetimeIndex(['2019-01-08', '2019-02-07', '2019-03-07', '2019-04-05',
               '2019-05-07', '2019-06-07', '2019-07-05', '2019-08-07',
               '2019-09-06', '2019-10-07', '2019-11-07', '2019-12-06',
               '2020-01-08', '2020-02-07', '2020-03-06', '2020-04-07',
               '2020-05-07', '2020-06-05', '2020-07-07', '2020-08-07',
               '2020-09-07', '2020-10-07', '2020-11-06', '2020-12-07',
               '2021-01-08', '2021-02-05', '2021-03-05', '2021-04-07',
               '2021-05-07', '2021-06-07', '2021-07-07', '2021-08-06',
               '2021-09-07', '2021-10-07', '2021-11-05', '2021-12-07',
               '2022-01-07', '2022-02-07', '2022-03-07', '2022-04-07',
               '2022-05-06', '2022-06-07', '2022-07-07', '2022-08-05',
               '2022-09-07', '2022-10-07', '2022-11-07', '2022-12-07',
               '2023-01-09', '2023-02-07', '2023-03-07', '2023-04-07',
               '2023-05-05', '2023-06-07', '2023-07-07', '2023-08-07',
               '2023-09-07', '2023-10-06', '2023-11-07', '2023-12-07',
               '2024-01-08', '2024-02-07', '2024-03-07', '2024-04-05',
               '2024-05-07', '2024-06-07', '2024-07-05', '2024-08-07'],
              dtype='datetime64[ns]', freq=None)