diff --git a/example b/example new file mode 100644 index 0000000000000..442ae1a5f1591 Binary files /dev/null and b/example differ diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index 4ba7bce51ed64..e2b56128a00b6 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -24,7 +24,14 @@ from pandas.core.dtypes.missing import array_equivalent cdef bint isiterable(obj): - return hasattr(obj, "__iter__") + # GH#54507 + if hasattr(obj, "__iter__"): + try: + iter(obj) + return True + except (TypeError, IndexError) as e: + return False + return False cdef bint has_length(obj): diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 375003e58c21a..8602813879368 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -113,6 +113,9 @@ def __len__(self) -> int: def __array__(self, t=None): return np.asarray(self._values, dtype=t) + def __getitem__(self, item): + return self._values[item] + @property def ndim(self): return self._values.ndim diff --git a/pandas/tests/util/test_assert_almost_equal.py b/pandas/tests/util/test_assert_almost_equal.py index a86302f158005..6341fc57dba20 100644 --- a/pandas/tests/util/test_assert_almost_equal.py +++ b/pandas/tests/util/test_assert_almost_equal.py @@ -10,6 +10,7 @@ Timestamp, ) import pandas._testing as tm +from pandas.tests.dtypes.test_inference import MockNumpyLikeArray def _assert_almost_equal_both(a, b, **kwargs): @@ -454,6 +455,17 @@ def test_assert_almost_equal_shape_mismatch_override(): tm.assert_almost_equal(np.array([1, 2]), np.array([3, 4, 5]), obj="Index") +@pytest.mark.parametrize( + "obj", + [ + MockNumpyLikeArray(np.array(2)), + ], + ids=["duck-ndarray-0d"], +) +def test_assert_almost_equal_not_iterable(obj): + tm.assert_almost_equal(obj, obj) + + def test_assert_almost_equal_unicode(): # see gh-20503 msg = """numpy array are different