The Python Holidays module is a library that provides an easy way to work with holidays and other important dates in Python. It includes lists of holidays for various countries and regions, as well as utility functions for working with dates and times.
To use the holidays module, you first need to install it using pip:
pip install holidays
Once installed, you can import it in your Python script and start using it. Here are some examples of what you can do with the holidays module:
Example 1: Get the holidays for a specific country
import holidays us_holidays = holidays.US() for date, name in us_holidays.items(): print(date, name)
This will print out a list of all the holidays for the United States, along with their dates and names.
Example 2: Check if a specific date is a holiday
import datetime import holidays us_holidays = holidays.US() date = datetime.date(2023, 7, 4) if date in us_holidays: print("July 4th is a US holiday") else: print("July 4th is not a US holiday")
This will check if July 4th, 2023 is a holiday in the United States, and print out the result.
Example 3: Get the next upcoming holiday
import holidays us_holidays = holidays.US() next_holiday = us_holidays.get_next(datetime.date.today()) print("The next US holiday is", next_holiday)
This will print out the next upcoming holiday in the United States.
Overall, the Python Holidays module is a useful tool for working with holidays and other important dates in Python.
Custom Holidays Adding for India:
To add custom holidays for India using the Python Holidays module, you can create a new HolidayBase class and add your custom holiday dates to it. Here’s an example:
import holidays from datetime import date class IndiaCustomHolidays(holidays.HolidayBase): def _populate(self, year): # Add custom holidays for India self[date(year, 1, 26)] = "Republic Day" self[date(year, 8, 15)] = "Independence Day" self[date(year, 10, 2)] = "Gandhi Jayanti" # Add more custom holidays here india_holidays = holidays.Join([holidays.India(), IndiaCustomHolidays()]) # Test the custom holidays for date, name in sorted(india_holidays.items()): print(date, name)
In this example, we created a new class called IndiaCustomHolidays that extends the HolidayBase class. We then added custom holiday dates for India by calling the
self object with the date and holiday name as arguments.
Next, we created a new instance of the Join class, which allows us to combine multiple holiday lists into a single list. We passed in the default India holiday list provided by the Holidays module, as well as our custom holiday list.
Finally, we tested the custom holiday list by iterating over all the holidays for India and printing out the dates and names.
You can add more custom holidays by adding new lines to the
_populate function with the desired date and name.
Available Countries in Holidays Library:
The Holidays library provides holiday information for many countries and regions around the world. Here’s a list of some of the countries and regions that are currently supported:
- Czech Republic
- New Zealand
- South Africa
- United Kingdom
- United States
These are just some of the countries and regions supported by the library, and new ones are being added all the time. To see the full list of countries and regions, you can check the documentation or the source code for the Holidays library.