Mat.S
Mat.S

Reputation: 1854

Business days between two dates excluding holidays in python

I am currently using

np.busday_count(day1, day2))

to count the number of business days between two dates. How do I exclude holidays that are on business days?

Upvotes: 5

Views: 8487

Answers (1)

Akshay Kandul
Akshay Kandul

Reputation: 602

As of v0.14 you can use holiday calendars

import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import CustomBusinessDay
day1 = '2010-01-01'
day2 = '2010-01-15'
us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar())

print(pd.DatetimeIndex(start=day1,end=day2, freq=us_bd))

Output:

DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07',
           '2010-01-08', '2010-01-11', '2010-01-12', '2010-01-13',
           '2010-01-14', '2010-01-15'],
          dtype='datetime64[ns]', freq='C')

To get the count of days use len

print(len(pd.DatetimeIndex(start=day1,end=day2, freq=us_bd)))

Output:

10

Upvotes: 8

Related Questions