Skip to content

CLN refactor core/computation #37585

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
Nov 2, 2020
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
12 changes: 7 additions & 5 deletions pandas/core/computation/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def _align_core_single_unary_op(
def _zip_axes_from_type(
typ: Type[FrameOrSeries], new_axes: Sequence[int]
) -> Dict[str, int]:
axes = {name: new_axes[i] for i, name in enumerate(typ._AXIS_ORDERS)}
return axes
return {name: new_axes[i] for i, name in enumerate(typ._AXIS_ORDERS)}


def _any_pandas_objects(terms) -> bool:
Expand Down Expand Up @@ -186,8 +185,11 @@ def reconstruct_object(typ, obj, axes, dtype):
# The condition is to distinguish 0-dim array (returned in case of
# scalar) and 1 element array
# e.g. np.array(0) and np.array([0])
if len(obj.shape) == 1 and len(obj) == 1:
if not isinstance(ret_value, np.ndarray):
ret_value = np.array([ret_value]).astype(res_t)
if (
len(obj.shape) == 1
and len(obj) == 1
and not isinstance(ret_value, np.ndarray)
):
ret_value = np.array([ret_value]).astype(res_t)

return ret_value
11 changes: 5 additions & 6 deletions pandas/core/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ def _check_engine(engine: Optional[str]) -> str:
# TODO: validate this in a more general way (thinking of future engines
# that won't necessarily be import-able)
# Could potentially be done on engine instantiation
if engine == "numexpr":
if not NUMEXPR_INSTALLED:
raise ImportError(
"'numexpr' is not installed or an unsupported version. Cannot use "
"engine='numexpr' for query/eval if 'numexpr' is not installed"
)
if engine == "numexpr" and not NUMEXPR_INSTALLED:
raise ImportError(
"'numexpr' is not installed or an unsupported version. Cannot use "
"engine='numexpr' for query/eval if 'numexpr' is not installed"
)

return engine

Expand Down
17 changes: 8 additions & 9 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,14 @@ def _maybe_evaluate_binop(
f"'{lhs.type}' and '{rhs.type}'"
)

if self.engine != "pytables":
if (
res.op in CMP_OPS_SYMS
and getattr(lhs, "is_datetime", False)
or getattr(rhs, "is_datetime", False)
):
# all date ops must be done in python bc numexpr doesn't work
# well with NaT
return self._maybe_eval(res, self.binary_ops)
if self.engine != "pytables" and (
res.op in CMP_OPS_SYMS
and getattr(lhs, "is_datetime", False)
or getattr(rhs, "is_datetime", False)
):
# all date ops must be done in python bc numexpr doesn't work
# well with NaT
return self._maybe_eval(res, self.binary_ops)

if res.op in eval_in_python:
# "in"/"not in" ops are always evaluated in python
Expand Down
16 changes: 8 additions & 8 deletions pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,14 @@ def prune(self, klass):
operand = self.operand
operand = operand.prune(klass)

if operand is not None:
if issubclass(klass, ConditionBinOp):
if operand.condition is not None:
return operand.invert()
elif issubclass(klass, FilterBinOp):
if operand.filter is not None:
return operand.invert()

if operand is not None and (
issubclass(klass, ConditionBinOp)
and operand.condition is not None
or not issubclass(klass, ConditionBinOp)
and issubclass(klass, FilterBinOp)
and operand.filter is not None
):
return operand.invert()
return None


Expand Down
3 changes: 1 addition & 2 deletions pandas/core/computation/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def __init__(
def __repr__(self) -> str:
scope_keys = _get_pretty_string(list(self.scope.keys()))
res_keys = _get_pretty_string(list(self.resolvers.keys()))
unicode_str = f"{type(self).__name__}(scope={scope_keys}, resolvers={res_keys})"
return unicode_str
return f"{type(self).__name__}(scope={scope_keys}, resolvers={res_keys})"

@property
def has_resolvers(self) -> bool:
Expand Down