Reputation: 185
df:
End_Dt_Id Start_Date End_Date
18AUG19 12-08-2019 18-08-2019 -- date format DD/MM/YYYY
25AUG19 19-08-2019 25-08-2019
01SEP19 26-08-2019 01-09-2019
08SEP19 02-09-2019 08-09-2019
15SEP19 09-09-2019 15-09-2019
22SEP19 16-09-2019 22-09-2019
12SEP19 23-09-2019 29-09-2019
I need to add 4 more weeks to my dataframe which will depend upon last week in my df, for example the last week would be this row "29SEP19 23-09-2019 29-09-2019" because of max(End_Date), there after i need to add four more weeks as shown in the output.
df_output:
End_Dt_Id Start_Date End_Date
18AUG19 12-08-2019 18-08-2019 -- date format DD/MM/YYYY
25AUG19 19-08-2019 25-08-2019
01SEP19 26-08-2019 01-09-2019
08SEP19 02-09-2019 08-09-2019
15SEP19 09-09-2019 15-09-2019
22SEP19 16-09-2019 22-09-2019
29SEP19 23-09-2019 29-09-2019
05OCT19 30-09-2019 05-10-2019
12OCT19 06-10-2019 12-10-2019
19OCT19 13-10-2019 19-10-2019
26OCT19 20-10-2019 26-10-2019
Upvotes: 0
Views: 52
Reputation: 1357
I'm not sure that I understand the logic, but this provides the output you are looking for:
last_row = df.sort_values('End_Date')[-1:]
new_rows = []
start_date = last_row['Start_Date'].iloc[0]
end_date = last_row['End_Date'].iloc[0]
for i in range(1,5):
start_date = end_date + dt.timedelta(days=1)
end_date = end_date + dt.timedelta(days=7)
end_dt_id = dt.datetime.strftime(end_date, '%d%b%y').upper()
new_rows.append([end_dt_id,start_date, end_date])
df = pd.concat([
df,
pd.DataFrame(new_rows, columns=['End_Dt_Id', 'Start_Date', 'End_Date'])
])
for col in ['Start_Date', 'End_Date']:
df[col] = pd.to_datetime(df[col]).dt.strftime('%d-%m-%Y')
Returns:
End_Dt_Id Start_Date End_Date
18AUG19 12-08-2019 18-08-2019
25AUG19 19-08-2019 25-08-2019
01SEP19 26-08-2019 01-09-2019
08SEP19 02-09-2019 08-09-2019
15SEP19 09-09-2019 15-09-2019
22SEP19 16-09-2019 22-09-2019
12SEP19 23-09-2019 12-09-2019
19SEP19 13-09-2019 19-09-2019
26SEP19 20-09-2019 26-09-2019
03OCT19 27-09-2019 03-10-2019
10OCT19 04-10-2019 10-10-2019
Upvotes: 1