Using numpy.is_busday() function (4 examples)

Updated: March 2, 2024 By: Guest Contributor Post a comment

Understanding how to navigate through dates and determine business days plays a vital role in financial analytics, reporting, and data preprocessing tasks in Python. The NumPy library, particularly its is_busday() function, offers an efficient way to perform these operations on arrays of dates. This tutorial will guide you through using the numpy.is_busday() function with practical examples, moving from basic to more advanced applications.

Introduction to numpy.is_busday()

The numpy.is_busday() function checks whether given dates are business days. A business day normally refers to any day except weekends (Saturday and Sunday) and may exclude specific public holidays depending on the context or geographical location. With NumPy, you can customize which days to consider as weekends and specify holidays to achieve accurate results tailored to any need.

Basic Usage: Identifying Business Days

import numpy as np

# Given dates
dates = np.array(['2023-04-10', '2023-04-11', '2023-04-15', '2023-04-16'])

# Check if these dates are business days
print(np.is_busday(dates))

The output will be:

[True, True, False, False]

This example clearly illustrates how to identify which dates are business days. Here, the weekend days (Saturday and Sunday) are automatically recognized as non-business days.

Changing Weekends and Specifying Holidays

import numpy as np

# Define custom weekends (Friday and Saturday)
weekmask = '1111001'

# Specify holidays
holidays = ['2023-04-12']

dates = np.array(['2023-04-10', '2023-04-11', '2023-04-12', '2023-04-13'])

# Apply custom weekend and holidays
print(np.is_busday(dates, weekmask=weekmask, holidays=holidays))

The output will demonstrate how the specified Wednesday (which is a public holiday) and the custom weekends are respected:

[True, True, False, True]

Through customizing the weekmask and defining holidays, we can accurately evaluate business days in various contexts.

Working with Business Day Offsets

One advanced application of is_busday() is to calculate the next or previous business day relative to a given date, considering weekends and holidays. This is particularly useful for forecasting or scheduling tasks.

import numpy as np

# Define a base date
base_date = '2023-04-13'

# Calculate the next business day
print(np.busday_offset(base_date, 1, roll='forward'))

# Calculate the previous business day
print(np.busday_offset(base_date, -1, roll='backward'))

The outputs show the next and previous business days from the base date, excluding weekends and holidays:

'2023-04-14'
'2023-04-12'

Including Custom Business Days

Further flexibility is achieved by defining custom business days outside the traditional Monday to Friday week. This is particularly useful in countries or industries where business days do not follow the global norm.

import numpy as np

# Define a range of dates
dates_range = np.arange('2023-04-10', '2023-04-20', dtype='datetime64[D]')

# Specify non-standard business days
holidays = ['2023-04-14']
custom_business_days = np.is_busday(dates_range, weekmask='1111100', holidays=holidays)

# Print only the business days
for i in range(len(dates_range)):
    if custom_business_days[i]:
        print(dates_range[i])

This script prints all days considered business days, including a Saturday but excluding the specified holiday:

'2023-04-10'
'2023-04-11'
'2023-04-12'
'2023-04-13'
'2023-04-15'
'2023-04-17'
'2023-04-18'
'2023-04-19'

Conclusion

The numpy.is_busday() function is a powerful tool for determining business days from a series of dates. Whether handling financial transactions, scheduling, or data analytics tasks, this functionality allows for precise control over date-related operations. By mastering its basic to advanced uses, you can customize your data processing workflows to suit specific business calendars and requirements.