the first row of my contract 144 starts at 2008-02-11 and ends at 2011-03-28. I want to calculate the anniversary date for each year between 2008 and 2011 (For 144 and others contracts)
the main goal is to check for each row of contract if the anniversary date its ok, if its not calculate it and update the start and the end with the right values
*this is my pandas dataframe which concerns two contracts "144" and "150":
| index | NUM_contrat | start | end | anniversary | quantity |
|---|---|---|---|---|---|
| 0 | 144 | 2008-02-11 | 2011-03-28 | 2009-02-11 | 550 |
| 1 | 144 | 2011-03-28 | 2011-09-19 | 2012-02-11 | 550 |
| 2 | 150 | 2011-09-19 | 2012-02-10 | 2012-09-19 | 900 |
| 3 | 150 | 2012-02-10 | 2013-02-10 | 2013-09-19 | 900 |
*this is the dataframe i want to get :
| index | NUM_contrat | start | end | anniversary | quantity |
|---|---|---|---|---|---|
| 0 | 144 | 2008-02-11 | 2009-02-11 | 2009-02-11 | 550 |
| 0 | 144 | 2009-02-11 | 2010-02-11 | 2010-02-11 | 550 |
| 0 | 144 | 2010-02-11 | 2011-02-11 | 2011-02-11 | 550 |
| 0 | 144 | 2011-02-11 | 2011-03-28 | 2012-02-11 | 550 |
| 1 | 144 | 2011-03-28 | 2011-09-19 | 2012-02-11 | 550 |
| 2 | 150 | 2011-09-19 | 2012-02-10 | 2012-09-19 | 900 |
| 3 | 150 | 2012-03-28 | 2012-07-11 | 2013-09-19 | 900 |
this is my code that works only when i have only one Num_contract but doesnot work with 2 or more NUM_Contrat
for NUM_contrat in df['NUM_contrat'].unique():
for i in df['index'].unique():
for index,row in df.iterrows():
if df.iloc[index]['end'] > df.iloc[index]['anniversary']:
df1=pd.DataFrame(df.iloc[index]).transpose()
df.loc[index, 'end'] = df.loc[index, 'anniversary']
df= pd.concat([df,df1],ignore_index=True).sort_values(['start','end']).reset_index(drop=True)
df.loc[index+1,'start'] = df.loc[index,'anniversary']
df.loc[index+1,'anniversary'] = df.loc[index,'anniversary'] + relativedelta(years=1)
return df