Skip to content

Commit c567de4

Browse files
committed
FIX: Bug whereby array_equivalent was not correctly comparing Float64Indexes with NaNs.
1 parent dc59749 commit c567de4

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pandas/core/common.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,14 @@ def array_equivalent(left, right):
311311
>>> array_equivalent(np.array([1, nan, 2]), np.array([1, 2, nan]))
312312
False
313313
"""
314+
left, right = np.asarray(left), np.asarray(right)
314315
if left.shape != right.shape: return False
315316
# NaNs occur only in object arrays, float or complex arrays.
317+
if issubclass(left.dtype.type, np.object_):
318+
return ((left == right) | (pd.isnull(left) & pd.isnull(right))).all()
316319
if not issubclass(left.dtype.type, (np.floating, np.complexfloating)):
317320
return np.array_equal(left, right)
318-
return ((left == right) | (np.isnan(left) & np.isnan(right))).all()
321+
return ((left == right) | (np.isnan(left) & np.isnan(right))).all()
319322

320323
def _iterable_not_string(x):
321324
return (isinstance(x, collections.Iterable) and

pandas/tests/test_common.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from nose.tools import assert_equal
66
import numpy as np
77
from pandas.tslib import iNaT, NaT
8-
from pandas import Series, DataFrame, date_range, DatetimeIndex, Timestamp
8+
from pandas import Series, DataFrame, date_range, DatetimeIndex, Timestamp, Float64Index
99
from pandas import compat
1010
from pandas.compat import range, long, lrange, lmap, u
1111
from pandas.core.common import notnull, isnull, array_equivalent
@@ -181,7 +181,11 @@ def test_array_equivalent():
181181
assert not array_equivalent(np.array([np.nan, 1, np.nan]),
182182
np.array([np.nan, 2, np.nan]))
183183
assert not array_equivalent(np.array(['a', 'b', 'c', 'd']), np.array(['e', 'e']))
184-
184+
assert array_equivalent(Float64Index([0, np.nan]), Float64Index([0, np.nan]))
185+
assert not array_equivalent(Float64Index([0, np.nan]), Float64Index([1, np.nan]))
186+
assert array_equivalent(DatetimeIndex([0, np.nan]), DatetimeIndex([0, np.nan]))
187+
assert not array_equivalent(DatetimeIndex([0, np.nan]), DatetimeIndex([1, np.nan]))
188+
185189
def test_datetimeindex_from_empty_datetime64_array():
186190
for unit in [ 'ms', 'us', 'ns' ]:
187191
idx = DatetimeIndex(np.array([], dtype='datetime64[%s]' % unit))

0 commit comments

Comments
 (0)