0

How calculate IRR on dateframe(df). I have dateframe:

ID Amount_paid Rate1 Rate2 Rate3
1 -3000 78,40 78,40 2500
2 -200 28,60 28,60 28,60
3 -4000 635,90 635,90 3600
4 -1000 635,90 635,90 3600
5 -800 635,90 635,90 3600
df['IRR']=npf.irr(df.iloc[:,1:]) 

ValueError: Input must be a rank-1 array.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
  • To calculate irr there must be at least one positive and at least one negative cash flow. All the `amount_paid` values you show are negative. Is there data not shown? Or are the cashflows those across the columns? So calculate 5 irrs? – Tls Chris Feb 09 '23 at 17:24
  • In Excel ID=1 pay month -3000 loan installment 1 78,4 loan installment 2 78,4 loan installment 3 2500 -4,08% ---> (Formula in Excel : IRR(all values ​​checked:-3000;78,4;78,4;78,4;2500) – Olga Małecka Feb 09 '23 at 17:25
  • for example works properly from ID=1: npf.irr(df.iloc[1,1:]) but from dateframe ValueError: Input must be a rank-1 array. – Olga Małecka Feb 09 '23 at 17:41

2 Answers2

0

df.iloc[:,1:] selects all three columns: Rate1, Rate2 and Rate3. But numpy.irr requires a single array as an input. It fails when you pass it three columns.

Can you let me know what your desired output is?

0

This works but there may be better ways to iterate through the dataframe. I don't use pandas often.

import pandas as pd
import numpy_financial as npf

df = pd.DataFrame()

df[ 'Amount_paid' ] = [ -3000, -200, -4000, -1000, -800 ]
df[ 'Rate1'] = [ 78.4, 28.6, 635.9, 635.9, 635.9 ]
df[ 'Rate2'] = [ 78.4, 28.6, 635.9, 635.9, 635.9 ]
df[ 'Rate3'] = [ 2500, 28.6, 3600, 3600, 3600 ]

result = []
for ix in range( len(df)):
    arr = df.iloc[ ix ]
    result.append( npf.irr(arr) )

result
# [-0.028570231670919766,
#  -0.3277960041116903,
#   0.07913479979420979,
#   0.930852297472391,
#   1.1442753876634035]

df[ 'IRR'] = result

df
#    Amount_paid  Rate1  Rate2   Rate3       IRR
# 0        -3000   78.4   78.4  2500.0 -0.040830
# 1         -200   28.6   28.6    28.6 -0.327796
# 2        -4000  635.9  635.9  3600.0  0.079135
# 3        -1000  635.9  635.9  3600.0  0.930852
# 4         -800  635.9  635.9  3600.0  1.144275

A version using df.apply

df[ 'IRR' ] = 0  # Reset 'IRR' to ensure apply does change it.
df[ 'IRR' ] = df.apply( npf.irr, axis = 1 )

df
#    Amount_paid  Rate1  Rate2   Rate3       IRR
# 0        -3000   78.4   78.4  2500.0 -0.040830
# 1         -200   28.6   28.6    28.6 -0.327796
# 2        -4000  635.9  635.9  3600.0  0.079135
# 3        -1000  635.9  635.9  3600.0  0.930852
# 4         -800  635.9  635.9  3600.0  1.144275    
Tls Chris
  • 3,564
  • 1
  • 9
  • 24