Skip to content

Commit b528be6

Browse files
authored
BUG: None in Float64Index raising TypeError, should return False (#35999)
1 parent d7f30b4 commit b528be6

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

doc/source/whatsnew/v1.1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Bug fixes
3131
- Bug in :class:`Series` constructor raising a ``TypeError`` when constructing sparse datetime64 dtypes (:issue:`35762`)
3232
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
3333
- Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should bw ``""`` (:issue:`35712`)
34-
-
34+
- Bug in :meth:`Float64Index.__contains__` incorrectly raising ``TypeError`` instead of returning ``False`` (:issue:`35788`)
3535

3636
.. ---------------------------------------------------------------------------
3737

pandas/_libs/index.pyx

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ cdef class IndexEngine:
8080
values = self._get_index_values()
8181

8282
self._check_type(val)
83-
loc = _bin_search(values, val) # .searchsorted(val, side='left')
83+
try:
84+
loc = _bin_search(values, val) # .searchsorted(val, side='left')
85+
except TypeError:
86+
# GH#35788 e.g. val=None with float64 values
87+
raise KeyError(val)
8488
if loc >= len(values):
8589
raise KeyError(val)
8690
if values[loc] != val:

pandas/tests/indexes/numeric/test_indexing.py

+6
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ def test_take_fill_value_ints(self, klass):
228228

229229

230230
class TestContains:
231+
@pytest.mark.parametrize("klass", [Float64Index, Int64Index, UInt64Index])
232+
def test_contains_none(self, klass):
233+
# GH#35788 should return False, not raise TypeError
234+
index = klass([0, 1, 2, 3, 4])
235+
assert None not in index
236+
231237
def test_contains_float64_nans(self):
232238
index = Float64Index([1.0, 2.0, np.nan])
233239
assert np.nan in index

0 commit comments

Comments
 (0)