You can use the following methods to get business days in pandas:
Method 1: Get Business Days (excludes all weekends)
business_days = pd.bdate_range('2022-01-01', '2022-12-31')
Method 2: Get Business Days (excludes all weekends and Federal holidays)
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import CustomBusinessDay
#define US business days
us_bus = CustomBusinessDay(calendar=USFederalHolidayCalendar())
#get all business days between certain start and end dates
us_business_days = pd.bdate_range('2022-01-01', '2022-12-31', freq=us_bus)
The following examples show how to use each method in practice.
Example 1: Get Business Days (excludes all weekends)
One way to get a list of business days between two dates in pandas is to use the bdate_range() function.
It’s worth noting that this function simply counts the number of days between a start and end date, excluding weekends.
For example, we can use the following syntax to count the number of business days between 1/1/2022 and 12/31/2022:
import pandas as pd
#get all business days between certain start and end dates
business_days = pd.bdate_range('2022-01-01', '2022-12-31')
#view first ten business days
print(business_days[0:10])
DatetimeIndex(['2022-01-03', '2022-01-04', '2022-01-05', '2022-01-06',
'2022-01-07', '2022-01-10', '2022-01-11', '2022-01-12',
'2022-01-13', '2022-01-14'],
dtype='datetime64[ns]', freq='B')
#view total number of business days
len(business_days)
260
The object called business_days contains every business day between the specified start and end dates.
And by using the len() function, we see that the total number of business days between the specified start and end dates is 260.
Example 2: Get Business Days (excludes all weekends and Federal holidays)
To get a list of business days between two dates in pandas that excludes both weekends and Federal holidays, we must use functions from the pandas tseries module.
For example, we can use the following syntax to count the number of business days (all days excluding weekends and Federal holidays) between 1/1/2022 and 12/31/2022:
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import CustomBusinessDay
#define US business days
us_bus = CustomBusinessDay(calendar=USFederalHolidayCalendar())
#get all business days between certain start and end dates
us_business_days = pd.bdate_range('2022-01-01', '2022-12-31', freq=us_bus)
#view first ten business days
print(us_business_days[0:10])
DatetimeIndex(['2022-01-03', '2022-01-04', '2022-01-05', '2022-01-06',
'2022-01-07', '2022-01-10', '2022-01-11', '2022-01-12',
'2022-01-13', '2022-01-14'],
dtype='datetime64[ns]', freq='C')
#view total number of business days
len(us_business_days)
250
The object called us_business_days contains every business day (all days excluding weekends and Federal holidays) between the specified start and end dates.
And by using the len() function, we see that the total number of business days between the specified start and end dates is 250.
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
How to Add and Subtract Days from a Date in Pandas
How to Convert Datetime to Date in Pandas
How to Extract Month from Date in Pandas