You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: np.percentile gives unreasonable results when array contains np.inf #21932
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:
importnumpyasnpnp.percentile(np.array([0, np.inf, np.inf]), 50) # Produces RuntimeWarning and returns np.nannp.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)]
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?
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.
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.
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.
Activity
generic-github-user commentedon Jul 10, 2022
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 ofinfin NumPy that they could link?seberg commentedon Jul 10, 2022
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.
evinism commentedon Jul 15, 2022
What would expected behavior be with something like
np.percentile([-np.inf, 5, np.inf], 50)? How aboutnp.percentile([-np.inf, np.inf], 50)? I assume these should probably each be NaN.seberg commentedon Jul 15, 2022
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 withinfwith numerical issues or so.If we all infinities to propagate numerical issues might cause infinity results where not expected.
BUG: Allow more permissive np.inf behavior in quantile and percentile
BUG: Better np.inf behavior for quantile and percentile calcs
BUG: Better np.inf behavior for quantile and percentile calcs
BUG: Better np.inf behavior for quantile and percentile calcs
seberg commentedon Sep 30, 2026
Closing this as duplicate of gh-21091 (I suspect there are smaller improvements one could do).