Pandas Time Series Offset

Trying to define a rule set using the pandas.tseries.holidays class, but cannot figure out how easy it is to create a rule based on another rule. I have the rule below, but then I just want to create another rule that shifts the original rule by one business day:

Thanksgiving Day:

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, I'm trying to create a rule for Cyber ​​Monday, which will be just Monday after Thanksgiving. Tried below, but it returns 11-2

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

But the above will not work back

Type error: unsupported type for add operation

+5
source share
4 answers

With the latest pandas 0.23.4 it's pretty easy to do 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)]) 
+1
source

At present, it is not possible to determine both the bias and compliance parameters in the same vacation rule.

I found this problem with boxing day in England . Boxing Day is the next business day after Christmas.

I encoded the solution using a compliance parameter that points to the corresponding rule, in this case: 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. If you need a different compliance rule, you can create your own function, for example, the following original Pandas watch functions:

 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)) 
+3
source

I do not believe that you can indicate holidays relative to other holidays. However, in your case, we can determine the holidays that meet your requirements. Given Thanksgiving is the fourth Thursday, BlackFriday is the fourth Friday, and CyberMonday is the fourth Saturday (celebrated next Monday). The most recent Thanksgiving date is November 28th, so Saturday will be November 30th and the “holiday” will be observed on December 2nd.

 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) 
+1
source

For everyone who comes across this. Got this help after posting a problem with Pandas that solved my problem. https://github.com/pydata/pandas/issues/10217#issuecomment-106040041

+1
source

All Articles