Skip to main content

Business (Python)

circleci-badge pypi-badge

Date calculations based on business calendars. (Python 3.6+)

Python implementation of https://github.com/gocardless/business

Documentation

To get business, simply:

$ pip install business-python

Getting started

Get started with business by creating an instance of the calendar class, passing in a hash that specifies which days of the week are considered working days, and which days are holidays.

from business.calendar import Calendar

calendar = Calendar(
  working_days=["monday", "tuesday", "wednesday", "thursday", "friday"],
  # array items are either parseable date strings, or real datetime.date objects
  holidays=["January 1st, 2020", "April 10th, 2020"],
  extra_working_dates=[],
)

extra_working_dates key makes the calendar to consider a weekend day as a working day.

A few calendar configs are bundled with the package (see business/data for details). Load them by calling the load class method on Calendar.

calendar = Calendar.load("weekdays")

If working_days is missing, then common default is used (mon-fri). If holidays is missing, "no holidays" assumed. If extra_working_dates is missing, then no changes in working_days will happen.

Elements of holidays and extra_working_dates may be either strings that Calendar.parse_date() can understand, or YYYY-MM-DD (which is considered as a Date by Python YAML itself).

holidays:
  - 2017-01-08 # Same as January 8th, 2017

The load_cache method allows a thread safe way to avoid reloading the same calendar multiple times, and provides a performant way to dynamically load calendars for different requests.

calendar = Calendar.load_cache("weekdays")

Input data types

The parse_date method is used to process the input date(s) in each method and return a datetime.date object.

Calendar.parse_date("2019-01-01")
# => datetime.date(2019, 1, 1)

Supported data types are:

  • datetime.date
  • datetime.datetime
  • pandas.Timestamp (treated as datetime.datetime)
  • date string parseable by dateutil.parser.parse

numpy.datetime64 is not supported, but can be converted to datetime.date:

numpy.datetime64('2014-06-01T23:00:05.453000000').astype('M8[D]').astype('O')
# =>  datetime.date(2014, 6, 1)

Checking for business days

To check whether a given date is a business day (falls on one of the specified working days or extra working dates, and is not a holiday), use the is_business_day method on Calendar.

calendar.is_business_day("Monday, 8 June 2020")
# => true
calendar.is_business_day("Sunday, 7 June 2020")
# => false

Custom calendars

To use a calendar you've written yourself, you need to add the directory it's stored in as an additional calendar load path:

Calendar.additional_load_paths = ['path/to/your/calendar/directory']

You can then load the calendar as normal.

Business day arithmetic

For our purposes, date-based calculations are sufficient. Supporting time-based calculations as well makes the code significantly more complex. We chose to avoid this extra complexity by sticking solely to date-based mathematics.

The add_business_days method is used to perform business day arithmetic on dates.

input_date = Calendar.parse_date("Thursday, 12 June 2014")
calendar.add_business_days(input_date, 4).strftime("%A, %d %B %Y")
# => "Wednesday, 18 June 2014"
calendar.add_business_days(input_date, -4).strftime("%A, %d %B %Y")
# => "Friday, 06 June 2014"

The roll_forward and roll_backward methods snap a date to a nearby business day. If provided with a business day, they will return that date. Otherwise, they will advance (forward for roll_forward and backward for roll_backward) until a business day is found.

input_date = Calendar.parse_date("Saturday, 14 June 2014")
calendar.roll_forward(input_date).strftime("%A, %d %B %Y")
# => "Monday, 16 June 2014"
calendar.roll_backward(input_date).strftime("%A, %d %B %Y")
# => "Friday, 13 June 2014"

In contrast, the next_business_day and previous_business_day methods will always move to a next or previous date until a business day is found, regardless if the input provided is a business day.

input_date = Calendar.parse_date("Monday, 9 June 2014")
calendar.roll_forward(input_date).strftime("%A, %d %B %Y")
# => "Monday, 09 June 2014"
calendar.next_business_day(input_date).strftime("%A, %d %B %Y")
# => "Tuesday, 10 June 2014"
calendar.previous_business_day(input_date).strftime("%A, %d %B %Y")
# => "Friday, 06 June 2014"

To count the number of business days between two dates, pass the dates to business_days_between. This method counts from start of the first date to start of the second date. So, assuming no holidays, there would be two business days between a Monday and a Wednesday.

from datetime import timedelta

input_date = Calendar.parse_date("Saturday, 14 June 2014")
calendar.business_days_between(input_date, input_date + timedelta(days=7))
# => 5

The get_business_day_of_month method return the running total of business days for a given date in that month. This method counts the number of business days from the start of the first day of the month to the given input date.

input_date = Calendar.parse_date("Thursday, 12 June 2014")
calendar.get_business_day_of_month(input_date)
# => 9

Included Calendars

We include some calendar data with this package but give no guarantees of its accuracy. The calendars that we include are:

  • ACH (United States)
  • Bacs
  • Bankgirot
  • BECS (Australia)
  • BECSNZ (New Zealand)
  • PAD (Canada)
  • Betalingsservice
  • Target (SEPA)
  • TargetFrance (SEPA + French bank holidays)

License & Contributing

GoCardless ♥ open source. If you do too, come join us.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

business-python-1.0.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

business_python-1.0.2-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file business-python-1.0.2.tar.gz.

File metadata

  • Download URL: business-python-1.0.2.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.9 Darwin/18.7.0

File hashes

Hashes for business-python-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e1c7159114f81a94d8f9b1f3463d2d5f5103b6a473a07caa6644757c650881bd
MD5 801e227a73d944842b5af4c1d56beebe
BLAKE2b-256 dfed489ecc0de580afd4fb10f04470234a32dce183078c4d486087bbe6ddf427

See more details on using hashes here.

File details

Details for the file business_python-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: business_python-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.9 Darwin/18.7.0

File hashes

Hashes for business_python-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3a924ed1b7a060e9910901e13957b1ec750c35a613e12ce92a7efc98653c0dff
MD5 9af064b1647066eb944a8b2063b4ef61
BLAKE2b-256 351b7d5a2c15f41d29b4c376667f0acf48537c0e414ec8cde649058da982027b

See more details on using hashes here.

Release history Release notifications | RSS feed

2.1.0

2 files

2.0.3

2 files

2.0.2

2 files

2.0.1

2 files

2.0.0

2 files

1.0.3

2 files

This release

1.0.2 This release

2 files

1.0.1

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page