The `business-duration` library calculates the duration between two dates and times, excluding weekends, public holidays, and non-business hours. It provides a single function, `businessDuration`, that can compute results in days, hours, minutes, or seconds. The project is actively maintained with irregular releases, with the latest version (0.68) released in October 2025.
pip install business-durationVerified import paths — ran on the pinned version, not inferred.
This example calculates the business duration in hours between two specific dates, excluding weekends and US public holidays in California, and considering a defined daily business hour range.
Iterate over the DataFrame or apply the function row-wise, ensuring individual datetime objects are passed for each calculation. Convert Series elements using `.dt.to_pydatetime()` or `pd.to_datetime(series_element)`.
Ensure `holidaylist` is an iterable of `datetime.date` objects or an object from the `holidays` library, as shown in the quickstart. For custom holidays, create a list of `datetime.date` objects.
Convert `datetime` objects to a consistent timezone (e.g., UTC) or apply timezone-aware adjustments before calling `businessDuration` if DST or differing timezones are a concern.
Apply the `businessDuration` function row-wise or in a loop. For example, `df.apply(lambda row: businessDuration(startdate=row['start_col'], enddate=row['end_col'], ...), axis=1)`.
Ensure `starttime` and `endtime` are `datetime.time` objects (e.g., `time(9, 0, 0)` for 9:00 AM) and `startdate`/`enddate` are `datetime.datetime` or `datetime.date` objects.
Verify that your `holidaylist` contains `datetime.date` objects. If using the `holidays` library, ensure it's imported as `pyholidays` and correctly initialized, e.g., `US_holiday_list = pyholidays.US(state='CA')`.