Skip to content

Commit 1b16a79

Browse files
BUG: Inconsistent behavior in Index.difference (#35231)
1 parent f0d57d9 commit 1b16a79

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ Numeric
954954
- Bug in :meth:`DataFrame.diff` with ``axis=1`` returning incorrect results with mixed dtypes (:issue:`32995`)
955955
- Bug in :meth:`DataFrame.corr` and :meth:`DataFrame.cov` raising when handling nullable integer columns with ``pandas.NA`` (:issue:`33803`)
956956
- Bug in :class:`DataFrame` and :class:`Series` addition and subtraction between object-dtype objects and ``datetime64`` dtype objects (:issue:`33824`)
957+
- Bug in :meth:`Index.difference` incorrect results when comparing a :class:`Float64Index` and object :class:`Index` (:issue:`35217`)
957958
- Bug in :class:`DataFrame` reductions (e.g. ``df.min()``, ``df.max()``) with ``ExtensionArray`` dtypes (:issue:`34520`, :issue:`32651`)
958959

959960
Conversion

pandas/core/indexes/numeric.py

-22
Original file line numberDiff line numberDiff line change
@@ -400,28 +400,6 @@ def _format_native_types(
400400
)
401401
return formatter.get_result_as_array()
402402

403-
def equals(self, other) -> bool:
404-
"""
405-
Determines if two Index objects contain the same elements.
406-
"""
407-
if self is other:
408-
return True
409-
410-
if not isinstance(other, Index):
411-
return False
412-
413-
# need to compare nans locations and make sure that they are the same
414-
# since nans don't compare equal this is a bit tricky
415-
try:
416-
if not isinstance(other, Float64Index):
417-
other = self._constructor(other)
418-
if not is_dtype_equal(self.dtype, other.dtype) or self.shape != other.shape:
419-
return False
420-
left, right = self._values, other._values
421-
return ((left == right) | (self._isnan & other._isnan)).all()
422-
except (TypeError, ValueError):
423-
return False
424-
425403
def __contains__(self, other: Any) -> bool:
426404
hash(other)
427405
if super().__contains__(other):

pandas/tests/indexes/test_numeric.py

+37
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,19 @@ def test_equals_numeric(self):
239239
i2 = Float64Index([1.0, np.nan])
240240
assert i.equals(i2)
241241

242+
@pytest.mark.parametrize(
243+
"other",
244+
(
245+
Int64Index([1, 2]),
246+
Index([1.0, 2.0], dtype=object),
247+
Index([1, 2], dtype=object),
248+
),
249+
)
250+
def test_equals_numeric_other_index_type(self, other):
251+
i = Float64Index([1.0, 2.0])
252+
assert i.equals(other)
253+
assert other.equals(i)
254+
242255
@pytest.mark.parametrize(
243256
"vals",
244257
[
@@ -635,3 +648,27 @@ def test_uint_index_does_not_convert_to_float64():
635648
tm.assert_index_equal(result.index, expected)
636649

637650
tm.assert_equal(result, series[:3])
651+
652+
653+
def test_float64_index_equals():
654+
# https://github.com/pandas-dev/pandas/issues/35217
655+
float_index = pd.Index([1.0, 2, 3])
656+
string_index = pd.Index(["1", "2", "3"])
657+
658+
result = float_index.equals(string_index)
659+
assert result is False
660+
661+
result = string_index.equals(float_index)
662+
assert result is False
663+
664+
665+
def test_float64_index_difference():
666+
# https://github.com/pandas-dev/pandas/issues/35217
667+
float_index = pd.Index([1.0, 2, 3])
668+
string_index = pd.Index(["1", "2", "3"])
669+
670+
result = float_index.difference(string_index)
671+
tm.assert_index_equal(result, float_index)
672+
673+
result = string_index.difference(float_index)
674+
tm.assert_index_equal(result, string_index)

0 commit comments

Comments
 (0)