I have a pandas dataframe as below:
import pandas as pd
df = pd.DataFrame({'date1':['12/1/2019','12/1/2019','12/1/2019'], 'nb_months':[11,11,12]})
df['date1'] = pd.to_datetime(df['date1'], format='%m/%d/%Y', errors='coerce').dropna()
df
date1 nb_months
0 2019-12-01 11
1 2019-12-01 11
2 2019-12-01 12
I want to add nb_months to date1 column. I did try below code but the output is not what I am expecting.The below code gives me 1 prior date as an output(for example for 1st row: date3 should be 2019-12-01 + 11 months = 2020-11-01 whereas am getting 2020-10-31.
df['date3'] = df.apply(lambda x: x['date1'] + pd.offsets.MonthEnd(x['nb_months']), axis=1)
df
date1 nb_months date3
0 2019-12-01 11 2020-10-31
1 2019-12-01 11 2020-10-31
2 2019-12-01 12 2020-11-30
My expected output:
date1 nb_months date3
0 2019-12-01 11 2020-11-01
1 2019-12-01 11 2020-11-01
2 2019-12-01 12 2020-12-01