Skip to content

Commit e27ef39

Browse files
authored
PERF: avoid warnings in IndexEngine.get_loc (#43792)
1 parent f10bbe9 commit e27ef39

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

pandas/_libs/index.pyx

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import warnings
2-
31
cimport cython
42

53
import numpy as np

pandas/_libs/index_class_helper.pxi.in

+11-18
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ cdef class {{name}}Engine(IndexEngine):
3737
if not util.is_integer_object(val):
3838
raise KeyError(val)
3939
{{else}}
40-
if util.is_bool_object(val):
41-
# avoid casting to True -> 1.0
40+
if not util.is_integer_object(val) and not util.is_float_object(val):
41+
# in particular catch bool and avoid casting True -> 1.0
4242
raise KeyError(val)
4343
{{endif}}
4444

@@ -53,22 +53,15 @@ cdef class {{name}}Engine(IndexEngine):
5353
self._check_type(val)
5454

5555
values = self.values
56-
try:
57-
with warnings.catch_warnings():
58-
# e.g. if values is float64 and `val` is a str, suppress warning
59-
warnings.filterwarnings("ignore", category=FutureWarning)
60-
{{if name in {'Float64', 'Float32'} }}
61-
if util.is_nan(val):
62-
indexer = np.isnan(values)
63-
else:
64-
indexer = values == val
65-
{{else}}
66-
indexer = values == val
67-
{{endif}}
68-
except TypeError:
69-
# if the equality above returns a bool, cython will raise TypeError
70-
# when trying to cast it to ndarray
71-
raise KeyError(val)
56+
57+
{{if name in {'Float64', 'Float32'} }}
58+
if util.is_nan(val):
59+
indexer = np.isnan(values)
60+
else:
61+
indexer = values == val
62+
{{else}}
63+
indexer = values == val
64+
{{endif}}
7265

7366
return indexer
7467

0 commit comments

Comments
 (0)