I have a date field called date1 and a weekday field called weekday in a dataframe called df. I am trying to create another field that if the weekday is a certain value, add a year,otherwise add a day. It comes up with an error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
So I added .any() and it runs but adds 365 days to all values.
if df['weekday'].any() < 6:
df['date1'] = df['date'] + timedelta(365)
elif df['weekday'].any() == 6:
df['date1'] = df['date'] + timedelta(1)
else:
df['date1'] = datetime.today()
I am learning python and searched extensively on the Q&As here so would appreciate guidance. Thanks
This errors comes when you compare series with one element. Try this way-
for i, value in enumerate(df['weekday']):
if value < 6:
df['date1'][i] = df['date'][i] + timedelta(365)
elif value == 6:
df['date1'][i] = df['date'][i] + timedelta(1)
else:
df['date1'][i] = datetime.today()
Hope this will help you.