Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a US Holiday

Tags:

python

date

bank

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.

like image 859
Cerin Avatar asked Mar 06 '10 21:03

Cerin


People also ask

How many US holidays are there in 2022?

In the list of US federal holidays, there are 11 days and other important days that Americans recognize and celebrate.

How many national holidays are there?

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 ...


2 Answers

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  
like image 94
user2235214 Avatar answered Sep 20 '22 14:09

user2235214


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

like image 21
Shaurya Uppal Avatar answered Sep 17 '22 14:09

Shaurya Uppal