pythonnumpyirr

IRR calcluation : ValueError: invalid literal for float():


I am trying to calculate IRR and its giving me result in command line but whenever I am trying to pass the values as a parameter its failing with below error

Code

import numpy
import sys   

def irrYearly(array):
    array = [array]
    irr = round(numpy.irr(array), 2)*100
    return irr
def irrCal(tenure, array):
    if tenure == "Yearly":
        # return irrYearly(array)
        # array = [-30000, -30000, -30000, -30000, -30000, 107180]  -- if I uncomment this line its works fine
        print(irrYearly(array))

    elif tenure == "Monthly":
        # return irrMonthly(array)
        print(irrMonthly(array))

def main():
    if len(sys.argv) == 3:
        tenure = sys.argv[1]
        array = sys.argv[2]
        irrCal(tenure, array)  

main()

Error

Please find the error

C:\Python27\python.exe C:/Users/abhis/PycharmProjects/IRR/irr.py Yearly -30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00
Traceback (most recent call last):
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 74, in <module>
['-30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00']
    main()
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 64, in main
    irrCal(tenure, array)
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 49, in irrCal
    print(irrYearly(array))
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py"", line 19, in irrYearly
    irr = round(numpy.irr(array), 2)*100
  File "C:\Python27\lib\site-packages\numpy\lib\financial.py", line 669, in irr
    res = np.roots(values[::-1])
  File "C:\Python27\lib\site-packages\numpy\lib\polynomial.py", line 222, in roots
    p = p.astype(float)
ValueError: invalid literal for float(): -30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00

Solution

  • argv[2] is a string with comma separated values, but to numpy it's a string not a list of floats. You first need to convert it like so: float_vals = [float(x) for x in argv[2].split(",")].