Given the below (working) Pandas calendar that sets a sequential set of days between Dec 22-30 as Holidays, is there a way to write this so that a range of dates can be set, instead of listing out each day individually?
import pandas as pd
import pandas.tseries.holiday as h
class CodeFreezeCalendar(h.AbstractHolidayCalendar):
rules = [
h.Holiday('New Years Day', month=1, day=1, observance=h.nearest_workday),
h.USThanksgivingDay,
h.Holiday('Dec 22', month=12, day=22),
h.Holiday('Dec 23', month=12, day=23),
h.Holiday('Dec 24', month=12, day=24),
h.Holiday('Dec 25', month=12, day=25),
h.Holiday('Dec 26', month=12, day=26),
h.Holiday('Dec 27', month=12, day=27),
h.Holiday('Dec 28', month=12, day=28),
h.Holiday('Dec 29', month=12, day=29),
h.Holiday('Dec 30', month=12, day=30)
)
]
I didn't see a way to have DateOffset handle an inclusive range of dates, so I don't think it can be used here. I may be wrong.
CodePudding user response:
If you use a for loop for the set days, 22-30, you can make a list and just add new years eve and thanksgiving after like this. Then add the dates for nye and thanksgiving manually.
import pandas as pd
import pandas.tseries.holiday as h
class CodeFreezeCalendar(h.AbstractHolidayCalendar):
rules = []
start = 22
end = 30
for i in range(start,end 1):
rules.append(h.Holiday(f'Dec {i}', month=12, day=i),)
rules = [
h.Holiday('New Years Day', month=1, day=1, observance=h.nearest_workday),
h.USThanksgivingDay,
]
CodePudding user response:
try that
import pandas as pd
import pandas.tseries.holiday as h
class CodeFreezeCalendar(h.AbstractHolidayCalendar):
rules = [
h.Holiday('New Years Day', month=1, day=1, observance=h.nearest_workday),
h.USThanksgivingDay,
*[h.Holiday(f'Dec {d}', month=12, day=d) for d in range(22,31)]
]