简体   繁体   English

熊猫时间序列假日规则抵消

[英]Pandas Time Series Holiday Rule Offset

Trying to define a set of rules using pandas.tseries.holidays class but can't figure out how to just create a rule based on another rule. 尝试使用pandas.tseries.holidays类定义一组规则,但无法弄清楚如何根据其他规则创建规则。 I have the below rule but then want to just create another rule that offsets original rule by one business day: 我有以下规则,但后来想要创建另一个规则,在一个工作日内抵消原始规则:

Thanksgiving: 感恩:

Holiday("Thanksgiving Day", month=11, day=1, offset=pd.DateOffset(weekday=TH(4))),

Black Friday: 黑色星期五:

Holiday("Thanksgiving Day", month=11, day=1, 
        offset=pd.DateOffset(weekday=TH(4))) + pd.DateOffset(1),

Similarly trying to create rule for Cyber Monday which would just be the Monday following Thanksgiving. 同样试图为网络星期一创建规则,这将是感恩节后的星期一。 Tried the below but this returns 11-2 尝试以下但是这返回11-2

Holiday("Thanksgiving Day", month=11, day=1,
        offset=pd.DateOffset(weekday=TH(4)), observance=next_monday)

But above won't work returning 但上面的回归不会起作用

TypeError: unsupported type for add operation TypeError:不支持的添加操作类型

Nowadays, it's not possible define both offset and observance parameters in the same holiday rule. 如今,在同一假日规则中无法定义偏移遵守参数。

I found this issue with the Boxing holiday day in England . 我在英格兰的拳击假日那天发现了这个问题。 The Boxing Day is the next workday after Christmas day. 节礼日是圣诞节后的下一个工作日。

I coded the solution using the observance parameter pointing to the appropriate rule, in this case: after_nearest_workday 我使用指向适当规则的observance参数对解决方案进行编码,在本例中为: after_nearest_workday

    Holiday('Christmas', month=12, day=25, observance=nearest_workday),
    Holiday('Boxing Day', month=12, day=25, observance=after_nearest_workday)

after_nearest_workday is a function. after_nearest_workday是一个函数。 If you need another observance rule, you can create your own function like the following original Pandas observance functions: 如果您需要另一个遵守规则,您可以创建自己的函数,如下面的原始Pandas 遵守函数:

def nearest_workday(dt):
    """
    If holiday falls on Saturday, use day before (Friday) instead;
    if holiday falls on Sunday, use day thereafter (Monday) instead.
    """
    if dt.weekday() == 5:
        return dt - timedelta(1)
    elif dt.weekday() == 6:
        return dt + timedelta(1)
    return dt

def after_nearest_workday(dt):
    """
    returns next workday after nearest workday
    needed for Boxing day or multiple holidays in a series
    """
    return next_workday(nearest_workday(dt))

I don't believe you can specify holidays relative other holidays. 我不相信你可以指定假期相对于其他假期。 However, in your case, we can define holidays that match your requirement. 但是,在您的情况下,我们可以定义符合您要求的假期。 Given that the Thanksgiving is the fourth Thursday, then BlackFriday is the fourth Friday, and CyberMonday is the fourth Saturday (observed on the following Monday). 鉴于感恩节是第四个星期四,BlackFriday是第四个星期五,Cyber​​Monday是第四个星期六(在下周一观察)。 The latest date possible for Thanksgiving is Nov. 28, so that Saturday would be Nov. 30 and the 'holiday' would be observed on Dec. 2. 感恩节的最晚日期是11月28日,因此星期六将是11月30日,12月2日将举行“假期”节目。

from pandas.tseries.holiday import Holiday, TH, FR, SA, next_monday

Holiday("Black Friday", month=11, day=1, offset=pd.DateOffset(weekday=FR(4)))
Holiday("CyberMonday", month=11, day=1, offset=pd.DateOffset(weekday=SA(4)), 
        observance=next_monday)

For anyone else who encounters this. 对于遇到此事的其他人。 Got this help after submitting issue with Pandas which solved my issue. 在提交Pandas问题解决了我的问题后得到了这个帮助。 https://github.com/pydata/pandas/issues/10217#issuecomment-106040041 https://github.com/pydata/pandas/issues/10217#issuecomment-106040041

With the latest pandas 0.23.4 , it's pretty easy to do this now. 随着最新的pandas 0.23.4 ,现在很容易做到这一点。

import pandas as pd
from pandas.tseries.offsets import Day
from dateutil.relativedelta import TH

BlackFriday = Holiday("Black Friday", month=11, day=1, 
        offset=[pd.DateOffset(weekday=TH(4)), Day(1)])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM