Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
taldcroft opened this issue Feb 16, 2017 · 4 comments
Open

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

taldcroft opened this issue Feb 16, 2017 · 4 comments
Labels
component: numpy.ma masked arrays

Comments

@taldcroft
Copy link

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.

@taldcroft
Copy link
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)

@eric-wieser
Copy link
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)

@eric-wieser
Copy link
Member

eric-wieser commented Feb 23, 2017

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

@sethtroisi
Copy link
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
Labels
component: numpy.ma masked arrays
Projects
None yet
Development

No branches or pull requests

3 participants