calculate IRR on dateframe

110 Views Asked by At

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.

2

There are 2 best solutions below

0
G S Praneeth Reddy On

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?

1
Tls Chris On

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