8000 ma.sort(..., endwith=True) broken with nans · Issue #4422 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
ma.sort(..., endwith=True) broken with nans #4422
Open
@jnothman

Description

@jnothman

A masked array containing nan currently sorts such that nans appear at the end of the array, violating endwith=True:

>>> m = np.ma.masked_array([np.nan, -1], mask=[0, 1])
>>> m
masked_array(data = [nan --],
             mask = [False  True],
       fill_value = 1e+20)
>>> m.sort()
>>> m
masked_array(data = [-- nan],
             mask = [ True False],
       fill_value = 1e+20)

Apart from its own contract, this breaks consistency within the current implementation of np.ma.median (which should probably not rely on sort anyway), returning:

>>> np.ma.median(np.ma.masked_array([np.nan, 5, -1], mask=[0, 0, 1]))
5.0
>>> np.ma.median(np.ma.masked_array([np.nan, 5], mask=[0, 0]))
nan

(not that I'm sure what it means to take a median when there are nans present)

Perhaps minimum_fill_value needs an alternative for the sort case because of the convention that nan comes last.

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

      0