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: quantile inconsitent with median for size=0 #29315
Right now, np.quantile([], x) raises an IndexError on empty inputs even though np.median([]) and np.mean([]) both return nan, so the API is inconsistent. We can resolve this by inserting an early guard clause that checks for zero‑length axes and directly returns a float64 array of nan.
I’d be happy to draft a PR implementing these changes and adding unit tests to cover these edge cases!
@imran4444shaik while it is great that you are so eager to fix things, I think we should wait for some NumPy developer. This would be a change of behavior and should be coordinated.
Furthermore, it's probably best not to reinvent the wheel and implement a different handling of size==0. np.median seems to simply fall back to np.mean for NaN and size==0 behavior (that's why we get strange warnings for np.median([])) and they mention
# can't be reasonably be implemented in terms of percentile as we have to# call mean to not break astropy
@eendebakpt thanks for the reference. Can you shortly elaborate? To me, it seems like you are addressing issues with imprecise q parameters for np.percentile? So far, I mostly saw issues with integer overflows, but I didn't check further. But this supports my statement that we need better tests. NumPy already uses hypothesis, so I can supply the property test. We probably just need to coordinate the addition of the test with #29105
I don't mind defining the empty quantile to NaN, although it seems mainly useful for nanquantile (which is an argument).
But I don't really want to push the fact that median and quantile must agree on this too much, although I agree it is an argument for quantile to behave similar.
One big problem is that the comparison with median only makes sense for interpolating quantile methods which can return NaN for non-float inputs. The discrete methods can't even return NaN for non-float inputs, so we might fix one surprise but introduce a new one.
I don't mind defining the empty quantile to NaN, although it seems mainly useful for nanquantile (which is an argument).
Sorry, I don't quite get your point here. Could you please elaborate?
But I don't really want to push the fact that median and quantile must agree on this too much, although I agree it is an argument for quantile to behave similar.
No problem, in this case it might be reasonable to reword the See Also section:
[median](https://numpy.org/doc/stable/reference/generated/numpy.median.html#numpy.median)
equivalent to quantile(..., 0.5)
But I am no native speaker, so maybe it is already clear enough.
One big problem is that the comparison with median only makes sense for interpolating quantile methods which can return NaN for non-float inputs. The discrete methods can't even return NaN for non-float inputs, so we might fix one surprise but introduce a new one.
To be honest, this is already a surprise for me. Just cross-checked it and you are right:
If the input contains integers or floats smaller than float64, the output data-type is float64. Otherwise, the output data-type is the same as that of the input.
Either the documentation or the behavior hast to be adjusted. To me the behavior is very surprising, it feels quite odd that the data type of the output depends on string arguments. But this might be considered a breaking change.
Activity
DerWeh commentedon Jul 3, 2025
Probably, we need more testing... hypothesis reveals more incorrect edge cases...
Apparently, the cast to
np.float64was done after calculating the difference...(Tested also with NumPy 2.3.1)
I would suggest adding hypothesis tests
np.quantile(..., 0) == np.median(...)andnp.min(...) <= np.quantile(..., q) <= np.max(...)(of course, paying attention to NaNs).imran4444shaik commentedon Jul 4, 2025
Thanks for raising this!
Right now, np.quantile([], x) raises an IndexError on empty inputs even though np.median([]) and np.mean([]) both return nan, so the API is inconsistent. We can resolve this by inserting an early guard clause that checks for zero‑length axes and directly returns a float64 array of nan.
I’d be happy to draft a PR implementing these changes and adding unit tests to cover these edge cases!
BUG: Fix quantile to return NaN for empty arrays like median
eendebakpt commentedon Jul 6, 2025
The float16 edge cases might be addressed by #29105
DerWeh commentedon Jul 6, 2025
@imran4444shaik while it is great that you are so eager to fix things, I think we should wait for some NumPy developer. This would be a change of behavior and should be coordinated.
Furthermore, it's probably best not to reinvent the wheel and implement a different handling of
size==0.np.medianseems to simply fall back tonp.meanfor NaN andsize==0behavior (that's why we get strange warnings fornp.median([])) and they mention@eendebakpt thanks for the reference. Can you shortly elaborate? To me, it seems like you are addressing issues with imprecise
qparameters fornp.percentile? So far, I mostly saw issues with integer overflows, but I didn't check further. But this supports my statement that we need better tests. NumPy already useshypothesis, so I can supply the property test. We probably just need to coordinate the addition of the test with #29105seberg commentedon Jul 30, 2025
I don't mind defining the empty quantile to NaN, although it seems mainly useful for
nanquantile(which is an argument).But I don't really want to push the fact that median and quantile must agree on this too much, although I agree it is an argument for quantile to behave similar.
One big problem is that the comparison with
medianonly makes sense for interpolating quantile methods which can return NaN for non-float inputs. The discrete methods can't even return NaN for non-float inputs, so we might fix one surprise but introduce a new one.DerWeh commentedon Jul 31, 2025
Thanks for your input @seberg
Sorry, I don't quite get your point here. Could you please elaborate?
No problem, in this case it might be reasonable to reword the
See Alsosection:But I am no native speaker, so maybe it is already clear enough.
To be honest, this is already a surprise for me. Just cross-checked it and you are right:
This clearly contradicts the documentation:
Either the documentation or the behavior hast to be adjusted. To me the behavior is very surprising, it feels quite odd that the data type of the output depends on string arguments. But this might be considered a breaking change.