Free cookie consent management tool by TermsFeed Creating business day calendar objects using Python | Pythontic.com

Creating business day calendar objects using Python

Overview:

  • The NumPy class busdaycalendar represents a business calendar. An instance of busdaycalendar holds two pieces of information:

    • What days of a week are working days and what days are non-working days – This information is held by the attribute weekmask.

    • A list of holidays which are exception to the attribute weekmask

  • The busdaycalendar is used by other NumPy functions that provides date and time support like is_busday() and busday_count().

Example 1:

This example creates a business calendar with the holidays for a stock exchange specified. It calculates how many trading days are there after deducting the two holidays in January.

# Example Python program that creates a business day 
# calendar for a Stock exchange and finds the 
# number of trading days
#
import numpy

# Data courtesy:https://www.nyse.com/markets/hours-calendars
# NYSE holidays for 2025
holidays = ['2025-01-01', 
            '2025-01-20',
            '2025-02-17',
            '2025-04-18',
            '2025-05-26',
            '2025-06-19',
            '2025-07-04',
            '2025-09-01',
            '2025-11-27',
            '2025-12-25']
workingDays = numpy.busdaycalendar(holidays = holidays)
print("Weekly working days mask:")
print(workingDays.weekmask)

startDate     = '2025-01-01'     
endDate     = '2025-01-31'

trdaingDayCount = numpy.busday_count(startDate, 
                                     endDate, 
                                     busdaycal = workingDays)
print("Number of trading days:{}".format(trdaingDayCount))

dateEx = '2025-07-29'
res = numpy.is_busday(dateEx, busdaycal = workingDays)
print("Is {} a working day?".format(dateEx))
print(res)

Output:

Weekly working days mask:
[ True  True  True  True  True False False]
Number of trading days:20
Is 2025-07-29 a working day?
True

Example 2:

This example has its week day mask modified for a Doctor who works on an emergency shift in December. The NumPy function busday_count() takes into account both the modified week day mask and the holiday list specified in the busdaycalendar object.

# Example Python program that creates calendar 
# for a Doctor who works on emergency shift

import numpy

# Holiday calendar for a Doctor who works on regular shifts
#regCalendar = numpy.busdaycalendar(holidays = holidays)

# Holiday calendar for a Doctor who works on emergency
# The weekly mask is different for the emergency doctor
# as she or he works on holidays
emgCalendar = numpy.busdaycalendar(weekmask ='0111100')
print("Number of working days in emergency shift:")
print(emgCalendar.weekmask)

# Number of working days in December for a Doctor who works
# on emergency shift
startDate     = "2025-12-01"
endDate     = "2025-12-31"

workingDaysEmg = numpy.busday_count(startDate, 
                                    endDate, 
                                    busdaycal = emgCalendar)
print("Number of Working days in emergency shift:")
print(workingDaysEmg)

# Check whether Christmas a working day for a Doctor who works on
# Emergency shift
resEmg = numpy.is_busday('2025-07-29', busdaycal = emgCalendar)
print("Is the emergency Doctor working on Christmas day?")
print(resEmg)

 

Output:

Number of working days in emergency shift:
[False  True  True  True  True False False]
Number of Working days in emergency shift:
17
Is the emergency Doctor working on Christmas day?
True

 


Copyright 2025 © pythontic.com