Importing Packages and Datasets
import pandas as pd
start_date = ['2019-06-03', '2019-06-13', '2019-10-01', '2019-09-01']
end_date = ['2019-08-31', '2019-06-21', '2019-10-25', '2019-12-25']
data = pd.DataFrame(list(zip(start_date,end_date)), columns = ['Start Date', 'End Date'])
data
Start Date | End Date | |
---|---|---|
0 | 2019-06-03 | 2019-08-31 |
1 | 2019-06-13 | 2019-06-21 |
2 | 2019-10-01 | 2019-10-25 |
3 | 2019-09-01 | 2019-12-25 |
Custm Holidays List
holiday_dates = [pd.datetime(2019, 8, 15), pd.datetime(2019, 10, 2), pd.datetime(2019, 10, 8),
pd.datetime(2019, 10, 28), pd.datetime(2019, 12, 25)]
Example - calculate business days (excluding weekends) with custom holidays
# Exclude weekends and custom holidays
pd.bdate_range(pd.datetime(2019, 8, 1), pd.datetime(2019, 8, 31), holidays=holiday_dates, freq='C', weekmask = None)
DatetimeIndex(['2019-08-01', '2019-08-02', '2019-08-05', '2019-08-06',
'2019-08-07', '2019-08-08', '2019-08-09', '2019-08-12',
'2019-08-13', '2019-08-14', '2019-08-16', '2019-08-19',
'2019-08-20', '2019-08-21', '2019-08-22', '2019-08-23',
'2019-08-26', '2019-08-27', '2019-08-28', '2019-08-29',
'2019-08-30'],
dtype='datetime64[ns]', freq='C')
Applying the same using lambda
data['Business Days'] = data.apply(lambda x: len(pd.bdate_range(x['Start Date'],
x['End Date'],
holidays=holiday_dates,
freq='C',
weekmask = None)), axis = 1)
data
Start Date | End Date | Business Days | |
---|---|---|---|
0 | 2019-06-03 | 2019-08-31 | 64 |
1 | 2019-06-13 | 2019-06-21 | 7 |
2 | 2019-10-01 | 2019-10-25 | 17 |
3 | 2019-09-01 | 2019-12-25 | 79 |
Notebook Link - Business Days with Custom Holidays