From aac01fc4ee202a79b7169ea4456f9f2369c4a45b Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 11 Jul 2020 14:22:39 +0100 Subject: [PATCH 1/5] BUG: Inconsistent behavior in Index.difference --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/core/indexes/numeric.py | 22 ---------------------- pandas/tests/indexes/test_numeric.py | 24 ++++++++++++++++++++++++ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 5f93e08d51baa..91fca53d9abeb 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -931,6 +931,7 @@ Numeric - Bug in :meth:`DataFrame.diff` with ``axis=1`` returning incorrect results with mixed dtypes (:issue:`32995`) - Bug in :meth:`DataFrame.corr` and :meth:`DataFrame.cov` raising when handling nullable integer columns with ``pandas.NA`` (:issue:`33803`) - Bug in :class:`DataFrame` and :class:`Series` addition and subtraction between object-dtype objects and ``datetime64`` dtype objects (:issue:`33824`) +- Bug in :meth:`Index.difference` incorrect results when comparing a :class:`Float64Index` and object :class:`Index` (:issue:`35217`) Conversion ^^^^^^^^^^ diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 5020a25c88ff4..731907993d08f 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -400,28 +400,6 @@ def _format_native_types( ) return formatter.get_result_as_array() - def equals(self, other) -> bool: - """ - Determines if two Index objects contain the same elements. - """ - if self is other: - return True - - if not isinstance(other, Index): - return False - - # 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: - if not isinstance(other, Float64Index): - other = self._constructor(other) - if not is_dtype_equal(self.dtype, other.dtype) or self.shape != other.shape: - return False - left, right = self._values, other._values - return ((left == right) | (self._isnan & other._isnan)).all() - except (TypeError, ValueError): - return False - def __contains__(self, other: Any) -> bool: hash(other) if super().__contains__(other): diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 33de0800658f2..44e5bfa863c35 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -635,3 +635,27 @@ def test_uint_index_does_not_convert_to_float64(): tm.assert_index_equal(result.index, expected) tm.assert_equal(result, series[:3]) + + +def test_float64_index_equals(): + # https://github.com/pandas-dev/pandas/issues/35217 + float_index = pd.Index([1.0, 2, 3]) + string_index = pd.Index(["1", "2", "3"]) + + result = float_index.equals(string_index) + assert result is False + + result = string_index.equals(float_index) + assert result is False + + +def test_float64_index_difference(): + # https://github.com/pandas-dev/pandas/issues/35217 + float_index = pd.Index([1.0, 2, 3]) + string_index = pd.Index(["1", "2", "3"]) + + result = float_index.difference(string_index) + tm.assert_index_equal(result, float_index) + + result = string_index.difference(float_index) + tm.assert_index_equal(result, string_index) From 2f4902c998ae16141ce4483965d5dbecfd7fb339 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 13 Jul 2020 12:45:44 +0100 Subject: [PATCH 2/5] extend test_equals_numeric --- pandas/tests/indexes/test_numeric.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 44e5bfa863c35..83887ddbdb7ce 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -232,12 +232,24 @@ def test_equals_numeric(self): i2 = Float64Index([1.0, 2.0]) assert i.equals(i2) - i = Float64Index([1.0, np.nan]) - assert i.equals(i) - assert i.identical(i) + i3 = Float64Index([1.0, np.nan]) + assert i3.equals(i3) + assert i3.identical(i3) - i2 = Float64Index([1.0, np.nan]) - assert i.equals(i2) + i4 = Float64Index([1.0, np.nan]) + assert i3.equals(i4) + + i5 = Int64Index([1, 2]) + assert i.equals(i5) + assert i5.equals(i) + + i6 = Index([1.0, 2.0], dtype=object) + assert i.equals(i6) + assert i6.equals(i) + + i7 = Index([1, 2], dtype=object) + assert i.equals(i7) + assert i7.equals(i) @pytest.mark.parametrize( "vals", From c485af5785ba686f463bdf89bb9964d8e86923ed Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 15 Jul 2020 16:32:53 +0100 Subject: [PATCH 3/5] parameterize --- pandas/tests/indexes/test_numeric.py | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 83887ddbdb7ce..a7c5734ef9b02 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -232,24 +232,25 @@ def test_equals_numeric(self): i2 = Float64Index([1.0, 2.0]) assert i.equals(i2) - i3 = Float64Index([1.0, np.nan]) - assert i3.equals(i3) - assert i3.identical(i3) - - i4 = Float64Index([1.0, np.nan]) - assert i3.equals(i4) - - i5 = Int64Index([1, 2]) - assert i.equals(i5) - assert i5.equals(i) + i = Float64Index([1.0, np.nan]) + assert i.equals(i) + assert i.identical(i) - i6 = Index([1.0, 2.0], dtype=object) - assert i.equals(i6) - assert i6.equals(i) + i2 = Float64Index([1.0, np.nan]) + assert i.equals(i2) - i7 = Index([1, 2], dtype=object) - assert i.equals(i7) - assert i7.equals(i) + @pytest.mark.parametrize( + "other", + ( + Int64Index([1, 2]), + Index([1.0, 2.0], dtype=object), + Index([1, 2], dtype=object), + ), + ) + def test_equals_numeric_other_index_type(self, other): + i = Float64Index([1.0, 2.0]) + assert i.equals(other) + assert other.equals(i) @pytest.mark.parametrize( "vals", From bdedd82d3122af2b294f98e6ce1f10bec383163f Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 15 Jul 2020 17:24:58 +0100 Subject: [PATCH 4/5] empty commit From 5cb440674f9cb68a5fa8ea50d083036b4ae6728d Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 15 Jul 2020 18:21:24 +0100 Subject: [PATCH 5/5] empty commit