Search code examples
pythonpandasdataframedatetimerelativedelta

TypeError: unsupported operand type(s) for +: 'DatetimeArray' and 'relativedelta'


I am trying to convert a column called Month_Next from a dataframe called df_actual from the last day of one month to the first day of the next. The column looks like this:

enter image description here

And I'm using

df_actual.Month_Next = pd.to_datetime(df_actual.Month_Next) + relativedelta(months=1, day=1)

and getting this error.

TypeError: unsupported operand type(s) for +: 'DatetimeArray' and 'relativedelta'

Which makes no sense to me since this exact code works in a different notebook where Month_Next comes in as I believe a Timestamp object like so

enter image description here

Any ideas as to what's going on here?


Solution

  • This works just fine:

    Month_Next = df_actual.AsOfDate + pd.DateOffset(months =1) - pd.offsets.MonthBegin(1)