Skip to content

Backport PR #35999 on branch 1.1.x (BUG: None in Float64Index raising TypeError, should return False) #36041

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Bug fixes
- Bug in :class:`Series` constructor raising a ``TypeError`` when constructing sparse datetime64 dtypes (:issue:`35762`)
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
- Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should bw ``""`` (:issue:`35712`)
-
- Bug in :meth:`Float64Index.__contains__` incorrectly raising ``TypeError`` instead of returning ``False`` (:issue:`35788`)

.. ---------------------------------------------------------------------------

Expand Down
6 changes: 5 additions & 1 deletion pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ cdef class IndexEngine:
values = self._get_index_values()

self._check_type(val)
loc = _bin_search(values, val) # .searchsorted(val, side='left')
try:
loc = _bin_search(values, val) # .searchsorted(val, side='left')
except TypeError:
# GH#35788 e.g. val=None with float64 values
raise KeyError(val)
if loc >= len(values):
raise KeyError(val)
if values[loc] != val:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/numeric/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ def test_take_fill_value_ints(self, klass):


class TestContains:
@pytest.mark.parametrize("klass", [Float64Index, Int64Index, UInt64Index])
def test_contains_none(self, klass):
# GH#35788 should return False, not raise TypeError
index = klass([0, 1, 2, 3, 4])
assert None not in index

def test_contains_float64_nans(self):
index = Float64Index([1.0, 2.0, np.nan])
assert np.nan in index
Expand Down