Skip to content

BUG: np.percentile gives unreasonable results when array contains np.inf #21932

Closed as duplicate

Description

@Markus28

Describe the issue:

Even when an array contains np.inf, there are cases where certain percentiles are clearly defined. E.g.

  • np.percentile(np.array([0, np.inf, np.inf]), 50) should be the median, which would be np.inf
  • np.percentile(np.array([0, np.inf, np.inf, np.inf]), 50) should (even more so) be np.inf

This is not the case. In both cases, we get np.nan.

Reproduce the code example:

import numpy as np
np.percentile(np.array([0, np.inf, np.inf]), 50) # Produces RuntimeWarning and returns np.nan
np.percentile(np.array([0, np.inf, np.inf, np.inf]), 50) # No warning but returns np.nan

Error message:

/home/markus/.local/lib/python3.10/site-packages/numpy/lib/function_base.py:4527: RuntimeWarning: invalid value encountered in subtract
  diff_b_a = subtract(b, a)

NumPy/Python version information:

1.23.0 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]

Activity

  1. generic-github-user commented on Jul 10, 2022

    @generic-github-user

    I'm not so sure this is well-defined - all of the interpolation methods would also have to work on np.inf. Maybe someone has a resource on the semantics of inf in NumPy that they could link?

  2. seberg commented on Jul 10, 2022

    @seberg
    Member

    I do think it is pretty well defined, the interpolation methods all do the same thing here so that is not a problem really. The way the interpolation is calculated does this, and it would need fixing up the interpolation itself (the "methods" do not change how that interpolation happens).

    This issue is a duplicate of gh-21091 there is a PR in gh-21343, but it needs work so even a new PR seems would seem OK to me.

  3. evinism commented on Jul 15, 2022

    @evinism

    What would expected behavior be with something like np.percentile([-np.inf, 5, np.inf], 50)? How about np.percentile([-np.inf, np.inf], 50)? I assume these should probably each be NaN.

  4. seberg commented on Jul 15, 2022

    @seberg
    Member

    OK, so np.percentile([-np.inf, np.inf], 50) is clearly NaN (I would say, it is the same as the mean). np.percentile([-np.inf, 5, np.inf], 50) is interesting, because it is perfectly well defined, but might end up with inf with numerical issues or so.
    If we all infinities to propagate numerical issues might cause infinity results where not expected.

  5. seberg commented on Sep 30, 2026

    @seberg
    Member

    Closing this as duplicate of gh-21091 (I suspect there are smaller improvements one could do).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions