Skip to content

Enable assert_almost_equal to test numpy-like zero-dimensional arrays #54507

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

Closed
wants to merge 3 commits into from
Closed
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
Binary file added example
Binary file not shown.
9 changes: 8 additions & 1 deletion pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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__"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel ok here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment # GH#54507 here

how does this affect the runtime of the test suite? if its neglible then im fine with it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem to affect the runtime of the test suite by much

try:
iter(obj)
return True
except (TypeError, IndexError) as e:
return False
return False


cdef bint has_length(obj):
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/util/test_assert_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down