Skip to content

Commit d21bece

Browse files
committed
BUG: Float64Index with nans not comparing correctly
1 parent 8517c05 commit d21bece

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Bug Fixes
141141
- Bug in hdfstore queries of the form ``where=[('date', '>=', datetime(2013,1,1)), ('date', '<=', datetime(2014,1,1))]`` (:issue:`6313`)
142142
- Bug in DataFrame.dropna with duplicate indices (:issue:`6355`)
143143
- Regression in chained getitem indexing with embedded list-like from 0.12 (:issue:`6394`)
144+
- ``Float64Index`` with nans not comparing correctly
144145

145146
pandas 0.13.1
146147
-------------

pandas/core/index.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1950,8 +1950,14 @@ def equals(self, other):
19501950
if self is other:
19511951
return True
19521952

1953+
# need to compare nans locations and make sure that they are the same
1954+
# since nans don't compare equal this is a bit tricky
19531955
try:
1954-
return np.array_equal(self, other)
1956+
if not isinstance(other, Float64Index):
1957+
other = self._constructor(other)
1958+
if self.dtype != other.dtype or self.shape != other.shape: return False
1959+
left, right = self.values, other.values
1960+
return ((left == right) | (isnull(left) & isnull(right))).all()
19551961
except TypeError:
19561962
# e.g. fails in numpy 1.6 with DatetimeIndex #1681
19571963
return False

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1184,12 +1184,12 @@ class NumericBlock(Block):
11841184

11851185

11861186
class FloatOrComplexBlock(NumericBlock):
1187+
11871188
def equals(self, other):
11881189
if self.dtype != other.dtype or self.shape != other.shape: return False
11891190
left, right = self.values, other.values
11901191
return ((left == right) | (np.isnan(left) & np.isnan(right))).all()
11911192

1192-
11931193
class FloatBlock(FloatOrComplexBlock):
11941194
is_float = True
11951195
_downcast_dtype = 'int64'

pandas/tests/test_index.py

+15
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,21 @@ def test_astype(self):
852852
self.assert_(i.equals(result))
853853
self.check_is_index(result)
854854

855+
def test_equals(self):
856+
857+
i = Float64Index([1.0,2.0])
858+
self.assertTrue(i.equals(i))
859+
self.assertTrue(i.identical(i))
860+
861+
i2 = Float64Index([1.0,2.0])
862+
self.assertTrue(i.equals(i2))
863+
864+
i = Float64Index([1.0,np.nan])
865+
self.assertTrue(i.equals(i))
866+
self.assertTrue(i.identical(i))
867+
868+
i2 = Float64Index([1.0,np.nan])
869+
self.assertTrue(i.equals(i2))
855870

856871
class TestInt64Index(tm.TestCase):
857872
_multiprocess_can_split_ = True

0 commit comments

Comments
 (0)