Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a Python function to calculate the total number of business days from a range of start and end date
Business days are weekdays (Monday through Friday), excluding weekends and holidays. Python's Pandas library provides several methods to calculate business days between two dates.
Understanding Business Days
First, let's see what business days look like in a date range ?
import pandas as pd
dates = pd.bdate_range('2020-01-01', '2020-01-31')
print("Business days in January 2020:")
print(dates)
print(f"Total business days: {len(dates)}")
Business days in January 2020:
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-06',
'2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10',
'2020-01-13', '2020-01-14', '2020-01-15', '2020-01-16',
'2020-01-17', '2020-01-20', '2020-01-21', '2020-01-22',
'2020-01-23', '2020-01-24', '2020-01-27', '2020-01-28',
'2020-01-29', '2020-01-30', '2020-01-31'],
dtype='datetime64[ns]', freq='B')
Total business days: 23
Method 1: Using bdate_range() with len()
The most efficient approach uses Pandas' bdate_range() function ?
import pandas as pd
def count_business_days(start_date, end_date):
dates = pd.bdate_range(start_date, end_date)
return len(dates)
# Example usage
start = '2020-01-01'
end = '2020-02-02'
business_days = count_business_days(start, end)
print(f"Business days from {start} to {end}: {business_days}")
Business days from 2020-01-01 to 2020-02-02: 23
Method 2: Using Manual Counting
You can also count business days by iterating through the date range ?
import pandas as pd
def count_business_days_manual(start_date, end_date):
dates = pd.bdate_range(start_date, end_date)
count = 0
for date in dates:
count += 1
return count
# Example usage
start = '2020-01-01'
end = '2020-02-02'
business_days = count_business_days_manual(start, end)
print(f"Business days from {start} to {end}: {business_days}")
Business days from 2020-01-01 to 2020-02-02: 23
Method 3: Using NumPy's busday_count()
NumPy also provides a direct function to count business days ?
import numpy as np
def count_business_days_numpy(start_date, end_date):
return np.busday_count(start_date, end_date)
# Example usage
start = '2020-01-01'
end = '2020-02-02'
business_days = count_business_days_numpy(start, end)
print(f"Business days from {start} to {end}: {business_days}")
Business days from 2020-01-01 to 2020-02-02: 22
Comparison
| Method | Function | Performance | Includes End Date |
|---|---|---|---|
| Pandas bdate_range | pd.bdate_range() |
Fast | Yes |
| Manual counting | Loop iteration | Slower | Yes |
| NumPy busday_count | np.busday_count() |
Fastest | No |
Conclusion
Use pd.bdate_range() with len() for most business day calculations. For maximum performance with large date ranges, consider np.busday_count(), but note it excludes the end date.
Advertisements
