Skip to content

PERF: avoid warnings in IndexEngine.get_loc #43792

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 1 commit into from
Sep 29, 2021
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: 0 additions & 2 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import warnings

cimport cython

import numpy as np
Expand Down
29 changes: 11 additions & 18 deletions pandas/_libs/index_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ cdef class {{name}}Engine(IndexEngine):
if not util.is_integer_object(val):
raise KeyError(val)
{{else}}
if util.is_bool_object(val):
# avoid casting to True -> 1.0
if not util.is_integer_object(val) and not util.is_float_object(val):
# in particular catch bool and avoid casting True -> 1.0
raise KeyError(val)
{{endif}}

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

values = self.values
try:
with warnings.catch_warnings():
# e.g. if values is float64 and `val` is a str, suppress warning
warnings.filterwarnings("ignore", category=FutureWarning)
{{if name in {'Float64', 'Float32'} }}
if util.is_nan(val):
indexer = np.isnan(values)
else:
indexer = values == val
{{else}}
indexer = values == val
{{endif}}
except TypeError:
# if the equality above returns a bool, cython will raise TypeError
# when trying to cast it to ndarray
raise KeyError(val)

{{if name in {'Float64', 'Float32'} }}
if util.is_nan(val):
indexer = np.isnan(values)
else:
indexer = values == val
{{else}}
indexer = values == val
{{endif}}

return indexer

Expand Down