Learn Python – Python Holidays Module- Basic and advance

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.

pip install holidays  

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 –

holidays.HolidayBase(years=[], expand=True, observed=True, prov=None, state=None)  

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 –

from datetime import date  
import holidays  
# Select country  
USA_holidays_list = holidays.US()  
# printing all the holiday of USA year 2020  
for p in holidays.US(years = 2020).items():  
    print(p)  

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

from datetime import date  
import holidays  
  
# Select country  
India_holidays_list = holidays.India()  
  
# printing all the holiday of India year 2020  
for p in holidays.India(years = 2020).items():  
    print(p)  

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:

from datetime import date  
import holidays  
# Select country  
uk_holidays_list = holidays.UnitedKingdom()  
# printing all the holiday of India year 2020  
for p in holidays.UnitedKingdom(years = 2021).items():  
    print(p)  

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 –

from datetime import date  
import holidays  
  
# Select country  
uk_holidays = holidays.UnitedKingdom()  
date1 = '25-25-2021'  
  
j = date1 in uk_holidays  
  
# Cheking whether given data is a holiday  
if j == True:  
    print("Yes: ")  
    print (uk_holidays.get(date1))  
  
else:  
    print("No Holiday")  

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 –

import holidays  
  
add_holidays = holidays.HolidayBase()  
print('28-03-2021' in add_holidays) # checking for holiday  
  
# here we are adding Holiday without description  
add_holidays.append('28-03-2021')  
  
print('28-03-2021' in add_holidays) # True  
  
# Add Holiday with description  
add_holidays.append({'28-03-2021':'Holi: Festival of Colors, Holiday'})  
print(add_holidays.get('28-03-2021'))  

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