0

I have to add 'N' no.of months in the date column and was trying to use this function to help me to do so.

order_emis_full['calc_due_date']=order_emis_full['agreement_date'].apply(lambda  x: x + relativedelta.relativedelta(months=1))  

I keep getting the error

 IllegalMonthError: bad month number nan; must be 1-12
languitar
  • 6,554
  • 2
  • 37
  • 62
  • Please show your input data frame. – languitar Apr 13 '17 at 10:39
  • You either have a string or `NaN` so do `order_emis_full['calc_due_date'].fillna(0, inplace=True)` or `order_emis_full['calc_due_date'].replace('nan',0, inplace=True)` – EdChum Apr 13 '17 at 10:40

1 Answers1

0

Without much context, this is something I would do to catch the error and supply a backup number

from calendar import IllegalMonthError

try:
   order_emis_full['calc_due_date']=order_emis_full['agreement_date'].apply(lambda  x: x + relativedelta.relativedelta(months=1))

except IllegalMonthError:
   'Your error catching code'

You could also add the try except statement in your pandas apply function so that you can control what value to be backfilled whenever there is IllegalMonthError exception

J. Spark
  • 128
  • 6