From befc68845d151dcefe722978b379b5e3143bf008 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 28 Sep 2021 21:12:59 -0700 Subject: [PATCH] PERF: avoid warnings in IndexEngine.get_loc --- pandas/_libs/index.pyx | 2 -- pandas/_libs/index_class_helper.pxi.in | 29 ++++++++++---------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 0cdb3518cd97c..185cbf6fd91b1 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -1,5 +1,3 @@ -import warnings - cimport cython import numpy as np diff --git a/pandas/_libs/index_class_helper.pxi.in b/pandas/_libs/index_class_helper.pxi.in index 3740c25528f8f..0386af5d12dd8 100644 --- a/pandas/_libs/index_class_helper.pxi.in +++ b/pandas/_libs/index_class_helper.pxi.in @@ -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}} @@ -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