I have a dataframe with the date and month_diff variable. I would like to get a new date (name it as Target_Date) based on the following logic: For example, the date is 2/13/2019, month_diff is 3, then the target date should be the month-end of the original date plus 3 months, which is 5/31/2019
I tried the following method to get the traget date first:
df["Target_Date"] = df["Date"] + pd.DateOffset(months = df["month_diff"])
But it failed, as I know, the parameter in the dateoffset should be a varaible or a fixed number.
I also tried:
df["Target_Date"] = df["Date"] + relativedelta(months = df["month_diff"])
It failes too.
Anyone can help? thank you.
edit: this is a large dataset with millions rows.
I was looking for a solution I can write in one line only and
apply
does the job. However, by defaultapply
function performs action on each column, so you have to remember to specify correct axis:axis=1
.