Skip to content

Commit 32c5016

Browse files
committed
Merge pull request #8512 from unutbu/equal-warning
BUG: Suppress FutureWarning when comparing object arrays
2 parents 520170d + 8a662c6 commit 32c5016

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

doc/source/basics.rst

+10
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ locations treated as equal.
317317
318318
(df+df).equals(df*2)
319319
320+
Note that the Series or DataFrame index needs to be in the same order for
321+
equality to be True:
322+
323+
.. ipython:: python
324+
325+
df = DataFrame({'col':['foo', 0, np.nan]})
326+
df2 = DataFrame({'col':[np.nan, 0, 'foo']}, index=[2,1,0])
327+
df.equals(df2)
328+
df.equals(df2.sort())
329+
320330
321331
Combining overlapping data sets
322332
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/source/v0.13.1.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ API changes
121121

122122
.. ipython:: python
123123

124-
df = DataFrame({'col':['foo', 0, np.nan]}).sort()
124+
df = DataFrame({'col':['foo', 0, np.nan]})
125125
df2 = DataFrame({'col':[np.nan, 0, 'foo']}, index=[2,1,0])
126-
df.equals(df)
126+
df.equals(df2)
127+
df.equals(df2.sort())
127128

128129
import pandas.core.common as com
129130
com.array_equivalent(np.array([0, np.nan]), np.array([0, np.nan]))

doc/source/v0.15.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1110,3 +1110,4 @@ Bug Fixes
11101110
- Regression in ``NDFrame.loc`` indexing when rows/columns were converted to Float64Index if target was an empty list/ndarray (:issue:`7774`)
11111111
- Bug in ``Series`` that allows it to be indexed by a ``DataFrame`` which has unexpected results. Such indexing is no longer permitted (:issue:`8444`)
11121112
- Bug in item assignment of a ``DataFrame`` with multi-index columns where right-hand-side columns were not aligned (:issue:`7655`)
1113+
- Suppress FutureWarning generated by NumPy when comparing object arrays containing NaN for equality (:issue:`7065`)

pandas/core/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ def array_equivalent(left, right, strict_nan=False):
406406
>>> array_equivalent(np.array([1, nan, 2]), np.array([1, 2, nan]))
407407
False
408408
"""
409+
409410
left, right = np.asarray(left), np.asarray(right)
410411
if left.shape != right.shape: return False
411412

@@ -414,8 +415,8 @@ def array_equivalent(left, right, strict_nan=False):
414415

415416
if not strict_nan:
416417
# pd.isnull considers NaN and None to be equivalent.
417-
return ((left == right) | (pd.isnull(left) & pd.isnull(right))).all()
418-
418+
return lib.array_equivalent_object(left.ravel(), right.ravel())
419+
419420
for left_value, right_value in zip(left, right):
420421
if left_value is tslib.NaT and right_value is not tslib.NaT:
421422
return False
@@ -426,7 +427,6 @@ def array_equivalent(left, right, strict_nan=False):
426427
else:
427428
if left_value != right_value:
428429
return False
429-
430430
return True
431431

432432
# NaNs can occur in float and complex arrays.

pandas/core/index.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ def equals(self, other):
10471047
if type(other) != Index:
10481048
return other.equals(self)
10491049

1050-
return array_equivalent(self, other)
1050+
return array_equivalent(_values_from_object(self), _values_from_object(other))
10511051

10521052
def identical(self, other):
10531053
"""Similar to equals, but check that other comparable attributes are
@@ -2260,7 +2260,7 @@ def equals(self, other):
22602260
# return False
22612261

22622262
try:
2263-
return array_equivalent(self, other)
2263+
return array_equivalent(_values_from_object(self), _values_from_object(other))
22642264
except TypeError:
22652265
# e.g. fails in numpy 1.6 with DatetimeIndex #1681
22662266
return False
@@ -4175,7 +4175,8 @@ def equals(self, other):
41754175
return True
41764176

41774177
if not isinstance(other, MultiIndex):
4178-
return array_equivalent(self.values, _ensure_index(other))
4178+
return array_equivalent(self.values,
4179+
_values_from_object(_ensure_index(other)))
41794180

41804181
if self.nlevels != other.nlevels:
41814182
return False

pandas/lib.pyx

+20
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ 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+
333353
@cython.wraparound(False)
334354
@cython.boundscheck(False)
335355
def fast_unique(ndarray[object] values):

0 commit comments

Comments
 (0)