本文介绍了 pandas 时间序列假日规则偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用pandas.tseries.holidays类定义一组规则,但无法弄清楚如何仅基于另一个规则创建规则.我有以下规则,但是接下来只想创建另一个规则即可在一个工作日内抵消原始规则:

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. I have the below rule but then want to just create another rule that offsets original rule by one business day:

感恩节:

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

黑色星期五:

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

类似地,尝试为网络星期一"(即感恩节之后的星期一)创建规则.尝试了以下内容,但返回11-2

Similarly trying to create rule for Cyber Monday which would just be the Monday following Thanksgiving. Tried the below but this returns 11-2

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

但是上面的返回键将无法正常工作

But above won't work returning

推荐答案

有了最新的pandas 0.23.4,现在就很容易做到这一点.

With the latest pandas 0.23.4, it's pretty easy to do this now.

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)])

这篇关于 pandas 时间序列假日规则偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:22