Skip to content

BUG: quantile/percentile: return the exact result for infinite values - #33361

Open
zaidwhy wants to merge 3 commits into
numpy:mainfrom
zaidwhy:fix-quantile-inf-lerp
Open

zaidwhy wants to merge 3 commits into
numpy:mainfrom
zaidwhy:fix-quantile-inf-lerp

Conversation

@zaidwhy

@zaidwhy zaidwhy commented Nov 17, 2026 •

Copy link
Copy Markdown

PR summary

Closes gh-21091.

np.percentile() sometimes returns nan for inputs containing inf or -inf, even when the expected result is a valid number.

For example:

np.percentile([np.inf], 50)               # nan -> inf
np.percentile([1, 2, np.inf], 50)         # nan -> 2.0
np.percentile([1, 2, np.inf], 75)         # nan -> inf
np.percentile([-np.inf, -np.inf, 1], 50)  # nan -> -inf

This happens because _lerp can produce nan during operations like inf - inf or inf * 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 nan even though the original inputs did not contain nan.

The expected behaviour is:

  • A weight of 0 returns a.
  • A weight of 1 returns b.
  • Equal endpoints return the same value.
  • Otherwise, if one endpoint is infinite, that endpoint determines the result.
  • Interpolating between -inf and inf with a weight between 0 and 1 still returns nan, consistent with np.median([-np.inf, np.inf]).

Tests

Added tests for _lerp and quantile:

  • TestLerp::test_linear_interpolation_infinite_endpoints
  • TestQuantile::test_infinite_values
  • TestQuantile::test_infinite_values_exact

40 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 _lerp copied 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 _lerp change 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.

_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 ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
@zaidwhy

zaidwhy commented Nov 18, 2026

Copy link
Copy Markdown
Author

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.

@seberg

seberg commented Nov 25, 2026

Copy link
Copy Markdown
Member

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.)

@zaidwhy

zaidwhy commented Nov 25, 2026

Copy link
Copy Markdown
Author

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.
Or I can close it, whatever you advise

Thank you for taking time to check my pr

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: inf in quantile has undefined behaviour (and possibly different for -inf vs +inf)

3 participants