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 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
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
12 changes: 1 addition & 11 deletions pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from contextlib import nullcontext
from datetime import datetime
from decimal import Decimal

Expand All @@ -7,7 +6,6 @@

from pandas._libs import missing as libmissing
from pandas._libs.tslibs import iNaT
from pandas.compat.numpy import np_version_gte1p25

from pandas.core.dtypes.common import (
is_float,
Expand Down Expand Up @@ -458,15 +456,7 @@ def test_array_equivalent_dti(dtype_equal):
)
def test_array_equivalent_series(val):
arr = np.array([1, 2])
msg = "elementwise comparison failed"
cm = (
# stacklevel is chosen to make sense when called from .equals
tm.assert_produces_warning(FutureWarning, match=msg, check_stacklevel=False)
if isinstance(val, str) and not np_version_gte1p25
else nullcontext()
)
with cm:
assert not array_equivalent(Series([arr, arr]), Series([arr, val]))
assert not array_equivalent(Series([arr, arr]), Series([arr, val]))


def test_array_equivalent_array_mismatched_shape():
Expand Down
12 changes: 1 addition & 11 deletions pandas/tests/series/methods/test_equals.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from contextlib import nullcontext
import copy

import numpy as np
import pytest

from pandas._libs.missing import is_matching_na
from pandas.compat.numpy import np_version_gte1p25

from pandas.core.dtypes.common import is_float

Expand All @@ -14,7 +12,6 @@
MultiIndex,
Series,
)
import pandas._testing as tm


@pytest.mark.parametrize(
Expand Down Expand Up @@ -48,14 +45,7 @@ def test_equals_list_array(val):
assert s1.equals(s2)

s1[1] = val

cm = (
tm.assert_produces_warning(FutureWarning, check_stacklevel=False)
if isinstance(val, str) and not np_version_gte1p25
else nullcontext()
)
with cm:
assert not s1.equals(s2)
assert not s1.equals(s2)


def test_equals_false_negative():
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