Python List Of Usa Holidays Between A Range
I have a need to fetch list of holidays in a given range, i.e., if start date is 20/12/2016 & end date is 10/1/2017, then I should get 25/12/2017, 1/1/2017. I can do this usin
Solution 1:
After a while of exploring, I have the got the solution myself, sharing here for reference as a solution:
%pyspark
import holidays
import datetime
from datetime import date, timedelta
import dateutil
from dateutil.relativedelta import relativedelta
us_holidays = holidays.UnitedStates()
custom_holidays = holidays.HolidayBase()
holidays_within_range=[]
fmt = '%Y-%m-%d'
holidays2013=[]
for date2,name in sorted(holidays.US(state='CA', years=2013).items()):
holidays2013.append(date2.strftime(fmt))
print holidays2013
fdate = date(2013, 1, 1)
s_date = fdate - dateutil.relativedelta.relativedelta(days=7)
e_date = fdate + relativedelta(months=1)
start_date = s_date.strftime(fmt)
end_date = e_date.strftime(fmt)
print "Range : "
print start_date, end_date
dd = [s_date + timedelta(days=x) for x in range((e_date-s_date).days + 1)]
for d in dd:
if(d in us_holidays):
custom_holidays.append(d)
holidays_within_range.append(d.strftime(fmt))
print holidays_within_range
days_from_closest_holiday = [(abs(fdate - datetime.datetime.strptime(hdate, fmt).date())).days for hdate in holidays_within_range]
print days_from_closest_holiday
The outputs for the above would be :
['2013-01-01', '2013-01-21', '2013-02-18', '2013-03-31', '2013-04-01', '2013-05-27', '2013-07-04', '2013-09-02', '2013-10-14', '2013-11-11', '2013-11-28', '2013-12-25']
Range :
2012-12-25 2013-02-01
['2012-12-25', '2013-01-01', '2013-01-21']
[7, 0, 20]
This doesn't require Pandas, and I hope this works with AWS Glue. Haven't updated the variable names, please change as necessary.
Thanks
Post a Comment for "Python List Of Usa Holidays Between A Range"