What's the simplest way to determine if a date is a U.S. bank holiday in Python? There seem to be various calendars and webservices listing holidays for various countries, but I haven't found anything specific to banks in the U.S.
In the list of US federal holidays, there are 11 days and other important days that Americans recognize and celebrate.
The United States has established by law the following 12 permanent federal holidays, listed in the order they appear in the calendar: New Year's Day, Martin Luther King Jr.'s Birthday, Inauguration Day (every four years following a presidential election), George Washington's Birthday, Memorial Day, Juneteenth National ...
The Pandas package provides a convenient solution for this:
from pandas.tseries.holiday import USFederalHolidayCalendar cal = USFederalHolidayCalendar() holidays = cal.holidays(start='2014-01-01', end='2014-12-31').to_pydatetime() if datetime.datetime(2014,01,01) in holidays: print True
Use the holiday library in python.
pip install holidays
For USA holiday:
1. To check a date holiday or not.
from datetime import date import holidays # Select country us_holidays = holidays.US() # If it is a holidays then it returns True else False print('01-01-2018' in us_holidays) print('02-01-2018' in us_holidays) # What holidays is it? print(us_holidays.get('01-01-2018')) print(us_holidays.get('02-01-2018'))
2. To list out all holidays in US:
from datetime import date import holidays # Select country us_holidays = holidays.US() # Print all the holidays in US in year 2018 for ptr in holidays.US(years = 2018).items(): print(ptr)
You can find holidays for any country you like the list of countries listed on my Blog. My Blog on Holidays
Github Link of Holidays Python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With