I have a pandas dataframe that has a date field. I'm trying to use this field to calculate the number of days in the month of that date. However, when I use the code below, I get the following error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
from calendar import monthrange
df['NumDays'] = monthrange(df['Date_Field'].map(lambda x: x.year),df['Date_Field'].map(lambda x: x.month))[1]
I've tested to see if the .year and .month are working by creating two separate columns in the DF with that data.
df['year'] = df['Date_Field'].map(lambda x: x.year)
df['month'] = df['Date_Field'].map(lambda x: x.month)
df['NumDays'] = monthrange(df['year'], df['month'])[1]
The resulting 'year' column had the correct year, and the 'month' column had the correct month, but when I tried to use them in monthrange() I get the same ValueError.
Any guidance would be appreciated.
Thanks.