Skip to content

BUG: Check types in Index.__contains__ (#22085) #22360

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

Closed
wants to merge 16 commits into from
Closed
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ Indexing
- Fixed ``DataFrame[np.nan]`` when columns are non-unique (:issue:`21428`)
- Bug when indexing :class:`DatetimeIndex` with nanosecond resolution dates and timezones (:issue:`11679`)
- Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`)
- Bug in :func:`Index.__contains__` that returns True just in case val is float and dtype is integer, because float casted to integer (:issue:`22085`)
Copy link
Contributor

Choose a reason for hiding this comment

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

something more along the line of
Bug in 'scalar in Index' if scalar is a float while the Index is of integer dtype.


Missing
^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,11 @@ def __nonzero__(self):
def __contains__(self, key):
hash(key)
try:
return key in self._engine
if is_float(key) and is_integer_dtype(self.dtype) and\
Copy link
Contributor

Choose a reason for hiding this comment

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

don't use \ for line continuation, instead use parens

int(key) != key:
return False
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

don't need the else here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

return key in self._engine
Copy link
Contributor

Choose a reason for hiding this comment

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

this is ok in python code, but actually can you see if you can do this in the cython code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. But I'm not good at Cython now. I'd like to try it but it'll take time.

except (OverflowError, TypeError, ValueError):
return False

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,14 @@ def test_comparison_tzawareness_compat(self, op):
# TODO: implement _assert_tzawareness_compat for the reverse
# comparison with the Series on the left-hand side

def test_contains_with_float_val(self):
# GH#22085
index = pd.Index([0, 1, 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.

can you also add a Float index and test the same cases


assert 1.1 not in index
assert 1.0 in index
assert 1 in index


class TestIndexUtils(object):

Expand Down