Workadays
Workadays is a Python library designed for working with business days, calendar days, and 360-day basis (30/360) date calculations. It allows users to add business days to a given date, check if a specific date is a business day, and retrieve a list of holidays for various countries, states, and provinces. The library had its latest release on December 30, 2023, and generally follows an annual or semi-annual release cadence.
Common errors
-
ModuleNotFoundError: No module named 'workday'
cause Attempting to import from the incorrect package name 'workday' instead of 'workadays'.fixChange your import statement from `from workday import ...` to `from workadays import ...`. -
AttributeError: module 'workadays' has no attribute 'networkdays'
cause You likely used `import workadays` and then tried to call methods directly on the `workadays` module. The core functions are typically exposed through the `workdays` object within the package.fixUse `from workadays import workdays` (or `from workadays import workdays as wd`) and then call methods like `workdays.networkdays(...)` or `wd.networkdays(...)`.
Warnings
- gotcha Be careful not to confuse `workadays` (plural) with the `workday` (singular) PyPI package. They are different libraries with distinct functionalities. Always ensure you install `workadays`.
- gotcha When calculating business days or checking holidays, remember to specify the `country` parameter (and optionally `state` or `province`) to ensure accurate results, as holiday calendars are region-specific. The default country might not match your expectation.
Install
-
pip install workadays
Imports
- workdays
import workadays
from workadays import workdays
- workdays
from workday import workdays
from workadays import workdays as wd
Quickstart
import datetime as dt
from workadays import workdays as wd
# Example: Calculate the number of business days between two dates in Brazil
dt_inicio = dt.date(2022, 9, 20)
dt_atual = dt.date(2022, 12, 12)
# You can also pass a list of custom holidays if needed
# holidays = [dt.date(2022, 10, 12), dt.date(2022, 11, 2)]
# n = wd.networkdays(dt_inicio, dt_atual, country='BR', holidays=holidays)
n = wd.networkdays(dt_inicio, dt_atual, country='BR')
print(f"Number of business days between {dt_inicio} and {dt_atual} in Brazil: {n}")
# Example: Add 5 business days to a date
start_date = dt.date(2024, 4, 15)
future_date = wd.add_working_days(start_date, 5, country='US')
print(f"5 business days after {start_date} in the US is: {future_date}")
# Example: Check if a date is a holiday
is_holiday = wd.is_holiday(dt.date(2024, 12, 25), country='US')
print(f"Is December 25, 2024 a holiday in the US? {is_holiday}")