11

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?

matan h
  • 900
  • 1
  • 10
  • 19
Blair Azzopardi
  • 502
  • 8
  • 17
  • 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 day – DasBoot Jul 24 '20 at 14:19
  • @DasBoot I tend to use pandas now whenever I need to handle holidays. Check out pandas date offsets https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.tseries.offsets.DateOffset.html; it might be helpful. – Blair Azzopardi Jul 24 '20 at 17:45
  • @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. – Joe Ferndz Jan 01 '21 at 23:37
  • @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. – Blair Azzopardi Jan 02 '21 at 10:10
  • more one, this also related to rrule itself in other languages where is no panda =) – Alexander Popov Mar 04 '21 at 16:13

0 Answers0