diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 83a1b09f00a11..fa6f1ef288a13 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -573,7 +573,7 @@ def maybe_booleans_to_slice(ndarray[uint8_t, ndim=1] mask): @cython.wraparound(False) @cython.boundscheck(False) -def array_equivalent_object(ndarray left, ndarray right) -> bool: +def array_equivalent_object(ndarray left, ndarray right, strict=False) -> bool: """ Perform an element by element comparison on N-d object arrays taking into account nan positions. @@ -599,14 +599,14 @@ def array_equivalent_object(ndarray left, ndarray right) -> bool: if x.shape != y.shape: return False if x.dtype == y.dtype == object: - if not array_equivalent_object(x, y): + if not array_equivalent_object(x, y, strict): return False else: # Circular import isn't great, but so it goes. # TODO: could use np.array_equal? from pandas.core.dtypes.missing import array_equivalent - if not array_equivalent(x, y): + if not array_equivalent(x, y, strict): return False elif PyArray_Check(x) or PyArray_Check(y): diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 1dd4ed7100ccf..b3b540ebdeaa6 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -503,8 +503,8 @@ def _array_equivalent_object( try: if mask is None: - return lib.array_equivalent_object(left, right) - if not lib.array_equivalent_object(left[~mask], right[~mask]): + return lib.array_equivalent_object(left, right, strict=True) + if not lib.array_equivalent_object(left[~mask], right[~mask], strict=True): return False left_remaining = left[mask] right_remaining = right[mask] diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index cee565d4f7c1e..7f2fad5e4b781 100644 --- a/pandas/tests/extension/base/missing.py +++ b/pandas/tests/extension/base/missing.py @@ -1,6 +1,8 @@ import numpy as np import pytest +from pandas.core.dtypes.missing import _array_equivalent_object + import pandas as pd import pandas._testing as tm @@ -179,3 +181,8 @@ def test_fillna_fill_other(self, data): expected = pd.DataFrame({"A": data, "B": [0.0] * len(result)}) tm.assert_frame_equal(result, expected) + + def test_array_equivalent_object_strict_comparison(self): + arr1 = np.array([1, 2, 3], dtype="int32") + arr2 = np.array([1, 2, 3], dtype="int64") + assert not _array_equivalent_object(arr1, arr2, strict_nan=False)