Skip to content

Commit ec31259

Browse files
committed
Merge pull request #8570 from jreback/equals
PERF: improve perf of array_equivalent_object (GH8512)
2 parents 09e0ba7 + 62913bc commit ec31259

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

pandas/core/common.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,13 @@ def array_equivalent(left, right, strict_nan=False):
411411
if left.shape != right.shape: return False
412412

413413
# Object arrays can contain None, NaN and NaT.
414-
if issubclass(left.dtype.type, np.object_):
414+
if issubclass(left.dtype.type, np.object_) or issubclass(right.dtype.type, np.object_):
415415

416416
if not strict_nan:
417417
# pd.isnull considers NaN and None to be equivalent.
418-
return lib.array_equivalent_object(left.ravel(), right.ravel())
419-
418+
return lib.array_equivalent_object(_ensure_object(left.ravel()),
419+
_ensure_object(right.ravel()))
420+
420421
for left_value, right_value in zip(left, right):
421422
if left_value is tslib.NaT and right_value is not tslib.NaT:
422423
return False

pandas/lib.pyx

+25-20
Original file line numberDiff line numberDiff line change
@@ -330,26 +330,6 @@ def list_to_object_array(list obj):
330330
return arr
331331

332332

333-
@cython.wraparound(False)
334-
@cython.boundscheck(False)
335-
def array_equivalent_object(ndarray left, ndarray right):
336-
cdef Py_ssize_t i, n
337-
cdef object lobj, robj
338-
339-
n = len(left)
340-
for i from 0 <= i < n:
341-
lobj = left[i]
342-
robj = right[i]
343-
344-
# we are either not equal or both nan
345-
# I think None == None will be true here
346-
if lobj != robj:
347-
if checknull(lobj) and checknull(robj):
348-
continue
349-
return False
350-
return True
351-
352-
353333
@cython.wraparound(False)
354334
@cython.boundscheck(False)
355335
def fast_unique(ndarray[object] values):
@@ -692,6 +672,31 @@ def scalar_compare(ndarray[object] values, object val, object op):
692672

693673
return result.view(bool)
694674

675+
@cython.wraparound(False)
676+
@cython.boundscheck(False)
677+
def array_equivalent_object(ndarray[object] left, ndarray[object] right):
678+
""" perform an element by element comparion on 1-d object arrays
679+
taking into account nan positions """
680+
cdef Py_ssize_t i, n
681+
cdef object x, y
682+
683+
n = len(left)
684+
for i from 0 <= i < n:
685+
x = left[i]
686+
y = right[i]
687+
688+
# we are either not equal or both nan
689+
# I think None == None will be true here
690+
if cpython.PyObject_RichCompareBool(x, y, cpython.Py_EQ):
691+
continue
692+
elif _checknull(x) and _checknull(y):
693+
continue
694+
else:
695+
return False
696+
697+
return True
698+
699+
695700
@cython.wraparound(False)
696701
@cython.boundscheck(False)
697702
def vec_compare(ndarray[object] left, ndarray[object] right, object op):

0 commit comments

Comments
 (0)