Guey
Guey

Reputation: 73

Calculate the number of working days between two dates by exiling the weekends and specific holidays with python

I have a holidays' list stored in an excel file. I would like to calculate the number of working days between two dates (date_1 and date_2) by ignoring the holidays stored in my excel file.

I tried np.busday_count but I do not know how to include my holiday file. This is my code:

claim=pd.read_excel("claim.xlsx")
Holydays=pd.read_excel("Holidays.xlsx")
claim.columns

claim["Resolved"] = pd.to_datetime(claim["Resolved"]).dt.strftime('%Y-%m-%d')
claim["Received_date"] = pd.to_datetime(claim["Received_date"]).dt.strftime('%Y-%m-%d')

#Je définie mes dates
date_1=claim["Received_date"]
date_2=claim["Resolved"]

#Calcul du nombre de jours entre la date et la date de résolution du dossier
claim["nb_jours"] = np.busday_count(date_1, date_2)

Upvotes: 1

Views: 803

Answers (1)

Georgina Skibinski
Georgina Skibinski

Reputation: 13387

This should do the trick:

import numpy as np

maskcal=np.busdaycalendar(
    holidays=[
         '2019-12-24', 
         '2020-01-01', 
         '2020-01-06', 
         '2020-01-10'
                     ])

diff_days=np.busday_count(
      '2019-12-20', '2020-01-20', 
         busdaycal=maskcal
)

Output:

17

The np.busdaycalendar is comprehensive to weekends (i.e. Saturday-Sunday). You just need to read your excel file, and format it into list of dates of datetime-readable format.

Upvotes: 2

Related Questions