From 8b468b2dd3ce92fb5411393cc9ad859917b34ed9 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 26 Sep 2019 16:56:45 -0700 Subject: [PATCH] CLN: Exception catching in expressions --- pandas/core/computation/expressions.py | 46 ++++++++++---------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/pandas/core/computation/expressions.py b/pandas/core/computation/expressions.py index 90bb12b4cd727..46bc762e1a0b3 100644 --- a/pandas/core/computation/expressions.py +++ b/pandas/core/computation/expressions.py @@ -107,15 +107,12 @@ def _evaluate_numexpr(op, op_str, a, b, reversed=False): a_value = getattr(a, "values", a) b_value = getattr(b, "values", b) - try: - result = ne.evaluate( - "a_value {op} b_value".format(op=op_str), - local_dict={"a_value": a_value, "b_value": b_value}, - casting="safe", - ) - except ValueError as detail: - if "unknown type object" in str(detail): - pass + + result = ne.evaluate( + "a_value {op} b_value".format(op=op_str), + local_dict={"a_value": a_value, "b_value": b_value}, + casting="safe", + ) if _TEST_MODE: _store_test_result(result is not None) @@ -140,21 +137,15 @@ def _where_numexpr(cond, a, b): a_value = getattr(a, "values", a) b_value = getattr(b, "values", b) - try: - result = ne.evaluate( - "where(cond_value, a_value, b_value)", - local_dict={ - "cond_value": cond_value, - "a_value": a_value, - "b_value": b_value, - }, - casting="safe", - ) - except ValueError as detail: - if "unknown type object" in str(detail): - pass - except Exception as detail: - raise TypeError(str(detail)) + result = ne.evaluate( + "where(cond_value, a_value, b_value)", + local_dict={ + "cond_value": cond_value, + "a_value": a_value, + "b_value": b_value, + }, + casting="safe", + ) if result is None: result = _where_standard(cond, a, b) @@ -167,11 +158,10 @@ def _where_numexpr(cond, a, b): def _has_bool_dtype(x): + if isinstance(x, ABCDataFrame): + return "bool" in x.dtypes try: - if isinstance(x, ABCDataFrame): - return "bool" in x.dtypes - else: - return x.dtype == bool + return x.dtype == bool except AttributeError: return isinstance(x, (bool, np.bool_))