Skip to content

BUG: avoid triggering numpy deprecation warning in assert functions for nested array with empty array/list #59778

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
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ def array_equivalent_object(ndarray left, ndarray right) -> bool:
if not array_equivalent(x, y):
return False

elif PyArray_Check(x) or PyArray_Check(y):
return False
Comment on lines +603 to +604
Copy link
Member Author

Choose a reason for hiding this comment

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

Below we would essentially do bool(left == right) which raises an error for arrays/lists of size > 1 (and that error would then be caught below, turned into return False. So for now, just directly returning False here when left or right is a numpy array and the other not.

That then avoids doing this bool(left == right) for empty array/list case, which triggers the warning.

Copy link
Member

Choose a reason for hiding this comment

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

So for now, just directly returning False here when left or right is a numpy array and the other not.

Did you mean to use xor instead of or?

Copy link
Member Author

Choose a reason for hiding this comment

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

The first if just before is if PyArray_Check(x) and PyArray_Check(y), so the or is sufficient

elif (x is C_NA) ^ (y is C_NA):
return False
elif not (
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/util/test_assert_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ def test_assert_almost_equal_iterable_values_mismatch():
np.array([np.array([1, 2, 3]), np.array([4, 5])], dtype=object),
np.array([[1, 2, 3], [4, 5]], dtype=object),
),
(
np.array([np.array([], dtype=object), None], dtype=object),
np.array([[], None], dtype=object),
),
(
np.array(
[
Expand Down
Loading