Workdays Date Utility
The `workdays` library provides utility functions to calculate working days between two dates, extending Python's standard `datetime` module. The current version is 1.5, with an infrequent release cadence focused on maintenance and minor enhancements.
Common errors
-
TypeError: 'str' object cannot be interpreted as a date
cause You are passing a date string directly to `networkdays` or within the `holidays` list instead of a `datetime.date` object.fixConvert date strings to `datetime.date` objects using `datetime.strptime('YYYY-MM-DD', '%Y-%m-%d').date()` or `date(year, month, day)`. -
TypeError: 'datetime.datetime' object is not iterable
cause You are passing a single `datetime.date` or `datetime.datetime` object directly to the `holidays` parameter, which expects an iterable (like a list).fixWrap single holiday dates in a list: `holidays=[date(2023, 1, 1)]`. -
AttributeError: 'datetime.datetime' object has no attribute 'weekday'
cause You are passing `datetime.datetime` objects where `datetime.date` objects are expected (e.g., for `start_date`, `end_date`, or elements in `holidays`).fixEnsure all date inputs are `datetime.date` objects. If you have a `datetime.datetime` object, convert it using `.date()`: `my_datetime_obj.date()`.
Warnings
- breaking Version 1.5 (released Feb 2023) introduced "flexibility of weekends". While not explicitly detailed as breaking, if your code relied on specific implicit default weekend behavior prior to 1.5, it's safest to explicitly define the `weekend_days` argument in your `networkdays` calls to maintain consistent behavior.
- gotcha The `holidays` argument for `networkdays` expects an iterable (e.g., a list or tuple) containing `datetime.date` objects. Passing strings, `datetime.datetime` objects, or a single `datetime.date` object directly (without being in a list) will lead to `TypeError` or incorrect calculations.
- gotcha Versions of `workdays` prior to `1.2` had known compatibility issues with Python 3. If you are running an older version on Python 3 and encounter unexpected errors, an upgrade is necessary.
Install
-
pip install workdays
Imports
- networkdays
from workdays import networkdays
Quickstart
from datetime import date
from workdays import networkdays
# Define start and end dates as datetime.date objects
start_date = date(2023, 1, 1)
end_date = date(2023, 1, 31)
# Define a list of holidays, also as datetime.date objects
holidays = [
date(2023, 1, 1), # New Year's Day
date(2023, 1, 16) # MLK Day
]
# Calculate network days (excluding weekends and specified holidays)
num_workdays = networkdays(start_date, end_date, holidays)
print(f"Number of network days between {start_date} and {end_date}: {num_workdays}")
# You can also specify custom weekend days (1=Monday, 7=Sunday, ISO weekday)
# For example, to make Friday, Saturday, Sunday weekends (5, 6, 7):
# num_workdays_custom_weekend = networkdays(start_date, end_date, holidays, weekend_days=[5, 6, 7])
# print(f"Network days with custom weekends (Fri, Sat, Sun off): {num_workdays_custom_weekend}")