Repository navigation
Conversation
_lerp computed inf - inf or inf * 0 for data with infinite entries, so e.g. np.percentile([1, 2, np.inf], 50) returned nan (with RuntimeWarnings) although the result is exactly 2.0 and np.median agrees. Fix up the entries that came out nan only because of an infinite endpoint and silence the spurious invalid warning.
ngoldbaum
left a comment
There was a problem hiding this comment.
At a minimum, this makes the implementation much more complicated for a niche edge case. This would be a lot more appealing if it wasn't so much more complicated than what's there.
Replace the nan fix-up branch with a single masked copyto: the weight selects an endpoint at t == 0 or t == 1, otherwise the infinite endpoint wins (a / 2 + b / 2, which stays nan for -inf and inf and cannot overflow for finite float16 values).
|
The fix is now roughly half the size, with 12 lines added instead of 24. Finite inputs follow exactly the same arithmetic as before, while a single masked copy corrects only invalid nan results where neither input was nan, preserving the expected endpoint and infinity behaviour. All 60 new tests still pass. |
|
I was previously tempted by a linear interpolation ufunc, but yeah. I am also unsure that this complexity is really worth the trouble as such. (It also does suppress correct warnings, even if most probably don't care about it.) |
|
Agreed it's too complex for an edge case this rare. I can leave the PR open in case you still want it, or for a future linear-interpolation ufunc. Thank you for taking time to check my pr |
PR summary
Closes gh-21091.
np.percentile()sometimes returnsnanfor inputs containinginfor-inf, even when the expected result is a valid number.For example:
This happens because
_lerpcan producenanduring operations likeinf - inforinf * 0.What changed?
I checked the previous discussions in #21343 and #22005 and followed the suggestion to fix only the affected values rather than changing the normal interpolation logic.
The usual calculation stays the same. After it runs, the code handles results that became
naneven though the original inputs did not containnan.The expected behaviour is:
a.b.-infandinfwith a weight between 0 and 1 still returnsnan, consistent withnp.median([-np.inf, np.inf]).Tests
Added tests for
_lerpandquantile:TestLerp::test_linear_interpolation_infinite_endpointsTestQuantile::test_infinite_valuesTestQuantile::test_infinite_values_exact40 of the 60 new test cases fail without the fix. All 60 pass with it.
I tested against the NumPy 2.5.4 wheel with the updated
_lerpcopied in. I haven't built NumPy from the main source branch yet.PR #30376 also touches this area, so a small rebase might be needed if it gets merged first.
First time contributor introduction
I'm a final-year IT student and use Python for my retrieval and ML projects, so NumPy is something I work with regularly. I found this issue while looking for long-standing correctness bugs and wanted to try fixing it.
AI Disclosure
I used Claude through Claude Code for this PR. It helped me find where the bug was, draft the
_lerpchange and tests, and run the tests. The code and tests were AI-generated. I reviewed the changes myself, and the PR description is written by me. I understand the changes and can explain the implementation. The revised fix after review was also drafted with Claude.