python loyalt point expire date calculation

  Kiến thức lập trình

for below dataset i need to calcute email based points expire date.Normally expire date is create+366 days but If customer makes another purchase within 365 days previouse expire dates shoul be automatilly update same as last purchase’s expire date. If there is more than 365 days nothing is change for previous expire dates, it should e iteratives.

data = """date_created,first_name,last_name,email,description,points_x,expire_date
20.01.2020,namex,surenamex,[email protected],Make a Purchase,74,
20.01.2022,namex,surenamex,[email protected],Double Points for Sale,148,
3.02.2022,namex,surenamex,[email protected],Make a Purchase,70,
10.02.2022,namex,surenamex,[email protected],Duplicate Campaign,-74,
23.02.2022,namex,surenamex,[email protected],Make a Purchase,0,
20.01.2020,namey,surenamey,[email protected],Make a Purchase,74,
20.01.2022,namey,surenamey,[email protected],Double Points for Sale,148,
3.02.2022,namey,surenamey,[email protected],Make a Purchase,70,
10.02.2022,namey,surenamey,[email protected],Duplicate Campaign,-74,
23.02.2022,namey,surenamey,[email protected],Make a Purchase,0,
"""[previous_purchase_index, 'date_created']).days <= 366:
previous_purchase_index = None
previous_expire_date = None
for i in range(len(df)):
        if[i, 'description'] == 'Make a Purchase':
            current_date =[i, 'date_created']
        if previous_purchase_index is not None and (current_date -[previous_purchase_index, 'date_created']).days <= 366:

      [i, 'expire_date'] = previous_expire_date
      [i, 'expire_date'] = current_date + pd.DateOffset(days=366)
 previous_purchase_index = i           
 previous_expire_date =[i, 'expire_date']