Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Business days between two dates excluding holidays in python

Tags:

python

date

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?

like image 605
Mat.S Avatar asked Mar 08 '23 17:03

Mat.S


1 Answers

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
like image 109
Akshay Kandul Avatar answered Apr 03 '23 01:04

Akshay Kandul