The holidays
module in Python is a library that provides a way to generate and manage holiday dates for different countries, regions, and subregions. This can be useful in various applications, such as calculating business days, scheduling events, or simply keeping track of public holidays.
Installing the Holidays Module
To use the holidays
module, you first need to install it using pip:
pip install holidays
Basic Usage
Once installed, you can use the holidays
module to create a list of holidays for a specific country. You can also check if a particular date is a holiday and get details about the holiday.
Example: Generating Holidays for a Country
import holidays
# Create a holiday list for the United States
us_holidays = holidays.US()
# Print all US holidays for the year 2024
for date, name in sorted(us_holidays.items()):
if date.year == 2024:
print(date, name)
Checking if a Date is a Holiday
You can check if a specific date is a holiday by simply using the date as a key in the holiday object. If the date is a holiday, it will return the name of the holiday; otherwise, it will return None
.
Example: Checking for a Holiday
import holidays
# Create a holiday list for the United States
us_holidays = holidays.US()
# Check if a specific date is a holiday
date_to_check = "2024-07-04"
if date_to_check in us_holidays:
print(f"{date_to_check} is a holiday: {us_holidays[date_to_check]}")
else:
print(f"{date_to_check} is not a holiday.")
Output:
2024-07-04 is a holiday: Independence Day
Working with Different Countries
The holidays
module supports holidays for many different countries. You can create holiday objects for different countries by passing the appropriate country code.
Example: Holidays for Different Countries
import holidays
# Create a holiday list for the United Kingdom
uk_holidays = holidays.UK()
# Check if a specific date is a holiday in the UK
date_to_check = "2024-12-25"
if date_to_check in uk_holidays:
print(f"{date_to_check} is a holiday in the UK: {uk_holidays[date_to_check]}")
else:
print(f"{date_to_check} is not a holiday in the UK.")
Output:
2024-12-25 is a holiday in the UK: Christmas Day
Customizing Holidays
You can customize the holidays by creating your own holiday classes. This can be useful if you want to add specific holidays for a company, a custom region, or other special occasions.
Example: Creating Custom Holidays
import holidays
# Define custom holidays
class MyCustomHolidays(holidays.HolidayBase):
def _populate(self, year):
# Call the base class method to ensure standard holidays are included
super()._populate(year)
# Add a custom holiday
self[year, 12, 31] = "New Year's Eve"
# Create an instance of the custom holidays
my_holidays = MyCustomHolidays()
# Check if a specific date is a holiday in the custom holiday list
date_to_check = "2024-12-31"
if date_to_check in my_holidays:
print(f"{date_to_check} is a custom holiday: {my_holidays[date_to_check]}")
else:
print(f"{date_to_check} is not a holiday in the custom holiday list.")
Output:
2024-12-31 is a custom holiday: New Year's Eve
Supported Countries and Regions
The holidays
module supports many countries and regions, including:
- United States (
holidays.US()
) - United Kingdom (
holidays.UK()
) - Canada (
holidays.CA()
) - Australia (
holidays.AU()
) - India (
holidays.IN()
) - And many more.
Each country may also support regional holidays specific to states or provinces.
Conclusion
The holidays
module in Python is a powerful tool for managing holiday dates for various countries and regions. Whether you need to check if a date is a holiday, generate a list of holidays, or create custom holidays, this module provides a flexible and easy-to-use interface for handling holidays in your Python applications.