Using numpy.busdaycalendar() function (4 examples)

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

Introduction

Understanding numpy’s busdaycalendar() function is integral for anyone working with date and time data in Python, especially in finance and econometrics. This powerful function helps in generating sequences of business days, which can be particularly useful for analyzing stock market data, scheduling, or any scenario where you need to consider only the working days in calculations.

Lets dive into understanding and using the numpy.busdaycalendar() function through a series of examples, transitioning from basic applications to more advanced uses.

Syntax & Parameters

Syntax:

numpy.busdaycalendar(weekmask='1111100', holidays=[])

Parameters:

  • weekmask: A seven-character string or an array_like of booleans, indicating which days of the week are considered business days. The default '1111100' treats Saturday and Sunday as weekends (non-business days).
  • holidays: An array_like object of datetime64[D], datetime.date, datetime.datetime, or strings in ISO 8601 date format, indicating the dates that should be excluded from the set of business days.

Returns:

  • output: A numpy.busdaycalendar object.

Example 1: Basic Usage

At its core, the numpy.busdaycalendar() function allows us to define what we consider to be business days. By default, Saturdays and Sundays are considered weekends, but this can be customized. Here’s how to define a simple business day calendar with default settings:

import numpy as np

# Define a business day calendar
busdaycal = np.busdaycalendar()

# Display the calendar's weekends
print(busdaycal.weekmask)

Output:

['1111100']

This output means that by default, Monday through Friday are considered business days, while Saturday and Sunday are not.

Example 2: Customizing Weekdays

Moving on to customization, let’s say your business also operates on Saturdays. Here’s how to adjust the calendar accordingly:

import numpy as np

# Custom business day calendar
busdaycal = np.busdaycalendar(weekmask='1111110')

# Display the customized weekends
print(busdaycal.weekmask)

Output:

['1111110']

This indicates that, in this customized calendar, only Sunday is considered a non-business day.

Example 3: Holidays

For more granularity, you can also specify holidays. Let’s add a specific date as a holiday to our business day calendar:

import numpy as np
from datetime import datetime

# Define a calendar with a holiday
holiday_date = datetime(2023, 1, 1) # New Year's Day
busdaycal = np.busdaycalendar(weekmask='1111110', holidays=['2023-01-01'])

# Check if a specific day is a business day
is_business_day = np.is_busday('2023-01-01', busdaycalendar=busdaycal)
print(f"Is New Year's Day a business day? {is_business_day}")

Output:

Is New Year's Day a business day? False

This illustrates how we can use the busdaycalendar() to account for holidays in determining business days.

Example 4: Advanced Calculation

Finally, let’s apply our custom busdaycalendar to perform a more complex calculation. We will calculate the business days between two dates, excluding weekends and specified holidays.

import numpy as np

# Define holidays
holidays = ['2023-01-01', '2023-07-04']

# Create a customized business day calendar
calendar = np.busdaycalendar(weekmask='1111110', holidays=holidays)

# Calculate business days between two dates
start_date = '2023-01-01'
end_date = '2023-12-31'
business_days = np.busday_count(start_date, end_date, busdaycalendar=calendar)

print(f'There are {business_days} business days between {start_date} and {end_date} considering the provided holidays.')

Output:

There are 313 business days between 2023-01-01 and 2023-12-31 considering the provided holidays.

This example demonstrates the utility of the numpy.busdaycalendar() function in performing detailed business day calculations, considering both weekends and holidays.

Conclusion

In this tutorial, we navigated through the capabilities of the numpy.busdaycalendar() function, from its basic setup to incorporating custom workdays and holidays into our calculations. Through these examples, it’s clear how invaluable numpy can be for handling complex date and time operations, especially when precise business day calculations are required.