Sathya Bhat
Sathya Bhat

Reputation: 35

Why do I get this error? TypeError: unsupported operand type(s) for +: 'TimedeltaIndex' and 'relativedelta' when adding two dates

I am trying to create a new column in my data frame by adding 6 months to the date column.

df_main['m1'] = df_main['date'] + relativedelta(months=6)

df_main['date'] is of the format datetime64[ns] I tried converting it to timedelta64[ns] still doesn't work.

Example df_main['date'] = 2019-04-01

Upvotes: 2

Views: 396

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169414

Instead do:

df['x'].dt.date + dateutil.relativedelta.relativedelta(months=6)

Or a more long-winded -- and slower -- way:

df_main['m1'] = df_main['date'].apply(lambda x: x + relativedelta(months=6))

Upvotes: 2

Related Questions