Pandas: Calculating IRR of loan cashflows

892 Views Asked by At

I have a Pandas dataframe of cashflows which are of unpredictable length. They range monthly over a period of between a few months and 30+ months and there is a column each month which contains that months cashflow information. I need to compute the IRR for each loan which is defined as follows;

Definition of IRR, where r is "IRR" when NPV is set to 0.

Below is a link to an example of the df where I have "months_remaining", the initial outflow and a range of cashflows over variable periods (between Feb-19 and Dec-29). "S" represents a loan that has settled, i.e. it has reached maturity and had paid back the full amount. "0" represents a loan that has defaulted therefore no further cashflows are expected in perpetuity;

Loans DF

I have tried a range of approaches. (1) Using iterrows to create a numpy array for each row then tried using numpy.irr, however the issue of only calculating IRR for a variable time period still stands (I need to recognise when cashflows stop with an "S" and stop calculating from this point.) (2) Avoiding row by row approaches and trying to perform calculations in Pandas, again the mixture of some "S" loans and some normal cashflows in columns trips this approach up.

Cell which I am currently working in.

Thanks for any help you can provide.

[EDIT] An example of working code using a for loop

 for row in irr_array:
  LoanID=row[1]
  Tier=row[2]
  row=row[2:2000]
  if "S" in row:
    new_array = [i for i in row if i is not "S"]
    irr_results["IRR"]= (pd.Series(round(irr(new_array),3)))
    irr_results["LoanID"]=(pd.Series(LoanID))
    irr_results["Tier"]=(pd.Series(Tier))
    results=irr_df.append(irr_results)
  if 0 in row:
    irr_results["IRR"]= 0
    irr_results["LoanID"]=(pd.Series(LoanID))
    irr_results["Tier"]=(pd.Series(Tier))
    results=irr_df.append(irr_results)      
0

There are 0 best solutions below