diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index c407de4cf7465..7ad5ea189763c 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -143,11 +143,9 @@ cpdef assert_almost_equal(a, b, from pandas.util.testing import assert_attr_equal assert_attr_equal('dtype', a, b, obj=obj) - try: - if array_equivalent(a, b, strict_nan=True): - return True - except: - pass + if array_equivalent(a, b, strict_nan=True): + return True + else: na, nb = len(a), len(b) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 056cd2222af3c..6dd032b9248ed 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -445,7 +445,7 @@ def array_equivalent(left, right, strict_nan=False): if not isinstance(right_value, float) or not np.isnan(right_value): return False else: - if left_value != right_value: + if np.any(left_value != right_value): return False return True diff --git a/pandas/tests/dtypes/test_missing.py b/pandas/tests/dtypes/test_missing.py index bbc485ecf94f2..1a292d5bfcbb6 100644 --- a/pandas/tests/dtypes/test_missing.py +++ b/pandas/tests/dtypes/test_missing.py @@ -360,6 +360,20 @@ def test_array_equivalent_str(): ) +def test_array_equivalent_nested(): + # reached in groupby aggregations, make sure we use np.any when checking + # if the comparison is truthy + left = np.array([np.array([50, 70, 90]), np.array([20, 30, 40])], dtype=object) + right = np.array([np.array([50, 70, 90]), np.array([20, 30, 40])], dtype=object) + + assert array_equivalent(left, right, strict_nan=True) + assert not array_equivalent(left, right[::-1], strict_nan=True) + + left = np.array([np.array([50, 50, 50]), np.array([40, 40, 40])], dtype=object) + right = np.array([50, 40]) + assert not array_equivalent(left, right, strict_nan=True) + + @pytest.mark.parametrize( "dtype, na_value", [