Skip to content

CLN: Exception catching in expressions #28650

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 27, 2019
Merged
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
46 changes: 18 additions & 28 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This did never happen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only errors that bubble up are TypeErrors, which we catch in the calling func in ops.array_ops


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)
Expand All @@ -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)
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use is_bool_dtype(x) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could, but thats non-performant, which is a big deal here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k

except AttributeError:
return isinstance(x, (bool, np.bool_))

Expand Down