E3FB BUG: Masked object array of ndarrays attaches mask to masked array value · Issue #8906 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Masked object array of ndarrays attaches mask to masked array value #8906

@eric-wieser

Description

@eric-wieser

See below

>>> m = np.ma.array([None], mask=[False])

>>> m[0] = np.ma.masked; m[0]
masked

>>> m[0] = np.eye(3); m[0]
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

>>> m[0] = np.ma.masked; m[0]
masked_array(data =
 [[-- -- --]
 [-- -- --]
 [-- -- --]],
             mask =
 [[ True  True  True]
 [ True  True  True]
 [ True  True  True]],
       fill_value = 1e+20)   # but that was the same line that we ran above!

The culprit is the # Did we extract a single item? check in numpy/ma/core.py:3192.

I just don't think we have the information needed to actually know whether we got a single item from self.data, which could be of any subclass (?).

Maybe the code should become something like:

# Normalize the index. This is wrong, but being right is really hard (#8276)
if not isinstance(idx, tuple): 
    idx = (idx,)

# append an ellipsis if there was not already one, and remember whether we did
could_be_scalar = Ellipsis not in idx
if could_be_scalar:
    idx = idx + (Ellipsis,)

# guaranteed to be a (possibly 0d) array and not a scalar element
dout = data[idx]

# flatten 0d arrays that were not requested
if could_be_scalar and dout.ndim == 0:
    result = result[()]
return result

#8276 (comment) for the lazy

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