Open In App

Python | Holidays library

Last Updated : 17 Jul, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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 date
import holidays
  
# Select country
uk_holidays = holidays.UnitedKingdom()
  
# Print all the holidays in UnitedKingdom in year 2018
for 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 date
import holidays
  
# Select country
uk_holidays = holidays.UnitedKingdom()
  
# If it is a holidays then it returns True else False
print('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 date
import holidays
  
# Combining Countries
north_america = holidays.CA() + holidays.US() + holidays.MX()
# Output list of countries combined
print(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 –

Country Abbr Provinces/States Available
Argentina AR None
Australia AU prov = ACT (default), NSW, NT, QLD, SA, TAS, VIC, WA
Austria AT prov = B, K, N, O, S, ST, T, V, W (default)
Belgium BE None
Canada CA prov = AB, BC, MB, NB, NL, NS, NT, NU, ON (default),
PE, QC, SK, YU
Colombia CO None
Czech CZ None
Denmark DK None
England None
EuropeanCentralBank ECB,TAR Trans-European Automated Real-time Gross Settlement (TARGET2)
Finland FI None
France FRA Métropole (default), Alsace-Moselle, Guadeloupe, Guyane,
Martinique, Mayotte, Nouvelle-Calédonie, La Réunion,
Polynésie Française, Saint-Barthélémy, Saint-Martin,
Wallis-et-Futuna
Germany DE BW, BY, BE, BB, HB, HH, HE, MV, NI, NW, RP, SL, SN, ST,
SH, TH
Hungary HU None
Ireland IE
Isle of Man None
Italy IT prov = MI, RM
Japan JP None
Mexico MX None
Netherlands NL None
NewZealand NZ prov = NTL, AUK, TKI, HKB, WGN, MBH, NSN, CAN, STC, WTL,
OTA, STL, CIT
Northern Ireland None
Norway NO None
Polish PL None
Portugal PT None
PortugalExt PTE Portugal plus extended days most people have off
Scotland None
Slovenia SI None
Slovakia SK None
South Africa ZA None
Spain ES prov = AND, ARG, AST, CAN, CAM, CAL, CAT, CVA, EXT, GAL,
IBA, ICA, MAD, MUR, NAV, PVA, RIO
Sweden SE None
Switzerland CH prov = AG, AR, AI, BL, BS, BE, FR, GE, GL, GR, JU, LU,
NE, NW, OW, SG, SH, SZ, SO, TG, TI, UR, VD, VS, ZG, ZH
UnitedKingdom UK None
UnitedStates US state = AL, AK, AS, AZ, AR, CA, CO, CT, DE, DC, FL, GA,
GU, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MH, MA, MI,
FM, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, MP,
OH, OK, OR, PW, PA, PR, RI, SC, SD, TN, TX, UT, VT, VA,
VI, WA, WV, WI, WY
Wales None

In this Library, many countries are missing. So, we can make own Custom Holidays.

Code #4 : Custom Holidays adding for India




from datetime import date
import holidays
  
in_holidays = holidays.HolidayBase()
  
# Let's check our republic day
print('26-01-2019' in in_holidays)
  
# Add Holiday without description
in_holidays.append('26-01-2019')
  
# Let's verify
print('26-01-2019' in in_holidays) # True
  
# Let's Check Description
print(in_holidays.get('26-01-2019'))
  
# Add Holiday with description
in_holidays.append({'26-01-2019':'Republic Day India'})
print(in_holidays.get('26-01-2019'))
  
  
# Add list of Dates Together
in_holidays.append(['02-10-2018', '15-08-2018'])
print('15-08-2018' in in_holidays) # True
print('02-10-2018' in in_holidays) # True
  
# a single date item
in_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/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads