Skip to content

BUG: np.ma.argsort has different default for axis than ndarray #8701

@eric-wieser

Description

@eric-wieser

Right now, the default arguments for sorting-related methods are:

Defaults to axis=-1:

  • np.sort
  • np.ndarray.sort
  • np.argsort
  • np.ndarray.argsort
  • np.ma.sort
  • np.masked_array.sort
  • The documentation of np.ma.argsort
  • The documentation of np.masked_array.argsort

Defaults to axis=None:

  • The implementation of np.ma.argsort
  • The implementation of np.masked_array.argsort

For 1D arrays, there is no difference, as None just means flatten and use the only axis.

For 2D arrays, however, code written for ndarrays can fail in unexpected ways when passed a masked_array, because argsort breaks liskov subsitution:

def foo(x):
    i = x.argsort()
    assert(i.shape == x.shape)

>>> foo(som_arr)  # ok
>>> foo(som_arr.view(MaskedArray)) # AssertionError

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions