Detecting a US Holiday

Cerin picture Cerin · Mar 6, 2010 · Viewed 27.1k times · Source

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.

Answer

user2235214 picture user2235214 · Jul 9, 2014

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