Pandas: Get the Business Days between two Dates

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Pandas: Get the Business Days between two Dates
  2. Pandas: Get the Business Days between two Dates excluding National Holidays
  3. Pandas: Get the Business Days between two Dates using pandas.date_range

# Pandas: Get the Business Days between two Dates

Use the pandas.bdate_range() method to get the business days between two dates in Pandas.

The method returns a fixed frequency DateTimeIndex with business day as the default.

main.py
import pandas as pd date1 = '2023-08-16' date2 = '2023-08-21' business_days = pd.bdate_range(start=date1, end=date2) # DatetimeIndex(['2023-08-16', '2023-08-17', '2023-08-18', '2023-08-21'], dtype='datetime64[ns]', freq='B') print(business_days) print(len(business_days)) # ๐Ÿ‘‰๏ธ 4

pandas get business days between two dates

The code for this article is available on GitHub

If you need to get the number of business days between 2 dates, use the len() function as shown in the code sample.

The pandas.bdate_range() method returns a fixed DatetimeIndex containing the business days between the two dates.

By default, the start and end dates are inclusive.

This can be changed by using the inclusive keyword argument when calling pandas.bdate_range().

The values for the inclusive argument are:

  • "both" - includes the start and end dates. This is the default value.
  • "neither" - excludes the start and end dates.
  • "left" - includes the start date but excludes the end date.
  • "right" - excludes the start date but includes the end date.

Here is an example of setting the argument to "neither".

main.py
import pandas as pd date1 = '2023-08-16' date2 = '2023-08-21' business_days = pd.bdate_range( start=date1, end=date2, inclusive='neither' ) # DatetimeIndex(['2023-08-17', '2023-08-18'], dtype='datetime64[ns]', freq='B') print(business_days) print(len(business_days)) # ๐Ÿ‘‰๏ธ 2

excluding the start and end dates

The code for this article is available on GitHub

Notice that the start and end dates are excluded from the range.

When using this approach, we consider business days to be Monday through Friday (we exclude weekends).

However, this approach doesn't exclude national holidays.

# Pandas: Get the Business Days between two Dates excluding National Holidays

If you want to get the business days between two dates excluding the national holidays, use the Custom business days classes.

main.py
import pandas as pd from pandas.tseries.holiday import USFederalHolidayCalendar from pandas.tseries.offsets import CustomBusinessDay date1 = '2023-11-20' date2 = '2023-11-24' us_business_days = CustomBusinessDay(calendar=USFederalHolidayCalendar()) business_days = pd.bdate_range( start=date1, end=date2, freq=us_business_days ) # DatetimeIndex(['2023-11-20', '2023-11-21', '2023-11-22', '2023-11-24'], dtype='datetime64[ns]', freq='C') print(business_days) print(len(business_days)) # ๐Ÿ‘‰๏ธ 4
The code for this article is available on GitHub

The USFederalHolidayCalendar class initializes a holiday object with a given set of rules.

The CustomBusinessDay class provides a parametric BusinessDay class that can be used to create customized business day calendars that take local holidays and local weekend conventions into consideration.

Notice that the output doesn't include November the 23rd (Thanksgiving)

main.py
# DatetimeIndex(['2023-11-20', '2023-11-21', '2023-11-22', '2023-11-24'], dtype='datetime64[ns]', freq='C') print(business_days)

If you want to get the number of business days in the range, use the len() function.

main.py
print(len(business_days)) # ๐Ÿ‘‰๏ธ 4

You can read more about using the CustomBusinessDay class in this section of the docs.

# Pandas: Get the Business Days between two Dates using pandas.date_range

You can also use the pandas.date_range method to get the business days between two dates.

  1. Use the pandas.date_range() method, passing it the two dates as parameters.
  2. Set the freq argument to "B" to select only the business days.
main.py
import pandas as pd date1 = '2023-09-26' date2 = '2023-09-30' business_days = pd.date_range(date1, date2, freq='B') # DatetimeIndex( # ['2023-09-26', '2023-09-27', '2023-09-28', '2023-09-29'], dtype='datetime64[ns]', freq='B') print(business_days) print(len(business_days)) # ๐Ÿ‘‰๏ธ 4

get business days between two dates using pandas

The code for this article is available on GitHub
Note that the start and end dates are inclusive.

The pandas.date_range() method returns a fixed frequency DatetimeIndex.

The first argument we passed to the method is the start date and the second is the end date.

The freq argument is the frequency of the generated dates.

main.py
business_days = pd.date_range(date1, date2, freq='B') # DatetimeIndex( # ['2023-09-26', '2023-09-27', '2023-09-28', '2023-09-29'], dtype='datetime64[ns]', freq='B') print(business_days)

If you need to get the number of business days between two dates, use the len() function.

main.py
print(len(business_days)) # ๐Ÿ‘‰๏ธ 4

The len() function returns the length (the number of items) of an object.

The freq argument must be set to an offset alias.

By default, the argument is set to "D" (calendar day frequency).

However, we only want the business days between the two dates, so we set the argument to "B" (business day frequency).

You can view all of the available offset aliases in this section of the docs.

The 30th of September is a Saturday (not a business day), so it wasn't included in the list.

When using this approach, we consider business days to be Monday through Friday (we exclude weekends).

However, this approach doesn't exclude national holidays.

main.py
import pandas as pd date1 = '2023-11-20' date2 = '2023-11-24' business_days = pd.date_range(date1, date2, freq='B') # DatetimeIndex( # ['2023-11-20', '2023-11-21', '2023-11-22', '2023-11-23', # '2023-11-24'], # dtype='datetime64[ns]', freq='B') print(business_days)

this approach does not exclude national holidays

The code for this article is available on GitHub

Notice that the list contains The 23rd of November (Thanksgiving).

I've also written an article on how to check if a date is during the weekend in Pandas.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev