numpy / numpy/numpy-financial

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

Open
#105 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug help wanted sustain-2026
Dominant language
Python
Stars
409
Forks
98
Avg merge
10h 43m
Merged PRs (30d)
12

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with numpy 1.26.4 and pandas 2.2.1, then inspect numpy_financial/_financial.py around ipmt, especially the failing np.where call. Compare the pandas-column inputs with the working reshaped-array example; done means ipmt accepts the reported DataFrame columns without raising and preserves the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.