Python Holidays Library

Let us see about the holidays library here.

holidays library in Python:

The holidays library will help to determine whether or not a particular day is a holiday in various countries. Only public holidays are visible to us.

Syntax:

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

Parameters

years:

The years argument specifies an iterable list of integers that should be generated automatically by the holiday object. This is only used when the expand parameter is set to False. The years parameter’s default value is [].

expand:

The expand is a boolean argument that specifies whether or not the holidays should be appended to the holidays object in new years. True is the default value.

observed:

It is also a boolean argument. When set to True, it includes the observed day of a holiday that falls on a weekend, if applicable. True is the default value.

prov:

It is a string that represents a province

state: 

It is a string representing a state.

Methods of holidays module

get(key, default = None):

It returns the name of the holiday on the specified date in the key parameter. If more than one holiday falls on the same day, the names are separated by commas.

getlist(key):

It returns a list of names for holidays.

pop(key, default = None):

It removes the key from the holidays object.

Before we work with this module, we should first install it.

Installation:

pip install holidays

Python Holidays Library

Printing the holidays in the specified Year

Approach:

  • Import holidays module using the import keyword
  • Instantiate the holidays.India() and pass some random year as an argument to it to get the India holidays for the given year(here it returns as a dictionary).
  • Loop in the above holidays dictionary using the items() function and the for loop
  • Print the holiday date and the occasion for the year given.
  • The Exit of the Program.

Below is the implementation:

# Import holidays module using the import keyword
import holidays

# Instantiate the holidays.India() and pass some random year as an argument to it
# to get the India holidays for the given year(here it returns as a dictionary).
Indiaholidays = holidays.India(years=2021)
# Loop in the above holidays dictionary using the items() function and the for loop
for holiday_date, occasion in Indiaholidays.items():
   # Print the holiday date and the occasion for the year given.
   print(f'{holiday_date}: {occasion}')

Output:

2021-01-14: Makar Sankranti / Pongal
2021-01-26: Republic Day
2021-08-15: Independence Day
2021-10-02: Gandhi Jayanti
2021-05-01: Labour Day
2021-12-25: Christmas

Checking if the given day is a holiday or Not

Approach:

  • Import time from datetime module using the import keyword
  • Import holidays module using the import keyword
  • Call the India() method of the holidays method to get the India holidays (here it returns as a dictionary).
  • Give the date as static input and store it in a variable.
  • Check if the given date is a holiday or NOT in the above holidays dictionary using in,
    if conditional statements.
  • If it is true, then print the Occassion/holiday name of the given date using the get() method
  • Else print the given date is NOT a Holiday.
  • The Exit of the Program.

Below is the implementation:

# Import time from datetime module using the import keyword
from datetime import time
# Import holidays module using the import keyword
import holidays
# Call the India() method of the holidays method to get the India holidays.
# (here it returns as a dictionary).
Indiaholidays = holidays.India()
# Give the date as static input and store it in a variable.
gvn_date = '2021-12-25'
# Check if the given date is a holiday or NOT in the above holidays dictionary using in, 
# if conditional statements.
if gvn_date in Indiaholidays:
    # If it is true, then print the Occassion/holiday name of the given date using the get() method
    print(Indiaholidays.get(gvn_date))
else:
    # Else print the given date is NOT a Holiday
    print("The given date {", gvn_date, "} is NOT a Holiday")

Output:

Christmas

To return the holiday date as a List

The get_list() methods returns the output as a list.

Approach:

  • Import time from datetime module using the import keyword
  • Import holidays module using the import keyword
  • Call the India() method of the holidays method to get the India holidays (here it returns as a dictionary).
  • Give the date as user input using the input() function and store it in a variable.
  • Check if the given date is a holiday or NOT in the above holidays dictionary using in, if conditional statements.
  • If it is true, then print the Occassion/holiday name of the given date using the get_list() method
    as a list.
  • Else print the given date is NOT a Holiday.
  • The Exit of the Program.

Below is the implementation:

# Import time from datetime module using the import keyword
from datetime import time
# Import holidays module using the import keyword
import holidays
# Call the India() method of the holidays method to get the India holidays.
# (here it returns as a dictionary).
Indiaholidays = holidays.India()
# Give the date as user input using the input() function and store it in a variable.
gvn_date = input("Enter some random date:")
# Check if the given date is a holiday or NOT in the above holidays dictionary using in, 
# if conditional statements.
if gvn_date in Indiaholidays:
    # If it is true, then print the Occassion/holiday name of the given date using the get_list() method
    # as a list.
    print(Indiaholidays.get_list(gvn_date))
else:
    # Else print the given date is NOT a Holiday
    print("The given date {", gvn_date, "} is NOT a Holiday")

Output:

Enter some random date:2021-01-26
['Republic Day']
Enter some random date:2015-03-02
The given date { 2015-03-02 } is NOT a Holiday