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

BUG: Fixed where keyword for np.mean & np.var methods #18612

Merged
merged 1 commit into from Mar 14, 2021

Conversation

charris
Copy link
Member

@charris charris commented Mar 14, 2021

Backport of #18560.

Addresses #18552

I've managed to fix where keyword issue by refactoring those if statements.

So the rcount is a scalar when where is not provided as we reduce equal number of elements for given axis (or axes). So the first and statement prevents from illegal comparison of array to 0 value that caused original error. If the where clause is provided and rcount is an array (as we might want to reduce different number of elements along axis due to masking) then we check second and statement if any of those reduce groups are zero. I also maintained this micro-optimization to avoid calling _any(rcount == 0) when where is True and not zero.

Is this correct?

Also I had to change umr_any to _any as I think there is another bug there (or umr_any shouldn't be used there). np.any without axis parameter evaluates to scalar value (that can be used in if statement). But umr_any does not behave that way - it only reduces one dimension. Here's code for reproducing:

import numpy as np
from numpy.core import umath as um
a = np.ones((3,4))

# used in numpy/core/_methods.py
umr_any = um.logical_or.reduce

# np.any works as expected
if np.any(a == 0):
    pass

# but umr_any can't be used in if statement as here in `_mean`
if umr_any(a == 0):
    pass

Here's a colab reproduction: https://colab.research.google.com/drive/1UJi6E50zzaLu2nZQWMEFHPLZnq2v6pzT?usp=sharing

Thank you for any help!

  • Fixed keyword bug

  • Added test case

  • Reverted to original notation

  • Added tests for var and std

Closes gh-18552

…mpygh-18560)

* Fixed  keyword bug

* Added test case

* Reverted to original notation

* Added tests for var and std

Closes numpygh-18552
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Mar 14, 2021
@charris charris added this to the 1.20.2 release milestone Mar 14, 2021
@charris charris merged commit acaa91c into numpy:maintenance/1.20.x Mar 14, 2021
@charris charris deleted the backport-18560 branch March 16, 2021 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug 08 - Backport Used to tag backport PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants