Business Days with Custom Holidays

  30 May 2019
  python

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 DateEnd Date
02019-06-032019-08-31
12019-06-132019-06-21
22019-10-012019-10-25
32019-09-012019-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 DateEnd DateBusiness Days
02019-06-032019-08-3164
12019-06-132019-06-217
22019-10-012019-10-2517
32019-09-012019-12-2579

Notebook Link - Business Days with Custom Holidays