8000 BUG: Fix array_equal for numeric and non-numeric scalar types by eendebakpt · Pull Request #27275 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix array_equal for numeric and non-numeric scalar types #27275

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 5 commits into from
Aug 26, 2024
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
16 changes: 8 additions & 8 deletions numpy/_core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2554,17 +2554,17 @@ def array_equal(a1, a2, equal_nan=False):
if a1.shape != a2.shape:
return False
if not equal_nan:
return builtins.bool((a1 == a2).all())
cannot_have_nan = (_dtype_cannot_hold_nan(a1.dtype)
and _dtype_cannot_hold_nan(a2.dtype))
if cannot_have_nan:
if a1 is a2:
return True
return builtins.bool((a1 == a2).all())
return builtins.bool((asanyarray(a1 == a2)).all())

if a1 is a2:
# nan will compare equal so an array will compare equal to itself.
return True

cannot_have_nan = (_dtype_cannot_hold_nan(a1.dtype)
and _dtype_cannot_hold_nan(a2.dtype))
if cannot_have_nan:
return builtins.bool(asarray(a1 == a2).all())

# Handling NaN values if equal_nan is True
a1nan, a2nan = isnan(a1), isnan(a2)
# NaN's occur at different locations
Expand Down Expand Up @@ -2624,7 +2624,7 @@ def array_equiv(a1, a2):
except Exception:
return False

return builtins.bool((a1 == a2).all())
return builtins.bool(asanyarray(a1 == a2).all())


def _astype_dispatcher(x, dtype, /, *, copy=None, device=None):
Expand Down
7 changes: 7 additions & 0 deletions numpy/_core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,13 @@ def test_array_equal_equal_nan(self, bx, by, equal_nan, expected):
assert_(res is expected)
assert_(type(res) is bool)

def test_array_equal_different_scalar_types(self):
# https://github.com/numpy/numpy/issues/27271
a = np.array("foo")
b = np.array(1)
assert not np.array_equal(a, b)
assert not np.array_equiv(a, b)

def test_none_compares_elementwise(self):
a = np.array([None, 1, None], dtype=object)
assert_equal(a == None, [True, False, True])
Expand Down
0