Skip to content
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

Odd behavior of ma.add.reduce on boolean arrays #7623

Open
abalkin opened this issue May 11, 2016 · 6 comments
Open

Odd behavior of ma.add.reduce on boolean arrays #7623

abalkin opened this issue May 11, 2016 · 6 comments

Comments

@abalkin
Copy link
Contributor

abalkin commented May 11, 2016

Consider two masked arrays:

In [40]: a = np.ma.array([True] * 3, mask=[False] * 3)

In [41]: b = np.ma.array([True] * 3)

The only difference is that one has an all-false mask and the other has mask=nomask. However, ma.add.reduce returns different values:

In [42]: np.ma.add.reduce(a)
Out[42]: True

In [43]: np.ma.add.reduce(b)
Out[43]: 3
@charris
Copy link
Member

charris commented May 11, 2016

Hmm, looks like this might be a problem in ndarray also

In [6]: a = np.array([True] * 3)

In [7]: a + a
Out[7]: array([ True,  True,  True], dtype=bool)

In [8]: add.reduce(a)
Out[8]: 3

In [9]: add.reduce(a, dtype=bool_)
Out[9]: True

@abalkin
Copy link
Contributor Author

abalkin commented May 12, 2016

@charris - the ndarray behavior that you show seems to follow naturally from the definition of addition on booleans: basically "add" means "or". Not the best, but it is what it is. What do you see as a bug in your display?

@abalkin
Copy link
Contributor Author

abalkin commented May 12, 2016

The issue that I am reporting here is ma specific. Take a look at this if statement in the definition of reduce(). The dtype is handled differently depending on whether the mask is nomask or not.

@charris
Copy link
Member

charris commented May 12, 2016

@abalkin The reference was to the fact that addition results in a boolean, but reduction returns and integer. That is an inconsistancy. I suspect both should return integer for addition, as a lot of code adds up the booleans for counting purposes, while, if the logical form is needed, logical_or is the way to go.

@abalkin
Copy link
Contributor Author

abalkin commented May 12, 2016

@charris - this is documented for np.sum:

In [5]: np.sum?
Signature: np.sum(a, axis=None, dtype=None, out=None, keepdims=False)
..
dtype : dtype, optional
    The type of the returned array and of the accumulator in which
    the elements are summed.  By default, the dtype of `a` is used.
    An exception is when `a` has an integer type with less precision
    than the default platform integer.  In that case, the default
    platform integer is used instead.

Maybe we should document an exception for the reduce as well.

@WarrenWeckesser WarrenWeckesser added the component: numpy.ma masked arrays label Nov 18, 2021
@lrq3000
Copy link

lrq3000 commented Mar 30, 2023

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

No branches or pull requests

4 participants