0
0
PythonHow-ToBeginner · 3 min read

How to Find Number of Working Days Between Dates in Python

To find the number of working days between two dates in Python, use numpy.busday_count which counts weekdays excluding weekends. Alternatively, use pandas.bdate_range to generate business days and count them.
📐

Syntax

The main function to count working days is numpy.busday_count(start_date, end_date). It counts weekdays (Monday to Friday) between two dates, excluding the end date. You can also specify holidays to exclude.

Another way is using pandas.bdate_range(start, end) which creates a range of business days including the end date.

python
import numpy as np

# Count weekdays between two dates (end date excluded)
working_days = np.busday_count('2024-06-01', '2024-06-10')

import pandas as pd

# Generate business days including end date
business_days = pd.bdate_range('2024-06-01', '2024-06-10')
count_business_days = len(business_days)
💻

Example

This example shows how to calculate the number of working days between two dates using numpy.busday_count and pandas.bdate_range. It also demonstrates excluding holidays.

python
import numpy as np
import pandas as pd

start_date = '2024-06-01'
end_date = '2024-06-10'
holidays = ['2024-06-05']  # Example holiday

# Using numpy to count working days excluding weekends and holidays
working_days_np = np.busday_count(start_date, end_date, holidays=holidays)

# Using pandas to generate business days and count them
business_days_pd = pd.bdate_range(start_date, end_date).difference(pd.to_datetime(holidays))
working_days_pd = len(business_days_pd)

print(f"Working days (numpy): {working_days_np}")
print(f"Working days (pandas): {working_days_pd}")
Output
Working days (numpy): 6 Working days (pandas): 6
⚠️

Common Pitfalls

  • Not excluding the end date when using numpy.busday_count, which counts days up to but not including the end date.
  • Forgetting to specify holidays, which can cause incorrect counts if holidays fall on weekdays.
  • Using pandas.date_range instead of bdate_range, which includes weekends.
python
import numpy as np

# Wrong: end date included (incorrect for busday_count)
wrong_count = np.busday_count('2024-06-01', '2024-06-11')  # Includes an extra day

# Correct: end date excluded
correct_count = np.busday_count('2024-06-01', '2024-06-10')

print(f"Wrong count: {wrong_count}")
print(f"Correct count: {correct_count}")
Output
Wrong count: 7 Correct count: 6
📊

Quick Reference

  • numpy.busday_count(start, end, holidays=None): Counts weekdays between start and end (end excluded).
  • pandas.bdate_range(start, end): Generates business days including end.
  • Specify holidays to exclude specific dates.
  • Weekends are Saturday and Sunday by default.

Key Takeaways

Use numpy.busday_count to count weekdays between two dates excluding the end date.
Use pandas.bdate_range to generate business days including the end date and count them.
Always exclude holidays to get accurate working day counts.
Remember numpy.busday_count excludes the end date, so adjust accordingly.
Avoid using pandas.date_range when you want only working days; use bdate_range instead.