Skip to content

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

Closed

Description

@charris

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'

Activity

  1. githubmlai commented on Jul 11, 2015

    @githubmlai

    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

  2. eric-wieser commented on Sep 27, 2017

    @eric-wieser
    Member

    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

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