Given data
Id | start Date | Frequency |
---|---|---|
1 | 10-10-2015 | 1 |
2 | 20–10-2016 | 2 |
I required in this format
Id | start Date | Frequency | Date1 | Dat2 | Date3 | Date4 |
---|---|---|---|---|---|---|
1 | 10-10-2015 | 1 | 10-10-2016 | 10-10-2017 | 10-10-2018 | |
2 | 20–10-2016 | 2 | 20-04-2017 | 20-10-2017 | 20-04-2018 | 20-10-2018 |
If the frequency is 1 Date increments by 1 year and should not exceed 2018 year. If the frequency is 2 Date increments by 6 month and should not exceed 2018 year.
CodePudding user response:
You could apply
pd.date_range
on each row to create your required dates and join
back to the original DataFrame:
df["start Date"] = pd.to_datetime(df["start Date"], format="%d-%m-%Y")
dates = df.apply(lambda x: pd.Series(pd.date_range(x["start Date"],
"2018-12-31",
freq=pd.DateOffset(months=12/x["Frequency"]))
).add_prefix("Date"), axis=1)
output = df.join(dates.iloc[:,1:])
>>> output
id start Date Frequency Date1 Date2 Date3 Date4
0 1 2015-10-10 1 2016-10-10 2017-10-10 2018-10-10 NaT
1 2 2016-10-20 2 2017-04-20 2017-10-20 2018-04-20 2018-10-20