Home » A Holidays library for python

A Holidays library for python

Java SE 11 Developer (Upgrade) [1Z0-817]
1 Year Subscription
Java SE 11 Programmer I [1Z0-815] Practice Tests
Oracle Java Certification
Java SE 11 Programmer II [1Z0-816] Practice Tests
Spring Framework Basics Video Course

In this example we look at a library that you can install  for generating country, province and state specific sets of holidays on the fly.

Installation

The latest version can be installed  via pip like this.

$ pip install holidays

Syntax

class holidays.HolidayBase(years=[], expand=True, observed=True, prov=None, state=None)
The base class used to create holiday country classes.

Parameters:

It takes the following parameter.

  • years – It is an iterable list of integers that specify the holidays object should pre-generated. This parameter can only use if the setting increases to False.
  • expand – It represents a Boolean value that denotes whether or not to add holidays in the New Year. By default, it is true.
  • observed – When we set the observed Boolean value as True, it will include the observed day of a holiday that falls on a weekend.
  • prov – It is a string value specifying a province that has unique constitutional holidays. By Default (Australia='ACT', Canada='ON', NewZealand=None).
  • state – It represents a state with unique constitutional holidays. (Default – United States = None).

Available Countries

Country ISO code Provinces/States Available
Angola AO/AGO None
Argentina AR/ARG None
Aruba AW/ABW None
Australia AU/AUS prov = ACT (default), NSW, NT, QLD, SA, TAS, VIC, WA
Austria AT/AUT prov = 1, 2, 3, 4, 5, 6, 7, 8, 9 (default)
Azerbaijan AZ/AZE None
Bangladesh BD/BGD None
Belarus BY/BLR None
Belgium BE/BEL None
Botswana BW/BWA None
Brazil BR/BRA state = AC, AL, AM, AP, BA, CE, DF, ES, GO, MA, MG, MS, MT, PA, PB, PE, PI, RJ, RN, RO, RR, RS, SC, SE, SP, TO
Bulgaria BG/BLG None
Burundi BI/BDI None
Canada CA/CAN prov = AB, BC, MB, NB, NL, NS, NT, NU, ON (default), PE, QC, SK, YU
Chile CL/CHL state = AI, AN, AP, AR, AT, BI, CO, LI, LL, LR, MA, ML, NB, RM, TA, VS
China CN/CHN
Colombia CO/COL None
Croatia HR/HRV None
Curacao CW/CUW None
Czechia CZ/CZE None
Denmark DK/DNK None
Djibouti DJ/DJI None
DominicanRepublic DO/DOM None
Egypt EG/EGY None
Estonia EE/EST None
Ethiopia ET/ETH None
EuropeanCentralBank ECB/TAR Trans-European Automated Real-time Gross Settlement (TARGET2)
Finland FI/FIN None
France FR/FRA prov = 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
Georgia GE/GEO
Germany DE/DEU prov = BB, BE, BW, BY, BYP, HB, HE, HH, MV, NI, NW, RP, SH, SL, SN, ST, TH
Greece GR/GRC None
Honduras HN/HND None
HongKong HK/HKG None
Hungary HU/HUN None
Iceland IS/ISL None
India IN/IND prov = AP, AS, BR, CG, GJ, HR, KA, KL, MH, MP, OD, RJ, SK, TN, TN, UK, UP, WB
Ireland IE/IRL None
Israel IL/ISR None
Italy IT/ITA prov = AN, AO, BA, BL, BO, BS, BZ, CB, Cesena, CH, CS, CT, EN, FC, FE, FI, Forlì, FR, GE, GO, IS, KR, LT, MB, MI, MO, MN, MS, NA, PA, PC, PD, PG, PR, RM, SP, TS, VI
Jamaica JM/JAM None
Japan JP/JPN None
Kazakhstan KZ/KAZ None
Kenya KE/KEN None
Korea KR/KOR None
Latvia LV/LVA None
Lesotho LS/LSO None
Lithuania LT/LTU None
Luxembourg LU/LUX None
Malaysia MY/MYS state = JHR, KDH, KTN, MLK, NSN, PHG, PNG, PRK, PLS, SBH, SWK, SGR, TRG, KUL, LBN, PJY
Malawi MW/MWI None
Mexico MX/MEX None
Morocco MA/MOR None
Mozambique MZ/MOZ None
Netherlands NL/NLD None
Namibia NA/NAM None
NewZealand NZ/NZL prov = AUK, CAN, CIT, HKB, MBH, NSN, NTL, OTA, STC, STL, TKI, WGN, WTL
Nicaragua NI/NIC prov = MN
Nigeria NG/NGA None
NorthMacedonia MK/MKD None
Norway NO/NOR None
Paraguay PY/PRY None
Peru PE/PER None
Poland PL/POL None
Portugal PT/PRT None
PortugalExt PTE/PRTE Portugal plus extended days most people have off
Romania RO/ROU None
Russia RU/RUS None
SaudiArabia SA/SAU None
Serbia RS/SRB None
Singapore SG/SGP None
Slovakia SK/SVK None
Slovenia SI/SVN None
SouthAfrica ZA/ZAF None
Spain ES/ESP prov = AN, AR, AS, CB, CL, CM, CN, CT, EX, GA, IB, MC, MD, NC, PV, RI, VC
Swaziland SZ/SZW None
Sweden SE/SWE None
Switzerland CH/CHE 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
Taiwan TW/TWN None
Turkey TR/TUR None
Tunisia TN/TUN None
Ukraine UA/UKR None
UnitedArabEmirates AE/ARE None
UnitedKingdom UK/GB/GBR state = UK (default), England, Isle of Man, Northern Ireland, Scotland, Wales
UnitedStates US/USA 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
Venezuela VE/VEN None
Vietnam VN/VNM None
Zambia ZM/ZMB None
Zimbabwe ZW/ZWE None

 

