# Finding the Number of Weekdays of a Given Month in NumPy

Numpy is an open-source library in Python used for numerical computing, scientific computing, data analysis, and machine learning applications. This library has powerful array methods and tools to perform vector and matrix operations.

You must install the NumPy module manually since it is not a component of the default Python library. The Python package manager pip can be used to accomplish this by executing the following command −

pip install numpy


We will use NumPy’s busday_count() function to calculate the required count of weekdays. This will calculate the number of business days (Monday to Friday) between 2 specific dates.

## Syntax

numpy.busday_count(startdate, enddate, weekmask, holidays)


Return Value − Integer (Number of days)

• Startdate − Date range (inclusive) for which you want to count the number of valid days. This should be a string in the format "YYYY-MM-DD"

• Enddate − date range (exclusive) for which you want to count the number of valid days. This should also be a string in the format "YYYY-MM-DD".

• Weekmask − this is an optional string parameter that represents the days of the week. By default, this is set to Monday to Friday.

• Holidays − An array of date strings, marked as holidays and to be excluded from the count. This is also an optional parameter.

## Example 1

The following example calculates the number of weekdays(Monday to Friday) in a given month using the NumPy and datetime module.

## Algorithm

• Import required numpy library.

• Pass startdate and enddate parameters to the busday_count() function.

• Print the no. of weekdays.

import numpy as np

# get the number of days in the month
num_days = np.busday_count('2023-02-01', '2023-03-01')

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


## Output

There are 20 weekdays in 2023 February


## Example 2

The following example calculates the no. of weekdays in February, provided it is a leap year. We consider the year 2024 which is a leap year.

import numpy as np

# get the number of days in a leap year month
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 2024 February")


## Output

There are 21 weekdays in  2024 February


## Example 3

The following example counts the number of weekdays excluding the national holidays.

## Algorithm

• Import numpy library.

• Create a list of holidays in January month.

• Calculate no. of weekdays and store the value in num_days variable and pass the holidays list along with the function.

• Print the values.

import numpy as np
holidays = ['2023-01-01', '2023-01-26']

# get the number of days in the month
num_days = np.busday_count('2023-01-01', '2023-02-01', holidays = holidays)

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


## Output

Weekdays in January 2023 excluding the national holidays are: 21


Note − Without excluding the national holidays, the no. of weekdays are 22. Since 1st January is on Sunday, it is not counted as a weekday and hence 21 weekdays and not (22-2=20) 20 weekdays.

The holidays parameter would take either string or list of dates as its value. It also checks if the national holiday specified is a weekday or not.

## Conclusion

busday_count() function is useful for applications that require the calculation of business days between 2 dates. It can be efficiently used in interest calculations, bond duration periods in banks or even trading periods in the stock market. It can also be used in project management applications to calculate the number of working days needed to complete a task or project.

Updated on: 23-Aug-2023

75 Views