Python Holidays Module

In this tutorial, we will learn one of the interesting Python libraries named holidays, which determines holidays of a given year of a particular day. It returns the specific result of the given date. We only need to pass the country name. However, it can only detect the fixed holidays such as Christmas, New Year, etc.

Installation

Type the following command to the terminal and press enter.

Import the holidays module to check whether it is installed or not.

The HolidayBase() Method

The holidays module provides the HolidayBase() method, which is used to detect the holidays of a specific date. Let's see the following syntax.

Syntax -

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

Let's understand the following example -

Example -

Output:

(datetime.date(2020, 1, 1), "New Year's Day")
(datetime.date(2020, 1, 20), 'Martin Luther King Jr. Day')
(datetime.date(2020, 2, 17), "Washington's Birthday")
(datetime.date(2020, 5, 25), 'Memorial Day')
(datetime.date(2020, 7, 4), 'Independence Day')
(datetime.date(2020, 7, 3), 'Independence Day (Observed)')
(datetime.date(2020, 9, 7), 'Labor Day')
(datetime.date(2020, 10, 12), 'Columbus Day')
(datetime.date(2020, 11, 11), 'Veterans Day')
(datetime.date(2020, 11, 26), 'Thanksgiving')
(datetime.date(2020, 12, 25), 'Christmas Day')

Example - 2

Output:

(datetime.date(2021, 1, 14), 'Makar Sankranti / Pongal')
(datetime.date(2021, 1, 26), 'Republic Day')
(datetime.date(2021, 8, 15), 'Independence Day')
(datetime.date(2021, 10, 2), 'Gandhi Jayanti')
(datetime.date(2021, 5, 1), 'Labour Day')
(datetime.date(2021, 12, 25), 'Christmas')

Note - As we can see in the output, the holidays module only returns the fixed-date holidays.

Example - 3:

Output:

 (datetime.date(2021, 1, 1), "New Year's Day")
(datetime.date(2021, 1, 2), 'New Year Holiday [Scotland]')
(datetime.date(2021, 1, 4), 'New Year Holiday [Scotland] (Observed)')
(datetime.date(2021, 3, 17), "St. Patrick's Day [Northern Ireland]")
(datetime.date(2021, 7, 12), 'Battle of the Boyne [Northern Ireland]')
(datetime.date(2021, 8, 2), 'Summer Bank Holiday [Scotland]')
(datetime.date(2021, 11, 30), "St. Andrew's Day [Scotland]")
(datetime.date(2021, 12, 25), 'Christmas Day')
(datetime.date(2021, 12, 27), 'Christmas Day (Observed)')
(datetime.date(2021, 4, 2), 'Good Friday')
(datetime.date(2021, 4, 5), 'Easter Monday [England, Wales, Northern Ireland]')
(datetime.date(2021, 5, 3), 'May Day')
(datetime.date(2021, 5, 31), 'Spring Bank Holiday')
(datetime.date(2021, 8, 30), 'Late Summer Bank Holiday [England, Wales, Northern Ireland]')
(datetime.date(2021, 12, 26), 'Boxing Day')
(datetime.date(2021, 12, 28), 'Boxing Day (Observed)')

We can get the holiday's list for the desired country.

How to check a given date is a holiday?

Suppose we want to check a specific date is a holiday or not. We can do this as follows.

Example -

Output:

Yes: 
Christmas Day

Custom Holidays Adding for India

Many countries are missed in the following table but luckily we can add own Custom holidays for a specific country. In the below example, we will add the custom holidays for India.

Example -

Output:

False
True
Holi: Festival of Colors, Holiday

Explanation:

In the above code, first, we have imported the date and holidays modules. We have then created the object of HolidayBase() and checked whether a given date consists of a holiday. It retuned false, so we add the custom holiday without description.

In the other statement, we have added the holiday with a description and printed the result.

Available Countries in Holidays Library

Below are the countries that are supported by the holidays library.

Country AbbreviationsProvice/State
ArgentinaARNone
AustraliaAUprov = ACT (default), NSW, NT, QLD, SA, TAS, VIC, WA
AustriaATprov = B, K, N, O, S, ST, T, V, W (default)
BelgiumBENone
CanadaCAprov = AB, BC, MB, NB, NL, NS, NT, NU, ON (default), PE, QC, SK, YU
ColombiaCONone
CzechCZNone
DenmarkDKNone
EnglandNone
EuropeanCentralBankECB,TARTrans-European Automated Real-time Gross Settlement (TARGET2)
FinlandFINone
FranceFRAMé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
GermanyDEBW, BY, BE, BB, HB, HH, HE, MV, NI, NW, RP, SL, SN, ST, SH, TH
HungaryHUNone
IrelandIE
Isle of ManNone
ItalyITprov = MI, RM
JapanJPNone
MexicoMXNone
NetherlandsNLNone
NewZealandNZprov = NTL, AUK, TKI, HKB, WGN, MBH, NSN, CAN, STC, WTL, OTA, STL, CIT
Northern IrelandNone
NorwayNONone
PolishPLNone
PortugalPTNone
PortugalExtPTEPortugal plus extended days most people have off
ScotlandNone
SloveniaSINone
SlovakiaSKNone
South AfricaZANone
SpainESprov = AND, ARG, AST, CAN, CAM, CAL, CAT, CVA, EXT, GAL,
IBA, ICA, MAD, MUR, NAV, PVA, RIO
SwedenSENone
SwitzerlandCHprov = 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
UnitedKingdomUKNone
UnitedStatesUSstate = 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
WalesNone





Latest Courses