Python | Holidays library - GeeksforGeeks

17 Jul, 2018
Python Holidays library is an efficient library for determining whether a specific date is a holiday as fast and flexible as possible. For any country, one can find whether that day is a holiday or not. Only fixed days(Public) holidays like Christmas, New Year, etc. can be detected.
Installation:
pip install holidays
Syntax:
holidays.HolidayBase(years=[], expand=True, observed=True, prov=None, state=None)
Parameters:
years : An iterable list of integers specifying the years that the Holiday object should pre-generate. This would generally only be used if setting expand to False. (Default[])
expand : A boolean value which specifies whether or not to append holidays in new years to the holidays object. (Default: True)
observed : A boolean value which when set to True will include the observed day of a holiday that falls on a weekend, when appropriate. (Default: True)
prov : A string specifying a province that has unique statutory holidays. (Default: Australia=’ACT’, Canada=’ON’, NewZealand=None)
state : A string specifying a state that has unique statutory holidays. (Default: UnitedStates=None)
Methods:
get(key, default=None): Returns a string containing the name of the holiday(s) in date key, which can be of date, datetime, string, unicode, bytes, integer or float type. If multiple holidays fall on the same date the names will be separated by commas.
get_list(key): Same as get except returns a list of holiday names instead of a comma-separated string.
pop(key, default=None): Same as get, except the key is removed from the holiday object update/append. Accepts dictionary of {date: name} pairs, a list of dates, or even singular date/string/timestamp objects and adds them to the list of holidays.
Code #1 : For a Particular Country and Year display all Holidays.
from datetime import dateimport holidays  # Select countryuk_holidays = holidays.UnitedKingdom()  # Print all the holidays in UnitedKingdom in year 2018for ptr in holidays.UnitedKingdom(years = 2018).items():    print(ptr)
Output:
(datetime.date(2018, 1, 1), "New Year's Day")
(datetime.date(2018, 1, 2), 'New Year Holiday [Scotland]')
(datetime.date(2018, 3, 17), "St. Patrick's Day [Northern Ireland]")
(datetime.date(2018, 3, 19), "St. Patrick's Day [Northern Ireland] (Observed)")
(datetime.date(2018, 3, 30), 'Good Friday')
(datetime.date(2018, 4, 2), 'Easter Monday [England, Wales, Northern Ireland]')
(datetime.date(2018, 5, 7), 'May Day')
(datetime.date(2018, 5, 28), 'Spring Bank Holiday')
(datetime.date(2018, 7, 12), 'Battle of the Boyne [Northern Ireland]')
(datetime.date(2018, 8, 6), 'Summer Bank Holiday [Scotland]')
(datetime.date(2018, 8, 27), 'Late Summer Bank Holiday [England, Wales, Northern Ireland]')
(datetime.date(2018, 11, 30), "St. Andrew's Day [Scotland]")
(datetime.date(2018, 12, 25), 'Christmas Day')
(datetime.date(2018, 12, 26), 'Boxing Day')

 Code #2 : Check whether a given date is holiday or not
from datetime import dateimport holidays  # Select countryuk_holidays = holidays.UnitedKingdom()  # If it is a holidays then it returns True else Falseprint('01-01-2018' in uk_holidays)print('02-01-2018' in uk_holidays)  # What holidays is it?print(uk_holidays.get('01-01-2018'))print(uk_holidays.get('02-01-2018'))
Output:
True
False
New Year's Day
None

 Code #3 : Holidays in North America
from datetime import dateimport holidays  # Combining Countriesnorth_america = holidays.CA() + holidays.US() + holidays.MX()# Output list of countries combinedprint(north_america.country)  print(north_america.get('07-01-2018'))print(north_america.get('07-04-2018'))
Output:
['CA', 'US', 'MX']
Canada Day
Independence Day

 
List of Countries included in Holiday Library –
In this Library, many countries are missing. So, we can make own Custom Holidays.
Code #4 : Custom Holidays adding for India
from datetime import dateimport holidays  in_holidays = holidays.HolidayBase()  # Let's check our republic dayprint('26-01-2019' in in_holidays)  # Add Holiday without descriptionin_holidays.append('26-01-2019')  # Let's verifyprint('26-01-2019' in in_holidays) # True  # Let's Check Descriptionprint(in_holidays.get('26-01-2019'))  # Add Holiday with descriptionin_holidays.append({'26-01-2019':'Republic Day India'})print(in_holidays.get('26-01-2019'))    # Add list of Dates Togetherin_holidays.append(['02-10-2018', '15-08-2018'])print('15-08-2018' in in_holidays) # Trueprint('02-10-2018' in in_holidays) # True  # a single date itemin_holidays.append(date(2018, 12, 25))print('25-12-2018' in in_holidays) # True
Output:
False 
True 
Holiday 
Republic Day India, Holiday
True 
True 
True 

 Reference: https://pypi.org/project/holidays/
Python-Library
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Selecting rows in pandas DataFrame based on conditions
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()