I have a data frame where I'm trying to add a new column based on the condition that adds 3 months to a DateTime.
ID1 ID2 Date
1 20 5/15/2019 11:06:47 AM
1 21 5/15/2019 11:06:47 AM
1 22 6/15/2019 11:06:47 AM
2 30 7/15/2019 11:06:47 AM
2 31 7/15/2019 11:06:47 AM
2 32 7/15/2019 11:06:47 AM
Required Output,
ID1 ID2 Date NewDate
1 20 5/15/2019 11:06:47 AM 8/15/2019 11:06:47 AM
1 21 5/15/2019 11:06:47 AM 9/15/2019 11:06:47 AM
1 22 6/15/2019 11:06:47 AM 10/15/2019 11:06:47 AM
2 30 7/15/2019 11:06:47 AM 10/15/2019 11:06:47 AM
2 31 7/15/2019 11:06:47 AM 11/15/2019 11:06:47 AM
2 32 7/15/2019 11:06:47 AM 12/15/2019 11:06:47 AM
For each ID1, there can be only one unique NewDate. If there exists a date that may fall in the same month, then add another month.
For ID1, having a different Date, if the NewDate falls on a month similar to previous NewDate, then we add another additional DateOffset as seen in Row 3 of the required output
I have tried the following code,
def add_date(df):
for each_ID1 in df['ID1']:
for each_ID2 in df['ID2']:
return df['Date'] + DateOffset(months = 3)
df['New Date'] = df.apply(add_date, axis = 1)
My code gives me the 3-month DateOffset only as shown,
ID1 ID2 Date NewDate
1 20 5/15/2019 11:06:47 AM 8/15/2019 11:06:47 AM
1 21 5/15/2019 11:06:47 AM 8/15/2019 11:06:47 AM
1 22 6/15/2019 11:06:47 AM 9/15/2019 11:06:47 AM
2 30 7/15/2019 11:06:47 AM 10/15/2019 11:06:47 AM
2 31 7/15/2019 11:06:47 AM 10/15/2019 11:06:47 AM
2 32 7/15/2019 11:06:47 AM 10/15/2019 11:06:47 AM
Output Error
ID1 ID2 Date NewDate
1 20 5/15/2019 11:06:47 AM 8/15/2019 11:06:47 AM
1 21 5/15/2019 11:06:47 AM 9/15/2019 11:06:47 AM
1 22 5/15/2019 11:06:47 AM 10/15/2019 11:06:47 AM
1 23 5/15/2019 11:06:47 AM 11/15/2019 11:06:47 AM
1 24 5/15/2019 11:06:47 AM 12/15/2020 11:06:47 AM
1 25 5/15/2019 11:06:47 AM 01/15/2021 11:06:47 AM
1 26 6/15/2019 11:06:47 AM 10/15/2019 11:06:47 AM
1 27 6/15/2019 11:06:47 AM 12/15/2019 11:06:47 AM
1 28 6/15/2019 11:06:47 AM 02/15/2020 11:06:47 AM
1 29 6/15/2019 11:06:47 AM 04/15/2020 11:06:47 AM
1 30 6/15/2019 11:06:47 AM 06/15/2020 11:06:47 AM
1 31 6/15/2019 11:06:47 AM 07/15/2020 11:06:47 AM