8000 BUG: ensure nomask in comparison result is not broadcast by mhvk · Pull Request #24559 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: ensure nomask in comparison result is not broadcast #24559

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
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4153,17 +4153,18 @@ def _comparison(self, other, compare):
if isinstance(check, (np.bool_, bool)):
return masked if mask else check

if mask is not nomask and compare in (operator.eq, operator.ne):
# Adjust elements that were masked, which should be treated
# as equal if masked in both, unequal if masked in one.
# Note that this works automatically for structured arrays too.
# Ignore this for operations other than `==` and `!=`
check = np.where(mask, compare(smask, omask), check)

if mask.shape != check.shape:
# Guarantee consistency of the shape, making a copy since the
# the mask may need to get written to later.
mask = np.broadcast_to(mask, check.shape).copy()
if mask is not nomask:
if compare in (operator.eq, operator.ne):
# Adjust elements that were masked, which should be treated
# as equal if masked in both, unequal if masked in one.
# Note that this works automatically for structured arrays too.
# Ignore this for operations other than `==` and `!=`
check = np.where(mask, compare(smask, omask), check)

if mask.shape != check.shape:
# Guarantee consistency of the shape, making a copy since the
# the mask may need to get written to later.
mask = np.broadcast_to(mask, check.shape).copy()

check = check.view(type(self))
check._update_from(self)
Expand Down
9 changes: 9 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,15 @@ def test_eq_broadcast_with_unmasked(self, op):
assert_(result.mask.shape == b.shape)
assert_equal(result.mask, np.zeros(b.shape, bool) | a.mask)

@pytest.mark.parametrize("op", [operator.eq, operator.gt])
def test_comp_no_mask_not_broadcast(self, op):
# Regression test for failing doctest in MaskedArray.nonzero
# after gh-24556.
a = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
result = op(a, 3)
assert_(not result.mask.shape)
assert_(result.mask is nomask)

@pytest.mark.parametrize('dt1', num_dts, ids=num_ids)
@pytest.mark.parametrize('dt2', num_dts, ids=num_ids)
@pytest.mark.parametrize('fill', [None, 1])
Expand Down
0