Skip to content

__array_prepare__ produces bad shape for np.ma.masked #8505

@eric-wieser

Description

@eric-wieser

A somewhat contrived example

>>> source = np.ma.masked  # comes from user function, could be anything
>>> source = np.asanyarray(source)  # force to array, so we can try to copy the type
>>> outarr = np.zeros((2, 2))  # allocate a raw ndarray for the result
>>> bad = source.__array_prepare__(outarr) # prepare that raw array for operations 
>>> bad.shape
(2, 2)
>>> bad.data.shape
()  # uh oh
>>> bad.mask.shape
()  # spaghettios

Which leads to failures like

>>> res.transpose((1, 0))
Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    np.asanyarray(np.ma.masked).__array_prepare__(np.zeros((2, 2))).transpose((1, 0))
  File "C:\Program Files\Python 3.5\lib\site-packages\numpy\ma\core.py", line 2509, in wrapped_method
    result = getattr(self._data, funcname)(*args, **params)
ValueError: axes don't match array

This come up when trying to invoke #8441 on masked arrays.

Is this a bug, or an invalid use of __array_prepare__?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions