From 4b5db3adde2b6b1e3a2132a9593e969b7d3a369a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 14 Oct 2019 12:24:59 -0700 Subject: [PATCH 1/4] REF: remove need for eval_kwargs --- pandas/core/computation/expressions.py | 13 ++++---- pandas/core/ops/__init__.py | 41 ++------------------------ pandas/core/ops/array_ops.py | 19 +++++------- 3 files changed, 15 insertions(+), 58 deletions(-) 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 398fa9b0c1fc0..bcd4293e7bb0d 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -181,41 +181,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: @@ -487,7 +452,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) def wrapper(left, right): if isinstance(right, ABCDataFrame): @@ -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) @@ -688,10 +652,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 8c9a4b94446c0..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, Dict, 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: Dict[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 From 688bb0427641ea786ea2244d11f7b4b8966a9adf Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 14 Nov 2019 07:40:14 -0800 Subject: [PATCH 2/4] fixup: missed in rebase --- pandas/core/ops/array_ops.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 1d22b3b55b48c..6787ce61c3ae8 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -160,7 +160,6 @@ def arithmetic_op( right: Any, op, str_rep: str, - eval_kwargs: Mapping[str, bool], ): """ Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ... From a512d4f1f9cfb6d49a080d16b7c305296865dc6e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 14 Nov 2019 09:31:48 -0800 Subject: [PATCH 3/4] CLN: fixup unused importt --- pandas/core/ops/array_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 6787ce61c3ae8..e33883fabdb33 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 From f68d50fd1fad5ebc7fd2cd4291105328e4f15b60 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 14 Nov 2019 10:38:36 -0800 Subject: [PATCH 4/4] blackify --- pandas/core/ops/array_ops.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index e33883fabdb33..414e241af7bbd 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -156,10 +156,7 @@ def na_arithmetic_op(left, right, op, str_rep: str): def arithmetic_op( - left: Union[np.ndarray, ABCExtensionArray], - right: Any, - op, - str_rep: str, + left: Union[np.ndarray, ABCExtensionArray], right: Any, op, str_rep: str ): """ Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...