diff --git a/pandas/core/computation/expressions.py b/pandas/core/computation/expressions.py index 46bc762e1a0b3..77999d2c166fd 100644 --- a/pandas/core/computation/expressions.py +++ b/pandas/core/computation/expressions.py @@ -62,9 +62,8 @@ def set_numexpr_threads(n=None): ne.set_num_threads(n) -def _evaluate_standard(op, op_str, a, b, reversed=False): +def _evaluate_standard(op, op_str, a, b): """ standard evaluation """ - # `reversed` kwarg is included for compatibility with _evaluate_numexpr if _TEST_MODE: _store_test_result(False) with np.errstate(all="ignore"): @@ -97,11 +96,12 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check): return False -def _evaluate_numexpr(op, op_str, a, b, reversed=False): +def _evaluate_numexpr(op, op_str, a, b): result = None if _can_use_numexpr(op, op_str, a, b, "evaluate"): - if reversed: + is_reversed = op.__name__.strip("_").startswith("r") + if is_reversed: # we were originally called by a reversed op method a, b = b, a @@ -190,7 +190,7 @@ def _bool_arith_check( return True -def evaluate(op, op_str, a, b, use_numexpr=True, reversed=False): +def evaluate(op, op_str, a, b, use_numexpr=True): """ Evaluate and return the expression of the op on a and b. @@ -203,12 +203,11 @@ def evaluate(op, op_str, a, b, use_numexpr=True, reversed=False): b : right operand use_numexpr : bool, default True Whether to try to use numexpr. - reversed : bool, default False """ use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b) if use_numexpr: - return _evaluate(op, op_str, a, b, reversed=reversed) + return _evaluate(op, op_str, a, b) return _evaluate_standard(op, op_str, a, b) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index f7a1258894b89..d14fb040c4e30 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -182,41 +182,6 @@ def maybe_upcast_for_op(obj, shape: Tuple[int, ...]): # ----------------------------------------------------------------------------- -def _gen_eval_kwargs(name): - """ - Find the keyword arguments to pass to numexpr for the given operation. - - Parameters - ---------- - name : str - - Returns - ------- - eval_kwargs : dict - - Examples - -------- - >>> _gen_eval_kwargs("__add__") - {} - - >>> _gen_eval_kwargs("rtruediv") - {'reversed': True, 'truediv': True} - """ - kwargs = {} - - # Series appear to only pass __add__, __radd__, ... - # but DataFrame gets both these dunder names _and_ non-dunder names - # add, radd, ... - name = name.replace("__", "") - - if name.startswith("r"): - if name not in ["radd", "rand", "ror", "rxor"]: - # Exclude commutative operations - kwargs["reversed"] = True - - return kwargs - - def _get_frame_op_default_axis(name): """ Only DataFrame cares about default_axis, specifically: @@ -488,7 +453,6 @@ def _arith_method_SERIES(cls, op, special): """ str_rep = _get_opstr(op) op_name = _get_op_name(op, special) - eval_kwargs = _gen_eval_kwargs(op_name) @unpack_zerodim_and_defer(op_name) def wrapper(left, right): @@ -497,7 +461,7 @@ def wrapper(left, right): res_name = get_op_result_name(left, right) lvalues = extract_array(left, extract_numpy=True) - result = arithmetic_op(lvalues, right, op, str_rep, eval_kwargs) + result = arithmetic_op(lvalues, right, op, str_rep) return _construct_result(left, result, index=left.index, name=res_name) @@ -682,10 +646,9 @@ def to_series(right): def _arith_method_FRAME(cls, op, special): str_rep = _get_opstr(op) op_name = _get_op_name(op, special) - eval_kwargs = _gen_eval_kwargs(op_name) default_axis = _get_frame_op_default_axis(op_name) - na_op = define_na_arithmetic_op(op, str_rep, eval_kwargs) + na_op = define_na_arithmetic_op(op, str_rep) is_logical = str_rep in ["&", "|", "^"] if op_name in _op_descriptions: diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 46c3b8b575af9..414e241af7bbd 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -3,7 +3,7 @@ ExtensionArrays. """ import operator -from typing import Any, Mapping, Union +from typing import Any, Union import numpy as np @@ -118,14 +118,14 @@ def masked_arith_op(x, y, op): return result -def define_na_arithmetic_op(op, str_rep: str, eval_kwargs): +def define_na_arithmetic_op(op, str_rep: str): def na_op(x, y): - return na_arithmetic_op(x, y, op, str_rep, eval_kwargs) + return na_arithmetic_op(x, y, op, str_rep) return na_op -def na_arithmetic_op(left, right, op, str_rep: str, eval_kwargs): +def na_arithmetic_op(left, right, op, str_rep: str): """ Return the result of evaluating op on the passed in values. @@ -136,7 +136,6 @@ def na_arithmetic_op(left, right, op, str_rep: str, eval_kwargs): left : np.ndarray right : np.ndarray or scalar str_rep : str or None - eval_kwargs : kwargs to pass to expressions Returns ------- @@ -149,7 +148,7 @@ def na_arithmetic_op(left, right, op, str_rep: str, eval_kwargs): import pandas.core.computation.expressions as expressions try: - result = expressions.evaluate(op, str_rep, left, right, **eval_kwargs) + result = expressions.evaluate(op, str_rep, left, right) except TypeError: result = masked_arith_op(left, right, op) @@ -157,11 +156,7 @@ def na_arithmetic_op(left, right, op, str_rep: str, eval_kwargs): def arithmetic_op( - left: Union[np.ndarray, ABCExtensionArray], - right: Any, - op, - str_rep: str, - eval_kwargs: Mapping[str, bool], + left: Union[np.ndarray, ABCExtensionArray], right: Any, op, str_rep: str ): """ Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ... @@ -212,7 +207,7 @@ def arithmetic_op( else: with np.errstate(all="ignore"): - res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep, eval_kwargs) + res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep) return res_values