Skip to content

Commit d2bc94a

Browse files
authored
refactor core/computation (#37585)
1 parent 8b58334 commit d2bc94a

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

pandas/core/computation/align.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ def _align_core_single_unary_op(
3838
def _zip_axes_from_type(
3939
typ: Type[FrameOrSeries], new_axes: Sequence[int]
4040
) -> Dict[str, int]:
41-
axes = {name: new_axes[i] for i, name in enumerate(typ._AXIS_ORDERS)}
42-
return axes
41+
return {name: new_axes[i] for i, name in enumerate(typ._AXIS_ORDERS)}
4342

4443

4544
def _any_pandas_objects(terms) -> bool:
@@ -186,8 +185,11 @@ def reconstruct_object(typ, obj, axes, dtype):
186185
# The condition is to distinguish 0-dim array (returned in case of
187186
# scalar) and 1 element array
188187
# e.g. np.array(0) and np.array([0])
189-
if len(obj.shape) == 1 and len(obj) == 1:
190-
if not isinstance(ret_value, np.ndarray):
191-
ret_value = np.array([ret_value]).astype(res_t)
188+
if (
189+
len(obj.shape) == 1
190+
and len(obj) == 1
191+
and not isinstance(ret_value, np.ndarray)
192+
):
193+
ret_value = np.array([ret_value]).astype(res_t)
192194

193195
return ret_value

pandas/core/computation/eval.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ def _check_engine(engine: Optional[str]) -> str:
5252
# TODO: validate this in a more general way (thinking of future engines
5353
# that won't necessarily be import-able)
5454
# Could potentially be done on engine instantiation
55-
if engine == "numexpr":
56-
if not NUMEXPR_INSTALLED:
57-
raise ImportError(
58-
"'numexpr' is not installed or an unsupported version. Cannot use "
59-
"engine='numexpr' for query/eval if 'numexpr' is not installed"
60-
)
55+
if engine == "numexpr" and not NUMEXPR_INSTALLED:
56+
raise ImportError(
57+
"'numexpr' is not installed or an unsupported version. Cannot use "
58+
"engine='numexpr' for query/eval if 'numexpr' is not installed"
59+
)
6160

6261
return engine
6362

pandas/core/computation/expr.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -496,15 +496,14 @@ def _maybe_evaluate_binop(
496496
f"'{lhs.type}' and '{rhs.type}'"
497497
)
498498

499-
if self.engine != "pytables":
500-
if (
501-
res.op in CMP_OPS_SYMS
502-
and getattr(lhs, "is_datetime", False)
503-
or getattr(rhs, "is_datetime", False)
504-
):
505-
# all date ops must be done in python bc numexpr doesn't work
506-
# well with NaT
507-
return self._maybe_eval(res, self.binary_ops)
499+
if self.engine != "pytables" and (
500+
res.op in CMP_OPS_SYMS
501+
and getattr(lhs, "is_datetime", False)
502+
or getattr(rhs, "is_datetime", False)
503+
):
504+
# all date ops must be done in python bc numexpr doesn't work
505+
# well with NaT
506+
return self._maybe_eval(res, self.binary_ops)
508507

509508
if res.op in eval_in_python:
510509
# "in"/"not in" ops are always evaluated in python

pandas/core/computation/pytables.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,14 @@ def prune(self, klass):
378378
operand = self.operand
379379
operand = operand.prune(klass)
380380

381-
if operand is not None:
382-
if issubclass(klass, ConditionBinOp):
383-
if operand.condition is not None:
384-
return operand.invert()
385-
elif issubclass(klass, FilterBinOp):
386-
if operand.filter is not None:
387-
return operand.invert()
388-
381+
if operand is not None and (
382+
issubclass(klass, ConditionBinOp)
383+
and operand.condition is not None
384+
or not issubclass(klass, ConditionBinOp)
385+
and issubclass(klass, FilterBinOp)
386+
and operand.filter is not None
387+
):
388+
return operand.invert()
389389
return None
390390

391391

pandas/core/computation/scope.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ def __init__(
144144
def __repr__(self) -> str:
145145
scope_keys = _get_pretty_string(list(self.scope.keys()))
146146
res_keys = _get_pretty_string(list(self.resolvers.keys()))
147-
unicode_str = f"{type(self).__name__}(scope={scope_keys}, resolvers={res_keys})"
148-
return unicode_str
147+
return f"{type(self).__name__}(scope={scope_keys}, resolvers={res_keys})"
149148

150149
@property
151150
def has_resolvers(self) -> bool:

0 commit comments

Comments
 (0)