Finding the Number of Weekdays of a Given Month in NumPy

NumPy is a powerful Python library for numerical computing and data analysis. When working with date calculations, you often need to count weekdays (business days) within a specific month, excluding weekends and holidays.

You can install NumPy using pip ?

pip install numpy

NumPy's busday_count() function calculates the number of business days (Monday to Friday) between two specific dates.

Syntax

numpy.busday_count(startdate, enddate, weekmask='1111100', holidays=None)

Parameters

  • startdate Start date (inclusive) in "YYYY-MM-DD" format

  • enddate End date (exclusive) in "YYYY-MM-DD" format

  • weekmask Optional string defining valid weekdays (default: Monday-Friday)

  • holidays Optional array of holiday dates to exclude

Return Value

Returns an integer representing the number of valid business days.

Example 1: Basic Weekday Count

Calculate weekdays in February 2023 ?

import numpy as np

# Count weekdays in February 2023
num_days = np.busday_count('2023-02-01', '2023-03-01')

print(f"There are {num_days} weekdays in February 2023")
There are 20 weekdays in February 2023

Example 2: Leap Year Calculation

Calculate weekdays in February 2024 (leap year) with explicit weekmask ?

import numpy as np

# Count weekdays in February 2024 (leap year)
num_days = np.busday_count('2024-02-01', '2024-03-01', 
                          weekmask='Mon Tue Wed Thu Fri')

print(f"There are {num_days} weekdays in February 2024")
There are 21 weekdays in February 2024

Example 3: Excluding Holidays

Count weekdays while excluding national holidays ?

import numpy as np

# Define holidays in January 2023
holidays = ['2023-01-01', '2023-01-26']

# Count weekdays excluding holidays
num_days = np.busday_count('2023-01-01', '2023-02-01', holidays=holidays)

print(f"Weekdays in January 2023 excluding holidays: {num_days}")

# Compare with total weekdays (no holidays excluded)
total_weekdays = np.busday_count('2023-01-01', '2023-02-01')
print(f"Total weekdays without excluding holidays: {total_weekdays}")
Weekdays in January 2023 excluding holidays: 21
Total weekdays without excluding holidays: 22

How It Works

The busday_count() function only excludes holidays that fall on weekdays. Since January 1st, 2023 was a Sunday, it wasn't counted as a weekday anyway. Only January 26th (Republic Day, which fell on a Thursday) was excluded from the count.

Custom Weekmask Example

Define custom working days (e.g., Tuesday to Saturday) ?

import numpy as np

# Custom weekmask: Tuesday to Saturday
custom_days = np.busday_count('2023-03-01', '2023-04-01', 
                             weekmask='Tue Wed Thu Fri Sat')

print(f"Tuesday-Saturday working days in March 2023: {custom_days}")
Tuesday-Saturday working days in March 2023: 22

Comparison of Methods

Parameter Default Value Usage
weekmask '1111100' (Mon-Fri) Standard business days
holidays None Exclude specific dates
Custom weekmask User-defined Non-standard work weeks

Conclusion

NumPy's busday_count() function efficiently calculates weekdays between dates, making it invaluable for business applications like payroll, project management, and financial calculations. It handles leap years, holidays, and custom work schedules seamlessly.

Updated on: 2026-03-27T13:20:52+05:30

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements