Python: How to calculate future US public holidays

Updated: February 13, 2024 By: Guest Contributor Post a comment

Overview

Predicting public holidays in the United States involves understanding the rules behind their occurrences. Whether fixed by date, like Independence Day on July 4th, or by a specific day of a month, like Thanksgiving on the fourth Thursday of November, each holiday follows its pattern. In this tutorial, we’ll explore how to calculate these dates using Python, one of the most accessible and widely-used programming languages today. We will utilize a mix of standard Python libraries and the third-party package holidays for comprehensive coverage.

First, ensure you have Python installed on your machine. This tutorial assumes you have Python 3.6 or newer. If you’re using an environment that does not already have the holidays package installed, you can do so by running pip install holidays in your command line.

Using the holidays Package

The holidays package is an excellent tool for getting a list of U.S. public holidays. It is regularly updated and supports various countries, including the United States. Here’s a simple example of how to use it:

import holidays

us_holidays = holidays.UnitedStates()
# Print all holidays in the current year
for date, name in sorted(us_holidays.items()):
    print(date, name)

This code snippet will print out all the U.S. public holidays for the current year. But what if we want to calculate holidays for a future year? The holidays package allows you to specify any year:

future_year = 2025
us_holidays = holidays.UnitedStates(year=future_year)
for date, name in sorted(us_holidays.items()):
    print(date, name)

Calculating Fixed Date Holidays

For holidays like New Year’s Day on January 1st or Independence Day on July 4th, the calculation is straightforward using Python’s datetime module:

from datetime import date

new_years_day = date(future_year, 1, 1)
print('New Year\'s Day:', new_years_day)

independence_day = date(future_year, 7, 4)
print('Independence Day:', independence_day)

Calculating Floating Holidays

Floating holidays, such as Thanksgiving, require a bit more logic. They are set as “the nth occurrence of a specific day in a month”. Using Python’s datetime and calendar modules, we can calculate these with ease:

import calendar
from datetime import date

def find_nth_weekday(year, month, weekday, nth):
    """Find the nth occurrence of a specific weekday within a given month."""
    month_calendar = calendar.monthcalendar(year, month)
    weekday_count = 0
    for week in month_calendar:
        if week[weekday] != 0: # weekday indexing starts from 0 (Monday)
            weekday_count += 1
            if weekday_count == nth:
                return date(year, month, week[weekday])
    return None

thanksgiving = find_nth_weekday(future_year, 11, 3, 4) # November, Thursday, 4th occurrence
print('Thanksgiving:', thanksgiving)

Considerations for Leap Years and Other Variabilities

Some holidays might shift depending on whether the year is a leap year or based on other criteria. It’s vital to incorporate this variability into your calculations. For example, in leap years, certain holidays might be observed on a different day to maintain consistency in observances.

Conclusion

By incorporating the holidays package along with Python’s built-in capabilities in handling dates and calendars, calculating U.S. public holidays becomes a simpler task. Whether for planning, scheduling, or just satisfying your curiosity, Python provides a robust toolset for accurately predicting when these holidays will occur in any given year. Remember that with time, the observance of holidays might evolve, and it’s essential to keep your dependencies, such as the holidays package, up-to-date to reflect these changes.