Skip to content

BUG: quantile inconsitent with median for size=0 #29315

Open

Description

@DerWeh

Describe the issue:

For empty inputs (size=0), quantile yields an Index error:

>> np.quantile([], 0.5)
IndexError: index -1 is out of bounds for axis 0 with size 0

While median yields np.float64(nan) (with warnings):

>> np.median([])
np.float64(nan)

The See also section, suggests np.quantile(..., 0.5) == np.median(...), else I find no information on the case size=0, or any mention of raised errors.
The behavior of median is consistent with mean, which follows the specification of https://data-apis.org/array-api/latest/API_specification/generated/array_api.mean.html.

I would expect quantile to return NaN for empty arrays.

Reproduce the code example:

import numpy as np
np.quantil([], 0.5)

Error message:

Python and NumPy Versions:

2.2.6
3.12.6 | packaged by conda-forge | (main, Sep 30 2024, 18:08:52) [GCC 13.3.0]

Runtime Environment:

No response

Context for the issue:

No response

Activity

  1. DerWeh commented on Jul 3, 2025

    @DerWeh
    ContributorAuthor

    Probably, we need more testing... hypothesis reveals more incorrect edge cases...

    >> import numpy as np
    >> np.quantile(np.array([32767, -1], dtype=np.int16), 0.5)
    np.float64(49151.0)
    >> np.median(np.array([32767, -1], dtype=np.int16))
    np.float64(16383.0)

    Apparently, the cast to np.float64 was done after calculating the difference...
    (Tested also with NumPy 2.3.1)

    I would suggest adding hypothesis tests np.quantile(..., 0) == np.median(...) and np.min(...) <= np.quantile(..., q) <= np.max(...) (of course, paying attention to NaNs).

  2. imran4444shaik commented on Jul 4, 2025

    @imran4444shaik

    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!

  3. added a commit that references this issue on Jul 5, 2025
    658a01a
  4. eendebakpt commented on Jul 6, 2025

    @eendebakpt
    Contributor

    The float16 edge cases might be addressed by #29105

  5. DerWeh commented on Jul 6, 2025

    @DerWeh
    ContributorAuthor

    @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

  6. seberg commented on Jul 30, 2025

    @seberg
    Member

    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.

  7. DerWeh commented on Jul 31, 2025

    @DerWeh
    ContributorAuthor

    Thanks for your input @seberg

    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:

    >> np.quantile([1, 2], 0.5, method="lower")
    np.int64(1)
    

    This clearly contradicts the documentation:

    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.

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