Skip to content

BUG: Inconsistent behavior in Index.difference #35231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,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`)
- Bug in :class:`DataFrame` reductions (e.g. ``df.min()``, ``df.max()``) with ``ExtensionArray`` dtypes (:issue:`34520`, :issue:`32651`)

Conversion
Expand Down
22 changes: 0 additions & 22 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
37 changes: 37 additions & 0 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ def test_equals_numeric(self):
i2 = Float64Index([1.0, np.nan])
assert i.equals(i2)

@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",
[
Expand Down Expand Up @@ -635,3 +648,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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally parameterize

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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

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)