numpy-financial icon indicating copy to clipboard operation
numpy-financial copied to clipboard

BUG: ipmt not working with numpy==1.26.4 when using a pandas df as input.

Open erikgrenestam opened this issue 11 months ago • 2 comments

Describe the issue:

Not sure if bug or feature request, but I used to be able to use pandas dataframe columns as input arrays to npf.ipmt(). With numpy 1.26.4 and pandas 2.2.1 that yields a ValueError. I think it might be due to a change in np.broadcast_arrays()

Reproduce the code example:

import pandas as pd
import numpy_financial as npf
import numpy as np

df = pd.DataFrame({'rate': [0.05, 0.07], 'periods':[np.array([1,2,3,4]), np.array([1,2,3,4])], 'pv': [10000, 12000], 'nper': [10,10]})

#ValueError
npf.ipmt(df['rate'], df['periods'], df['nper'], df['pv'])

#Works
npf.ipmt(np.array(df['rate'].tolist()).reshape(-1,1), np.array(df['periods'].tolist()), np.array(df['nper'].tolist()).reshape(-1,1), np.array(df['pv'].tolist()).reshape(-1,1))

Error message:

Traceback (most recent call last)
Cell In[4], line 2
      1 df = pd.DataFrame({'rate': [0.05, 0.07], 'periods':[np.array([1,2,3,4]), np.array([1,2,3,4])], 'pv': [10000, 12000], 'nper': [10,10]})
----> 2 npf.ipmt(df['rate'], df['periods'], df['nper'], df['pv'])

File ~\.conda\envs\test\Lib\site-packages\numpy_financial\_financial.py:394, in ipmt(rate, per, nper, pv, fv, when)
    392 try:
    393     ipmt = np.where(when == 1, ipmt/(1 + rate), ipmt)
--> 394     ipmt = np.where(np.logical_and(when == 1, per == 1), 0, ipmt)
    395 except IndexError:
    396     pass

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Runtime information:

1.26.4 3.11.8 | packaged by Anaconda, Inc. | (main, Feb 26 2024, 21:34:05) [MSC v.1916 64 bit (AMD64)]

[{'numpy_version': '1.26.4', 'python': '3.11.8 | packaged by Anaconda, Inc. | (main, Feb 26 2024, ' '21:34:05) [MSC v.1916 64 bit (AMD64)]', 'uname': uname_result(system='Windows', node='xxx', release='10', version='10.0.19045', machine='AMD64')}, {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'], 'found': ['SSSE3', 'SSE41', 'POPCNT', 'SSE42', 'AVX', 'F16C', 'FMA3', 'AVX2'], 'not_found': ['AVX512F', 'AVX512CD', 'AVX512_SKX', 'AVX512_CLX', 'AVX512_CNL', 'AVX512_ICL']}}]

Context for the issue:

No response

erikgrenestam avatar Mar 19 '24 21:03 erikgrenestam