NumPy: Find the number of weekdays in March 2017
Write a NumPy program to find the number of weekdays in March 2017.
Note: "busday" default of Monday through Friday being valid days.
Sample Solution:
Python Code:
# Importing the required libraries
import numpy as np
# Finding the number of weekdays in March 2017 using numpy's busday_count function
# '2017-03' indicates the start of the period (March 2017)
# '2017-04' indicates the end of the period (April 2017), but this day is not counted
# The function calculates the number of weekdays (excluding weekends) between the provided dates
print("Number of weekdays in March 2017:")
print(np.busday_count('2017-03', '2017-04'))
Sample Output:
Number of weekdays in March 2017: 23
Explanation:
The above NumPy exercise returns the number of business days between the start and end dates (inclusive) excluding weekends and any holidays that fall in-between.
np.busday_count('2017-03', '2017-04'):
In the above code –
- np.busday_count() function is used to calculate the number of business days between two dates.
- The first argument '2017-03' is the start date, and the second argument '2017-04' is the end date (exclusive).
- The function returns the number of business days between these two dates, which is the number of weekdays excluding any holidays that occur in between.
- In this case, it returns 23 because March 2017 has 23 weekdays
Pictorial Presentation:
Python-Numpy Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics