Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why the function networkdays get the error "'Series' object has no attribute 'days'

Below is my sample dataframe, I would like to use networkdays to calculate the working day in columns in my df, but it got the issue as below, can someone help assist on this?

#import lib
import pandas as pd
from workdays import workday, networkdays
#sample dict to create df 
dict1 = {
    'startdate' : ['2022-01-17','2022-02-28'],
    'enddate' : ['2022-01-17','2022-03-15']
}
#convert to datetime format
df['startdate'] = df['startdate'].astype('datetime64')
df['enddate'] = df['enddate'].astype('datetime64')
#create new column count and apply function
df['count']=df.apply(networkdays(df['startdate'],df['enddate']))

#getting error :
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_20080/333906513.py in <module>
----> 1 df['count']=df.apply(networkdays(df['startdate'],df['enddate']))

C:\ProgramData\Anaconda3\lib\site-packages\workdays.py in networkdays(start_date, end_date, holidays)
     10 
     11 def networkdays(start_date, end_date, holidays=[]):
---> 12     delta_days = (end_date - start_date).days + 1
     13     full_weeks, extra_days = divmod(delta_days, 7)
     14     # num_workdays = how many days/week you work * total # of weeks

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   5485         ):
   5486             return self[name]
-> 5487         return object.__getattribute__(self, name)
   5488 
   5489     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'days'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['startdate'] is a series object, it contains more than one piece of data.

>> df['enddate'] # <class 'pandas.core.series.Series'>
0   2022-01-17
1   2022-03-15

You should pass a lambda to the df.apply function, if you have to apply a function to all the rows in the DataFrame. The parameter of the lambda is the row of data.

df = pd.DataFrame(dict1)
df['startdate'] = df['startdate'].astype('datetime64')
df['enddate'] = df['enddate'].astype('datetime64')
df['count']=df.apply(lambda x: networkdays(x['startdate'], x['enddate']), axis=1)
print(df)
#    startdate    enddate  count
# 0 2022-01-17 2022-01-17      1
# 1 2022-02-28 2022-03-15     12
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading