I'm trying to create an additional column in a data frame to show the number of network days (excluding custom holidays) between two dates. I'm using a function to which I'm trying to pass dates from df
's columns as arguments, but I can't make it work.
Below is my code (I'm using two made-up holidays in the given set):
from networkdays import networkdays
import datetime as dt
import numpy as np
import pandas as pd
public_holidays_list = [dt.date(2021, 1, 6), dt.date(2021, 1, 7)]
public_holidays = set(public_holidays_list)
def working_days(start, end, holidays):
days = networkdays.Networkdays(start, end, holidays)
working_days = len(days.networkdays())
return working_days
The formula itself works fine:
print(working_days(dt.date(2021, 1, 4), dt.date(2021, 1, 8), public_holidays))
3
Minimal data frame with the dtypes
I'm working on:
d = {'Name': ['A', 'B'], 'Start_Date': [dt.date(2021, 1, 4), dt.date(2021, 1, 11)], 'End_Date': [dt.date(2021, 1, 8), dt.date(2021, 1, 15)]}
df = pd.DataFrame(data = d)
df['Start_Date'] = pd.to_datetime(df['Start_Date'])
df['End_Date'] = pd.to_datetime(df['End_Date'])
When I'm trying the below way...
df['Working_Days'] = working_days(df['Start_Date'], df['End_Date'])
...I'm getting an error:
AttributeError: 'Series' object has no attribute 'days'
I've also tried to use numpy
:
df['Working_Days'] = np.vectorize(working_days)(df['Start_Date'], df['End_Date'])
I got an error as well:
AttributeError: 'numpy.timedelta64' object has no attribute 'days'
Could you point me in the right direction?