pyxirr icon indicating copy to clipboard operation
pyxirr copied to clipboard

`ipmt()` and `ppmt()` do not accept an array as `per`

Open Peque opened this issue 2 years ago • 3 comments

It seems that is fine with numpy_financial:

>>> numpy_financial.ipmt(rate=0.03 / 12, per=numpy.arange(35) + 1, nper=35, pv=10000)
array([-25.        , -24.3156167 , -23.62952244, -22.94171294, ...

However, with pyxirr:

>>> pyxirr.ipmt(rate=0.03 / 12, per=numpy.arange(35) + 1, nper=35, pv=10000)
TypeError: argument 'per': only size-1 arrays can be converted to Python scalars

Not sure if it is related or not, but the returned type when not using an array is also different:

>>> numpy_financial.ipmt(rate=0.03 / 12, per=1, nper=35, pv=10000)
array(-25.)                                                                    
>>> pyxirr.ipmt(rate=0.03 / 12, per=1, nper=35, pv=10000)                          
-25

Peque avatar Nov 18 '21 12:11 Peque

It seems the same applies to pyxirr.ppmt. Updating title. :blush:

Peque avatar Nov 18 '21 12:11 Peque

@Peque , the same applies to all functions :) At the moment, pyxirr doesn't support vectorized calculations. I have it in the roadmap, but I don't have any estimates. Despite the fact that pyxirr implements all the functions from numpy_financial, it does not position itself as a drop-in-replacement for numpy_financial.

You can easily bypass this limitation by using "numpy.vectorize":

>>> ipmt = numpy.vectorize(pyxirr.ipmt)
>>> ipmt(rate=0.03 / 12, per=numpy.arange(35) + 1, nper=35, pv=10000)
array([-25.        , -24.3156167  ....

However, numpy.vectorize is likely to run slower than list comprehension:

>>> [pyxirr.ipmt(rate=0.03 / 12, per=per + 1, nper=35, pv=10000) for per in range(35)]

Anexen avatar Nov 18 '21 15:11 Anexen

@Anexen Thanks for the clarification! :blush:

Peque avatar Nov 18 '21 22:11 Peque

I finally managed to get this to work! I think I have developed an approach and will add vectorized versions of functions in the foreseeable future (probably by the end of March).

Anexen avatar Feb 26 '23 21:02 Anexen