Skip to content

ma.where does not work with structured types. #5826

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

Closed
charris opened this issue May 2, 2015 · 2 comments
Closed

ma.where does not work with structured types. #5826

charris opened this issue May 2, 2015 · 2 comments

Comments

@charris
Copy link
Member

charris commented May 2, 2015

I think this is an easy fix for someone familiar with the ma module.

In [1]: x = np.ones(3, dtype=[('a', 'i4'), ('b', 'f4')]).view(ma.MaskedArray)

In [2]: y = np.zeros(3, dtype=[('a', 'i4'), ('b', 'f4')]).view(ma.MaskedArray)

In [3]: ma.where([True, False, True], x, y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-5e3a8c327920> in <module>()
----> 1 ma.where([True, False, True], x, y)

/home/charris/.local/lib/python2.7/site-packages/numpy/ma/core.pyc in where(condition, x, y)
   6786     # Create an empty mask and fill it
   6787     mask = np.zeros(fc.shape, dtype=MaskType)
-> 6788     np.copyto(mask, getmask(x), where=fc)
   6789     np.copyto(mask, getmask(y), where=notfc)
   6790     mask |= getmaskarray(condition)

TypeError: Cannot cast scalar from dtype([('a', '?'), ('b', '?')]) to dtype('bool') according to the rule 'same_kind'
@githubmlai
Copy link

Not easy fix?

It might have to use result_type in where function

    if x is masked:
        ndtype = yv.dtype
    elif y is masked:
        ndtype = xv.dtype
    else:
        ndtype = np.result_type(xv.dtype, yv.dtype)

might help but cause further error because you can't do OR operation on structured mask

e.g.

    _mask |= getmaskarray(condition)

throws error

@njsmith njsmith removed the Easy Fix label Jul 12, 2015
@eric-wieser
Copy link
Member

eric-wieser commented Sep 27, 2017

This is fixed in 1.14-dev:

masked_array(data = [(1, 1.0) (0, 0.0) (1, 1.0)],
             mask = [(False, False) (False, False) (False, False)],
       fill_value = (999999, 1.00000002e+20),
            dtype = [('a', '<i4'), ('b', '<f4')])

Edit: #8647

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

Successfully merging a pull request may close this issue.

4 participants