Hi All,
Suppose there is a data frame like below,
df = pd.DataFrame({'Date':['2008-01-31','2008-02-29','2008-03-31','2008-06-30'],'offset':[1, 2, 5, 3]})
df['Date'] = pd.to_datetime(df['Date'])
Date offset
2008-01-31 1
2008-02-29 2
2008-03-31 5
2008-06-30 3
...
I want to shift each date by number of months in the offset column, saying the resulted dataframe looks like this
Date offset offset_date
2008-01-31 1 2008-02-29
2008-02-29 2 2008-04-30
2008-03-31 5 2008-08-31
2008-06-30 3 2008-09-30
...
For this, I can loop through the dates by adding pd.DateOffset(months=offset) + pd.offsets.MonthEnd(0) either using for loop or apply. But I wonder if there is any vectorised way to do this in case of large data frame? I found some solutions using something like numpy.timedelta64['M'], but they cannot produce exact months like the approach with DateOffset. But DateOffset seems not support vectorisation?
Thanks in advance!
Best regards,
James