From d21bece131e4d34c80bc90a4cdfcbf647c58a591 Mon Sep 17 00:00:00 2001 From: jreback Date: Tue, 18 Feb 2014 17:33:05 -0500 Subject: [PATCH] BUG: Float64Index with nans not comparing correctly --- doc/source/release.rst | 1 + pandas/core/index.py | 8 +++++++- pandas/core/internals.py | 2 +- pandas/tests/test_index.py | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 2a7506ac4788e..4ddf125905204 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -141,6 +141,7 @@ Bug Fixes - Bug in hdfstore queries of the form ``where=[('date', '>=', datetime(2013,1,1)), ('date', '<=', datetime(2014,1,1))]`` (:issue:`6313`) - Bug in DataFrame.dropna with duplicate indices (:issue:`6355`) - Regression in chained getitem indexing with embedded list-like from 0.12 (:issue:`6394`) +- ``Float64Index`` with nans not comparing correctly pandas 0.13.1 ------------- diff --git a/pandas/core/index.py b/pandas/core/index.py index 405e584454c06..46e1fef9984f6 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -1950,8 +1950,14 @@ def equals(self, other): if self is other: return True + # need to compare nans locations and make sure that they are the same + # since nans don't compare equal this is a bit tricky try: - return np.array_equal(self, other) + if not isinstance(other, Float64Index): + other = self._constructor(other) + if self.dtype != other.dtype or self.shape != other.shape: return False + left, right = self.values, other.values + return ((left == right) | (isnull(left) & isnull(right))).all() except TypeError: # e.g. fails in numpy 1.6 with DatetimeIndex #1681 return False diff --git a/pandas/core/internals.py b/pandas/core/internals.py index e68fc8da6a5db..761fd6b4af61c 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1184,12 +1184,12 @@ class NumericBlock(Block): class FloatOrComplexBlock(NumericBlock): + def equals(self, other): if self.dtype != other.dtype or self.shape != other.shape: return False left, right = self.values, other.values return ((left == right) | (np.isnan(left) & np.isnan(right))).all() - class FloatBlock(FloatOrComplexBlock): is_float = True _downcast_dtype = 'int64' diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 1668bcb1e8d1f..d8625c8687f79 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -852,6 +852,21 @@ def test_astype(self): self.assert_(i.equals(result)) self.check_is_index(result) + def test_equals(self): + + i = Float64Index([1.0,2.0]) + self.assertTrue(i.equals(i)) + self.assertTrue(i.identical(i)) + + i2 = Float64Index([1.0,2.0]) + self.assertTrue(i.equals(i2)) + + i = Float64Index([1.0,np.nan]) + self.assertTrue(i.equals(i)) + self.assertTrue(i.identical(i)) + + i2 = Float64Index([1.0,np.nan]) + self.assertTrue(i.equals(i2)) class TestInt64Index(tm.TestCase): _multiprocess_can_split_ = True