Skip to content

Commit d38627b

Browse files
jbrockmendeljreback
authored andcommitted
CLN/BUG: array_equivalent on nested objects (#28347)
1 parent 54e9b75 commit d38627b

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

pandas/_libs/testing.pyx

+3-5
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,9 @@ cpdef assert_almost_equal(a, b,
143143
from pandas.util.testing import assert_attr_equal
144144
assert_attr_equal('dtype', a, b, obj=obj)
145145

146-
try:
147-
if array_equivalent(a, b, strict_nan=True):
148-
return True
149-
except:
150-
pass
146+
if array_equivalent(a, b, strict_nan=True):
147+
return True
148+
151149
else:
152150
na, nb = len(a), len(b)
153151

pandas/core/dtypes/missing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def array_equivalent(left, right, strict_nan=False):
445445
if not isinstance(right_value, float) or not np.isnan(right_value):
446446
return False
447447
else:
448-
if left_value != right_value:
448+
if np.any(left_value != right_value):
449449
return False
450450
return True
451451

pandas/tests/dtypes/test_missing.py

+14
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,20 @@ def test_array_equivalent_str():
360360
)
361361

362362

363+
def test_array_equivalent_nested():
364+
# reached in groupby aggregations, make sure we use np.any when checking
365+
# if the comparison is truthy
366+
left = np.array([np.array([50, 70, 90]), np.array([20, 30, 40])], dtype=object)
367+
right = np.array([np.array([50, 70, 90]), np.array([20, 30, 40])], dtype=object)
368+
369+
assert array_equivalent(left, right, strict_nan=True)
370+
assert not array_equivalent(left, right[::-1], strict_nan=True)
371+
372+
left = np.array([np.array([50, 50, 50]), np.array([40, 40, 40])], dtype=object)
373+
right = np.array([50, 40])
374+
assert not array_equivalent(left, right, strict_nan=True)
375+
376+
363377
@pytest.mark.parametrize(
364378
"dtype, na_value",
365379
[

0 commit comments

Comments
 (0)