Skip to content

Masked array set slice fails with np.ma.masked on right side #8624

Open

Description

@taldcroft

The following behavior is not what I would expect and seems like a bug:

In [1]: x = np.ma.MaskedArray([1,2,3])
In [2]: x[:2] = [100, np.ma.masked]
In [3]: x
Out[3]: 
masked_array(data = [100   0   3],
             mask = False,
       fill_value = 999999)

One can correctly set a single item (e.g. x[2] = np.ma.masked) or set a slice to a scalar (x[:2] = np.ma.masked).

I'm using numpy 1.11.3.

Activity

  1. taldcroft commented on Feb 16, 2017

    @taldcroft
    Author

    Interestingly, this works as expected:

    In [1]: x = np.ma.MaskedArray([1,2,3])
    In [2]: x[:2] = np.ma.asarray([100, np.ma.masked], dtype=int)
    In [3]: x
    Out[3]: 
    masked_array(data = [100 -- 3],
                 mask = [False  True False],
           fill_value = 999999)
    
  2. eric-wieser commented on Feb 23, 2017

    @eric-wieser
    Member

    A related bug came up in #8510, which I'll repeat here since that wasn't the focus of that issue:

    >>> arr = np.ma.arange(5)
    >>> arr[1:3] = [1, np.ma.masked]
    ValueError: setting an array element with a sequence.
    
    #expected result:
    >>> arr
    masked_array(data = [0 1 -- 3 4],
                 mask = [False False  True False False],
           fill_value = 999999)
  3. eric-wieser commented on Feb 23, 2017

    @eric-wieser
    Member

    Your code fails in the same way as the above for me on both master and 1.11.1

  4. sethtroisi commented on Sep 17, 2019

    @sethtroisi
    Contributor

    This no longer reproduces and this problem is "fixed"

    >>> x = np.ma.MaskedArray([1,2,3])
    >>> x[:2] = [100, np.ma.masked]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.6/dist-packages/numpy/ma/core.py", line 3323, in __setitem__
        _data[indx] = dval
      File "/usr/local/lib/python3.6/dist-packages/numpy/ma/core.py", line 4312, in __int__
        raise MaskError('Cannot convert masked element to a Python int.')
    numpy.ma.core.MaskError: Cannot convert masked element to a Python int.
    >>> x = np.ma.MaskedArray([1,2,3])
    >>> x[:2] = np.ma.asarray([100, np.ma.masked], dtype=int)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.6/dist-packages/numpy/ma/core.py", line 7771, in asarray
        subok=False, order=order)
      File "/usr/local/lib/python3.6/dist-packages/numpy/ma/core.py", line 2788, in __new__
        order=order, subok=True, ndmin=ndmin)
      File "/usr/local/lib/python3.6/dist-packages/numpy/ma/core.py", line 4312, in __int__
        raise MaskError('Cannot convert masked element to a Python int.')
    numpy.ma.core.MaskError: Cannot convert masked element to a Python int.
    >>> arr = np.ma.arange(5)
    >>> arr[1:3] = [1, np.ma.masked]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.6/dist-packages/numpy/ma/core.py", line 3323, in __setitem__
        _data[indx] = dval
      File "/usr/local/lib/python3.6/dist-packages/numpy/ma/core.py", line 4312, in __int__
        raise MaskError('Cannot convert masked element to a Python int.')
    numpy.ma.core.MaskError: Cannot convert masked element to a Python int.
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