Skip to content

Object-type masked array special-cases elements that are ndarray #18981

Open

Description

@taldcroft

The MaskedArray class has unexpected special-casing of ndarray objects within an object array. I expect an object array to be a generic container of whatever is in each cell, and if that element is masked then accessing it should always return np.ma.masked. Instead MaskedArray appears to turn the ndarray into a fully-masked MaskedArray.

Reproducing code example:

import numpy as np
>>> m = np.ma.array([None, [1, 2], np.array([1, 2])], dtype=object)
>>> m
masked_array(data=[None, list([1, 2]), array([1, 2])],
             mask=False,
       fill_value='?',
            dtype=object)
>>> m.mask = [True, True, True]
>>> m[0]
masked
>>> m[1]
masked
>>> m[2]  # Accessing the ndarray has auto-magically converted it to masked_array
masked_array(data=[--, --],
             mask=[ True,  True],
       fill_value=999999,
            dtype=int64)
>>> m.data[2]  # The data element under the mask is still the original ndarray
array([1, 2])

Expected:

>>> m[2]
masked

Error message:

N/A

NumPy/Python version information:

1.19.5 3.7.9 (default, Aug 31 2020, 07:22:35) 
[Clang 10.0.0 ]

Activity

  1. seberg commented on May 12, 2021

    @seberg
    Member

    I expect this needs a check for object dtype (maybe together with a check for ndim == 1. Although, IIRC that logic is definitely pretty holey... It was not a regression in 1.19.x, right?

  2. fengluoqiuwu commented on Oct 26, 2024

    @fengluoqiuwu
    Contributor

    It also looks like there could be a conflict with another issue from about 10 years ago.

    # special case introduced in gh-5962
    elif (self.dtype.type is np.object_ and
          isinstance(dout, np.ndarray) and
          dout is not masked):
        # If masked, turn into a MaskedArray, with everything masked.
        if mout:
            return MaskedArray(dout, mask=True)
        else:
            return dout

    If we want to fix both of it, we must change the basic structure of masked......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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