1
\$\begingroup\$

I have the following code which I use to determine how much of each repayment goes into each late payments bucket (i.e. 0-7 days past due, 7-15 days past due... etc)

For example, the repayment amount is 100. 20 gets repid early, and then the customer misses the repayment date. Therefore, 80 goes into the 0-7dpd bucket and so on. Similarly, if the customer does not repay for 7 days, the 80 goes into the 7-15dpd bracket.

The code works, but takes a long time. I was wondering if there is a method to make the code run faster.

repayments['principal_0_7_dpd'] = np.where((repayments.paid_y_n == 'no') & (repayments.days_since_due >= 2.0), repayments.amount_principal - (repayments.child_principal_repaid_early), 0)
repayments['principal_7_15_dpd'] = np.where((repayments.paid_y_n == 'no') & (repayments.days_since_due >= 7.0), repayments.amount_principal - (repayments.child_principal_repaid_early + repayments.child_principal_repaid_1_7_days), 0)
repayments['principal_15_30_dpd'] = np.where((repayments.paid_y_n == 'no') & (repayments.days_since_due >= 15.0), repayments.amount_principal - (repayments.child_principal_repaid_early + repayments.child_principal_repaid_1_7_days + repayments.child_principal_repaid_7_15_days), 0)  
repayments['principal_30_45_dpd'] = np.where((repayments.paid_y_n == 'no') & (repayments.days_since_due >= 30.0), repayments.amount_principal - (repayments.child_principal_repaid_early + repayments.child_principal_repaid_1_7_days + repayments.child_principal_repaid_7_15_days + repayments.child_principal_repaid_15_30_days), 0)  
repayments['principal_45_60_dpd'] = np.where((repayments.paid_y_n == 'no') & (repayments.days_since_due >= 45.0), repayments.amount_principal - (repayments.child_principal_repaid_early + repayments.child_principal_repaid_1_7_days + repayments.child_principal_repaid_7_15_days + repayments.child_principal_repaid_15_30_days + repayments.child_principal_repaid_30_45_days), 0)  
repayments['principal_60_90_dpd'] = np.where((repayments.paid_y_n == 'no') & (repayments.days_since_due >= 60.0), repayments.amount_principal - (repayments.child_principal_repaid_early + repayments.child_principal_repaid_1_7_days + repayments.child_principal_repaid_7_15_days + repayments.child_principal_repaid_15_30_days + repayments.child_principal_repaid_30_45_days + repayments.child_principal_repaid_45_60_days), 0)  
repayments['principal_90_120_dpd'] = np.where((repayments.paid_y_n == 'no') & (repayments.days_since_due >= 90.0), repayments.amount_principal - (repayments.child_principal_repaid_early + repayments.child_principal_repaid_1_7_days + repayments.child_principal_repaid_7_15_days + repayments.child_principal_repaid_15_30_days + repayments.child_principal_repaid_30_45_days + repayments.child_principal_repaid_45_60_days + repayments.child_principal_repaid_60_90_days), 0)  
repayments['principal_120_plus_dpd'] = np.where((repayments.paid_y_n == 'no') & (repayments.days_since_due >= 120.0), repayments.amount_principal - (repayments.child_principal_repaid_early + repayments.child_principal_repaid_1_7_days + repayments.child_principal_repaid_7_15_days + repayments.child_principal_repaid_15_30_days + repayments.child_principal_repaid_30_45_days + repayments.child_principal_repaid_45_60_days + repayments.child_principal_repaid_60_90_days + repayments.child_principal_repaid_90_120_days), 0)       

You can use the following test data. The expected output is also included below:

df = pd.DataFrame({
'amount_principal':[456.6459958, 456.6459958], 
'days_since_due':[1068, 1061], 
'paid_y_n':['yes, 'no'], 
'child_principal_repaid_early':[0, 0], 
'child_principal_repaid_1_7_days':[0, 0], 
'child_principal_repaid_7_15_days':[0, 0],
'child_principal_repaid_15_30_days':[0, 180.2126753],
'child_principal_repaid_30_45_days':[0, 0],
'child_principal_repaid_45_60_days':[0, 0],
'child_principal_repaid_60_90_days':[0, 0],
'child_principal_repaid_90_120_days':[0, 0],
'child_principal_repaid_120_plus_days':[0, 0]}, index = [0,1])

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.