Skip to content

Commit 90d7eee

Browse files
jbrockmendeljreback
authored andcommitted
CLN: Exception catching in expressions (#28650)
1 parent 2988afb commit 90d7eee

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

pandas/core/computation/expressions.py

+18-28
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,12 @@ def _evaluate_numexpr(op, op_str, a, b, reversed=False):
107107

108108
a_value = getattr(a, "values", a)
109109
b_value = getattr(b, "values", b)
110-
try:
111-
result = ne.evaluate(
112-
"a_value {op} b_value".format(op=op_str),
113-
local_dict={"a_value": a_value, "b_value": b_value},
114-
casting="safe",
115-
)
116-
except ValueError as detail:
117-
if "unknown type object" in str(detail):
118-
pass
110+
111+
result = ne.evaluate(
112+
"a_value {op} b_value".format(op=op_str),
113+
local_dict={"a_value": a_value, "b_value": b_value},
114+
casting="safe",
115+
)
119116

120117
if _TEST_MODE:
121118
_store_test_result(result is not None)
@@ -140,21 +137,15 @@ def _where_numexpr(cond, a, b):
140137
a_value = getattr(a, "values", a)
141138
b_value = getattr(b, "values", b)
142139

143-
try:
144-
result = ne.evaluate(
145-
"where(cond_value, a_value, b_value)",
146-
local_dict={
147-
"cond_value": cond_value,
148-
"a_value": a_value,
149-
"b_value": b_value,
150-
},
151-
casting="safe",
152-
)
153-
except ValueError as detail:
154-
if "unknown type object" in str(detail):
155-
pass
156-
except Exception as detail:
157-
raise TypeError(str(detail))
140+
result = ne.evaluate(
141+
"where(cond_value, a_value, b_value)",
142+
local_dict={
143+
"cond_value": cond_value,
144+
"a_value": a_value,
145+
"b_value": b_value,
146+
},
147+
casting="safe",
148+
)
158149

159150
if result is None:
160151
result = _where_standard(cond, a, b)
@@ -167,11 +158,10 @@ def _where_numexpr(cond, a, b):
167158

168159

169160
def _has_bool_dtype(x):
161+
if isinstance(x, ABCDataFrame):
162+
return "bool" in x.dtypes
170163
try:
171-
if isinstance(x, ABCDataFrame):
172-
return "bool" in x.dtypes
173-
else:
174-
return x.dtype == bool
164+
return x.dtype == bool
175165
except AttributeError:
176166
return isinstance(x, (bool, np.bool_))
177167

0 commit comments

Comments
 (0)