Skip to content

BUG Masked arrays treated incorrectly in isclose(..,..,equal_nan=True) #3914

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

Merged
merged 1 commit into from
Oct 16, 2013

Conversation

mhvk
Copy link
Contributor

@mhvk mhvk commented Oct 14, 2013

Currently, the mask of a MaskedArray is incorrectly changed in the following:

import numpy as np; from numpy import isclose, nan
m = np.ma.MaskedArray([nan, nan], mask=[True, False])
isclose(m, m, equal_nan=True)

yields

masked_array(data = [True True],
             mask = [False False],
       fill_value = True)

The problem lies in the fact that in isclose, the following final assignment is done

cond[isnan(x) & isnan(y)] = True 

The problem here is that the mask of the index is ignored in __setitem__ (and it would be very hard to do otherwise), but and hence all elements are being set. Since True has no mask, the mask of all ements is cleared. To avoid this, this PR ensured that the vallues used include a mask, by replacing the above line with

both_nan = isnan(x) & isnan(y)
cond[both_nan] = both_nan[both_nan]

A test case is included as well.

p.s. This bug was found while trying to allow MaskedArray to behave better with subclasses; see #3907

@charris
Copy link
Member

charris commented Oct 16, 2013

LGTM, thanks.

charris added a commit that referenced this pull request Oct 16, 2013
BUG Masked arrays treated incorrectly in isclose(..,..,equal_nan=True)
@charris charris merged commit 95a5219 into numpy:master Oct 16, 2013
@mhvk mhvk deleted the numeric/isclose-mask-safe branch October 16, 2013 18:30
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

Successfully merging this pull request may close these issues.

2 participants