8000 __array_prepare__ produces bad shape for np.ma.masked · Issue #8505 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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

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
eric-wieser opened this issue Jan 20, 2017 · 2 comments
Closed

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

eric-wieser opened this issue Jan 20, 2017 · 2 comments

Comments

@eric-wieser
Copy link
Member
eric-wieser commented Jan 20, 2017

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__?

@eric-wieser
Copy link
Member Author
eric-wieser commented Jan 20, 2017

Related:

def assert_broadcasts(ufunc, a, b):
    expected = np.broadcast(a, b).shape
    actual = ufunc(a, b).shape
    assert expected == actual

assert_broadcasts(np.add, [1, 2, 3], 1)
assert_broadcasts(np.add, [1, 2, 3], np.ma.masked)  # AssertionError

@eric-wieser
Copy link
Member Author

Closing to move discussion to the above PR

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

No branches or pull requests

1 participant
0