I have the following function that I've made which takes a member's sign-in date and the length of their contract and returns a modified dataframe with an expiration date for each member. The function works as intended, but I wanted to know if there was a cleaner way of writing the nested function month_to_num(ind)
. I know python does not implement cases
, but is there a way to rewrite the all the if/elif/else
statements?
def renewal_date(df):
def month_to_num(ind):
if (df.loc[ind, "Contract Type"] == "1 Month"):
return 1
elif (df.loc[ind, "Contract Type"] == "3 Months Prom" or
df.loc[ind, "Contract Type"] == "3 Month"):
return 3
elif (df.loc[ind, "Contract Type"] == "4 Month Promo"):
return 4
elif (df.loc[ind, "Contract Type"] == "6 Months"):
return 6
else:
return 12
for z in range(0, len(df)):
exp_date = (df.loc[z, "Date-Joined"] +
relativedelta(months=+month_to_num(z)))
df.set_value(z,"Date-Renewal", exp_date)
return df