Examples

In this example we will get the list of Uk holidays for 2022

from datetime import date  
import holidays

# Select country  
uk_holidays_list = holidays.UnitedKingdom()

# printing all the holiday of India year 2022  
for p in holidays.UnitedKingdom(years = 2022).items():  
    print(p)  

Running this I saw the following displayed

Run holidasyuk.py
(datetime.date(2022, 1, 1), "New Year's Day")
(datetime.date(2022, 1, 3), "New Year's Day (Observed)")
(datetime.date(2022, 1, 2), 'New Year Holiday [Scotland]')
(datetime.date(2022, 1, 4), 'New Year Holiday [Scotland] (Observed)')
(datetime.date(2022, 3, 17), "St. Patrick's Day [Northern Ireland]")
(datetime.date(2022, 7, 12), 'Battle of the Boyne [Northern Ireland]')
(datetime.date(2022, 8, 1), 'Summer Bank Holiday [Scotland]')
(datetime.date(2022, 11, 30), "St. Andrew's Day [Scotland]")
(datetime.date(2022, 12, 25), 'Christmas Day')
(datetime.date(2022, 12, 27), 'Christmas Day (Observed)')
(datetime.date(2022, 4, 15), 'Good Friday')
(datetime.date(2022, 4, 18), 'Easter Monday [England/Wales/Northern Ireland]')
(datetime.date(2022, 5, 2), 'May Day')
(datetime.date(2022, 6, 2), 'Spring Bank Holiday')
(datetime.date(2022, 8, 29), 'Late Summer Bank Holiday [England/Wales/Northern Ireland]')
(datetime.date(2022, 12, 26), 'Boxing Day')
(datetime.date(2022, 6, 3), 'Platinum Jubilee of Elizabeth II')

 

Holidays for a state or province

Now, I live in Scotland, so to get my holidays you can see in the table above I can set the state parameter to Scotland

from datetime import date  
import holidays

# Select country  
uk_holidays_list = holidays.UnitedKingdom()

# printing all the holiday of India year 2022  
for p in holidays.UnitedKingdom(state='Scotland',years = 2022).items():  
    print(p)  

When I run this I see the following

%Run holidays_scotland.py
(datetime.date(2022, 1, 1), "New Year's Day")
(datetime.date(2022, 1, 3), "New Year's Day (Observed)")
(datetime.date(2022, 1, 2), 'New Year Holiday')
(datetime.date(2022, 1, 4), 'New Year Holiday (Observed)')
(datetime.date(2022, 8, 1), 'Summer Bank Holiday')
(datetime.date(2022, 11, 30), "St. Andrew's Day")
(datetime.date(2022, 12, 25), 'Christmas Day')
(datetime.date(2022, 12, 27), 'Christmas Day (Observed)')
(datetime.date(2022, 4, 15), 'Good Friday')
(datetime.date(2022, 5, 2), 'May Day')
(datetime.date(2022, 6, 2), 'Spring Bank Holiday')
(datetime.date(2022, 12, 26), 'Boxing Day')
(datetime.date(2022, 6, 3), 'Platinum Jubilee of Elizabeth II')

Check a given date is a holiday

If you want to check a date is a holiday you can do something like this

from datetime import date
import holidays  
  
# Select country  
print (date(2022, 1, 1) in holidays.UK())
print (date(2022, 1, 24) in holidays.UK())
print (date(2022, 12, 25) in holidays.UK())

When run I saw the following displayed

>>> %Run checkholiday.py
True
False
True

Links

examples on github

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More