Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python holidays Library



What is holidays Library in Python?

Python holidays library is used for determining whether a specific date is a holiday or not in the specific country. The holidays library provides the simples way to check if a specific date is a holiday, list holidays for a year, or even get the name of a holiday for a given date.

Usage of holidays Library

The holidays library can be applied to a wide range of uses −

  • This library can be useful to create workdays Vs. holidays scheduling.
  • It can be used for designing non-holiday calendars.
  • It can be used for rescheduling deadlines around holidays.
  • It can be used for creating reports with holiday.
  • You can explore country specific holidays.
  • It can be used for merging sets of country holiday items when handling over multiple zones.
  • It is useful to create project-based holiday lists along with your customized holidays.

Installation of holidays Library

To start using the holidays library, you need to install it first using the PIP, below is the command syntax to install it −

pip install holidays

Importing holidays Library

After installing the library, you need to import it in your Python code. Below is the import statement to import the holidays library −

import holidays

Specifying Country

To work with holidays for a specific country, create an instant of holidays library. Use the following statement to create an instance of holidays library for a specific country −

uk_holidays = holidays.UnitedKingdom()

Checking if a Date is a Holiday

You can use the holidays library to check whether a given date is a holiday in the specific country or not.

Example

In this example, we are checking a few dates whether they are holidays for not in united Kingdome.

# Importing holidays library
import holidays
# Specifying country
uk_holidays = holidays.UnitedKingdom() 
# Checking dates
print('01-01-2018' in uk_holidays) 
print('02-01-2018' in uk_holidays)

Output

True
False

Getting Holiday Name

To get the name of the specific holiday based on the given date, use the .get() method with the object name by passing the date as the parameter.

Example

In this example, we are getting the name of the holidays on the specified dates −

# Importing holidays library
import holidays
# Specifying country
uk_holidays = holidays.UnitedKingdom() 
# Getting Holiday Name
print(uk_holidays.get('01-01-2018'))
print(uk_holidays.get('02-01-2018'))

Output

New Year's Day
None

Listing All Holidays for a Year

You can also print all holidays in the specific country. To print all holidays for a particular year, iterate the loop with .items() method.

Example

In this example, we are printing the dates and names of the holidays in year 2018 in the United Kingdom.

# Importing holidays library
import holidays
# Specifying country
uk_holidays = holidays.UnitedKingdom() 
# Listing All Holidays for a Year 
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]") 
... 

Combining Countries

You can combine holiday lists from multiple countries.

Example

The following example demonstrates how you can combine the countries −

# Importing holidays library
import holidays
north_america = holidays.CA() + holidays.US() + holidays.MX() 
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

Creating Custom Holidays

By using holidays library, you can create custom holiday lists.

Example

In this example, we are creating custom holidays list −

# Importing holidays library
import holidays
import datetime
in_holidays = holidays.HolidayBase() 
# Add a single date 
in_holidays.append('26-01-2019')   
# Add a date with description 
in_holidays.append({'26-01-2019': 'Republic Day India'}) 
# Add multiple dates 
in_holidays.append(['02-10-2018', '15-08-2018'])   
# Add a date object 
in_holidays.append(datetime.date(2018, 12, 25)) 
print('26-01-2019' in in_holidays)
print(in_holidays.get('26-01-2019'))

Output

True
Holiday; Republic Day India

Common Methods of holidays Library

Some of the commonly used methods of holidays library are −

Method Description
get(key, default=None) This method returns the holiday name(s) for a given date.
get_list(key) This method is same as get, but it returns a list instead of a comma-separated string
pop(key, default=None) This method removes and returns the holiday for a given date.
python_reference.htm
Advertisements