Skip to content

np.max() doesn't behave properly for ndarray subclasses that define __array_ufunc__ #9102

Closed
@ngoldbaum

Description

@ngoldbaum

Take the following example:

import numpy as np

class MyArr(np.ndarray):
    def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
        func = getattr(ufunc, method)
        inp = [i.view(np.ndarray) for i in inputs]
        return func(*inp, **kwargs)

data = np.array([[1, 2, 3], [1, 2, 3]])

arr = data.view(MyArr)

print(data.max())
print(arr.max())

On numpy 1.13rc1 this prints:

3
[1 2 3]

For the call to arr.max() numpy calls __array_ufunc__, but there doesn't seem to be a way to tell from inside __array_ufunc__ that we should be reducing across the whole array and not just the first dimension. I think this would work properly if numpy were calling __array_ufunc__ with axis=None.

Ping @mhvk

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions