Skip to content

Commit c0f1288

Browse files
committed
API: use strict=True in array_equivalent_object
1 parent 1efb8c3 commit c0f1288

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

pandas/_libs/lib.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def maybe_booleans_to_slice(ndarray[uint8_t, ndim=1] mask):
573573

574574
@cython.wraparound(False)
575575
@cython.boundscheck(False)
576-
def array_equivalent_object(ndarray left, ndarray right) -> bool:
576+
def array_equivalent_object(ndarray left, ndarray right, strict=False) -> bool:
577577
"""
578578
Perform an element by element comparison on N-d object arrays
579579
taking into account nan positions.
@@ -599,14 +599,14 @@ def array_equivalent_object(ndarray left, ndarray right) -> bool:
599599
if x.shape != y.shape:
600600
return False
601601
if x.dtype == y.dtype == object:
602-
if not array_equivalent_object(x, y):
602+
if not array_equivalent_object(x, y, strict):
603603
return False
604604
else:
605605
# Circular import isn't great, but so it goes.
606606
# TODO: could use np.array_equal?
607607
from pandas.core.dtypes.missing import array_equivalent
608608

609-
if not array_equivalent(x, y):
609+
if not array_equivalent(x, y, strict):
610610
return False
611611

612612
elif PyArray_Check(x) or PyArray_Check(y):

pandas/core/dtypes/missing.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
)
4545
from pandas.core.dtypes.inference import is_list_like
4646

47+
import pandas as pd
48+
4749
if TYPE_CHECKING:
4850
from re import Pattern
4951

@@ -502,9 +504,15 @@ def _array_equivalent_object(
502504
mask = None
503505

504506
try:
507+
warnings.warn(
508+
"array_equivalent_object now uses strict=True for comparison. "
509+
"This may break code that relied on non-strict comparison.",
510+
pd.errors.FutureWarning,
511+
stacklevel=2,
512+
)
505513
if mask is None:
506-
return lib.array_equivalent_object(left, right)
507-
if not lib.array_equivalent_object(left[~mask], right[~mask]):
514+
return lib.array_equivalent_object(left, right, strict=True)
515+
if not lib.array_equivalent_object(left[~mask], right[~mask], strict=True):
508516
return False
509517
left_remaining = left[mask]
510518
right_remaining = right[mask]

pandas/tests/extension/base/missing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.core.dtypes.missing import _array_equivalent_object
5+
46
import pandas as pd
57
import pandas._testing as tm
68

@@ -179,3 +181,8 @@ def test_fillna_fill_other(self, data):
179181
expected = pd.DataFrame({"A": data, "B": [0.0] * len(result)})
180182

181183
tm.assert_frame_equal(result, expected)
184+
185+
def test_array_equivalent_object_strict_comparison(self):
186+
arr1 = np.array([1, 2, 3], dtype="int32")
187+
arr2 = np.array([1, 2, 3], dtype="int64")
188+
assert not _array_equivalent_object(arr1, arr2, strict_nan=False)

0 commit comments

Comments
 (0)