Exclude calendar holidays from rrule before processing
Asked Answered
S

0

12

I am using the rrule method from python-dateutil package. I would like to create a rule that can ignore dates that are within a holiday calendar. I know about the exdate() method but this seems to filter the date from the output list only it has been generated.

from dateutil.rrule import *
from datetime import datetime

set = rruleset()
set.rrule(rrule(MONTHLY, count=4, bysetpos=-1, byweekday=(MO, TU, WE, TH, FR), dtstart=datetime(2013, 1, 1)))
print(list(set))
# outputs 4 dates without an exdate() call
# print : [datetime.datetime(2013, 1, 31, 0, 0), datetime.datetime(2013, 2, 28, 0, 0), datetime.datetime(2013, 3, 29, 0, 0), datetime.datetime(2013, 4, 30, 0, 0)]

set.exdate(datetime(2013, 2, 28, 0, 0))
list(set)
# only outputs 3 dates, ignoring the date provided in exdate()
# print : [datetime.datetime(2013, 1, 31, 0, 0), datetime.datetime(2013, 3, 29, 0, 0), datetime.datetime(2013, 4, 30, 0, 0)]

What I would like rrule to do is not omit the datetime(2013, 2, 28, 0, 0) but instead seek back to find the next best date according to the original rules, i.e. in this case datetime(2013, 2, 27, 0, 0). Any ideas how I could achieve this?

Supersaturate answered 13/5, 2013 at 11:53 Comment(5)
Quite an old question but did you find a way to solve this? I am thinking about a custom logic that checks the resulting dates against the "holidays" and moving the date backwards or forwards until the next valid dayEscapee
@Escapee I tend to use pandas now whenever I need to handle holidays. Check out pandas date offsets pandas.pydata.org/pandas-docs/stable/reference/api/…; it might be helpful.Supersaturate
@BlairAzzopardi, if this has been solved through Pandas now, do you still want to keep this question open? There are better ways to take care of this with newer pandas and python options.Giselegisella
@JoeFerndz I feel it's better to leave the question open and unanswered. If anyone in future has the same question they can save themselves time by seeing it is unanswered here. They can also check these comments for workarounds (currently pandas but there might be others). Perhaps even one day python-dateutil might support holidays in rrules.Supersaturate
more one, this also related to rrule itself in other languages where is no panda =)Tubbs

© 2022 - 2024 — McMap. All rights reserved.