lundi 7 décembre 2020

lambda row function with if else statement

I have a pandas dataframe df and an array of datetimes holidays

df.head()

date    hour    count   Relative Humidity   Temperature Precipitation   dow
0   2019-07-01  0   672 57.64   71.8    0.0 Monday
1   2019-07-01  1   359 61.61   70.8    0.0 Monday
2   2019-07-01  2   197 61.63   69.8    0.0 Monday
3   2019-07-01  3   115 63.32   69.0    0.0 Monday
4   2019-07-01  4   168 67.91   67.9    0.0 Monday

df.dtypes

date                  object
hour                   int64
count                  int64
Relative Humidity    float64
Temperature          float64
Precipitation        float64
dow                   object
dtype: object

holidays

[datetime.date(2019, 9, 2), datetime.date(2019, 7, 4)]

My goal is to create a new column that indicates whether or not the date is a workday but the following if else statement throws an error:

df['is_workday'] = df.apply(lambda row: False if (row['dow'] in ('Saturday', 'Sunday') | pd.to_datetime(row['date'],  format='%Y-%m-%d') in holidays) else True)

KeyError: 'dow'

What could be causing this issue?

Aucun commentaire:

Enregistrer un commentaire