Skip to content

Commit 2c31a99

Browse files
jbrockmendelproost
authored andcommitted
REF: eliminate eval_kwargs (pandas-dev#29611)
1 parent 90e62bc commit 2c31a99

File tree

3 files changed

+15
-58
lines changed

3 files changed

+15
-58
lines changed

pandas/core/computation/expressions.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ def set_numexpr_threads(n=None):
6262
ne.set_num_threads(n)
6363

6464

65-
def _evaluate_standard(op, op_str, a, b, reversed=False):
65+
def _evaluate_standard(op, op_str, a, b):
6666
""" standard evaluation """
67-
# `reversed` kwarg is included for compatibility with _evaluate_numexpr
6867
if _TEST_MODE:
6968
_store_test_result(False)
7069
with np.errstate(all="ignore"):
@@ -97,11 +96,12 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check):
9796
return False
9897

9998

100-
def _evaluate_numexpr(op, op_str, a, b, reversed=False):
99+
def _evaluate_numexpr(op, op_str, a, b):
101100
result = None
102101

103102
if _can_use_numexpr(op, op_str, a, b, "evaluate"):
104-
if reversed:
103+
is_reversed = op.__name__.strip("_").startswith("r")
104+
if is_reversed:
105105
# we were originally called by a reversed op method
106106
a, b = b, a
107107

@@ -190,7 +190,7 @@ def _bool_arith_check(
190190
return True
191191

192192

193-
def evaluate(op, op_str, a, b, use_numexpr=True, reversed=False):
193+
def evaluate(op, op_str, a, b, use_numexpr=True):
194194
"""
195195
Evaluate and return the expression of the op on a and b.
196196
@@ -203,12 +203,11 @@ def evaluate(op, op_str, a, b, use_numexpr=True, reversed=False):
203203
b : right operand
204204
use_numexpr : bool, default True
205205
Whether to try to use numexpr.
206-
reversed : bool, default False
207206
"""
208207

209208
use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b)
210209
if use_numexpr:
211-
return _evaluate(op, op_str, a, b, reversed=reversed)
210+
return _evaluate(op, op_str, a, b)
212211
return _evaluate_standard(op, op_str, a, b)
213212

214213

pandas/core/ops/__init__.py

+2-39
Original file line numberDiff line numberDiff line change
@@ -182,41 +182,6 @@ def maybe_upcast_for_op(obj, shape: Tuple[int, ...]):
182182
# -----------------------------------------------------------------------------
183183

184184

185-
def _gen_eval_kwargs(name):
186-
"""
187-
Find the keyword arguments to pass to numexpr for the given operation.
188-
189-
Parameters
190-
----------
191-
name : str
192-
193-
Returns
194-
-------
195-
eval_kwargs : dict
196-
197-
Examples
198-
--------
199-
>>> _gen_eval_kwargs("__add__")
200-
{}
201-
202-
>>> _gen_eval_kwargs("rtruediv")
203-
{'reversed': True, 'truediv': True}
204-
"""
205-
kwargs = {}
206-
207-
# Series appear to only pass __add__, __radd__, ...
208-
# but DataFrame gets both these dunder names _and_ non-dunder names
209-
# add, radd, ...
210-
name = name.replace("__", "")
211-
212-
if name.startswith("r"):
213-
if name not in ["radd", "rand", "ror", "rxor"]:
214-
# Exclude commutative operations
215-
kwargs["reversed"] = True
216-
217-
return kwargs
218-
219-
220185
def _get_frame_op_default_axis(name):
221186
"""
222187
Only DataFrame cares about default_axis, specifically:
@@ -488,7 +453,6 @@ def _arith_method_SERIES(cls, op, special):
488453
"""
489454
str_rep = _get_opstr(op)
490455
op_name = _get_op_name(op, special)
491-
eval_kwargs = _gen_eval_kwargs(op_name)
492456

493457
@unpack_zerodim_and_defer(op_name)
494458
def wrapper(left, right):
@@ -497,7 +461,7 @@ def wrapper(left, right):
497461
res_name = get_op_result_name(left, right)
498462

499463
lvalues = extract_array(left, extract_numpy=True)
500-
result = arithmetic_op(lvalues, right, op, str_rep, eval_kwargs)
464+
result = arithmetic_op(lvalues, right, op, str_rep)
501465

502466
return _construct_result(left, result, index=left.index, name=res_name)
503467

@@ -682,10 +646,9 @@ def to_series(right):
682646
def _arith_method_FRAME(cls, op, special):
683647
str_rep = _get_opstr(op)
684648
op_name = _get_op_name(op, special)
685-
eval_kwargs = _gen_eval_kwargs(op_name)
686649
default_axis = _get_frame_op_default_axis(op_name)
687650

688-
na_op = define_na_arithmetic_op(op, str_rep, eval_kwargs)
651+
na_op = define_na_arithmetic_op(op, str_rep)
689652
is_logical = str_rep in ["&", "|", "^"]
690653

691654
if op_name in _op_descriptions:

pandas/core/ops/array_ops.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
ExtensionArrays.
44
"""
55
import operator
6-
from typing import Any, Mapping, Union
6+
from typing import Any, Union
77

88
import numpy as np
99

@@ -118,14 +118,14 @@ def masked_arith_op(x, y, op):
118118
return result
119119

120120

121-
def define_na_arithmetic_op(op, str_rep: str, eval_kwargs):
121+
def define_na_arithmetic_op(op, str_rep: str):
122122
def na_op(x, y):
123-
return na_arithmetic_op(x, y, op, str_rep, eval_kwargs)
123+
return na_arithmetic_op(x, y, op, str_rep)
124124

125125
return na_op
126126

127127

128-
def na_arithmetic_op(left, right, op, str_rep: str, eval_kwargs):
128+
def na_arithmetic_op(left, right, op, str_rep: str):
129129
"""
130130
Return the result of evaluating op on the passed in values.
131131
@@ -136,7 +136,6 @@ def na_arithmetic_op(left, right, op, str_rep: str, eval_kwargs):
136136
left : np.ndarray
137137
right : np.ndarray or scalar
138138
str_rep : str or None
139-
eval_kwargs : kwargs to pass to expressions
140139
141140
Returns
142141
-------
@@ -149,19 +148,15 @@ def na_arithmetic_op(left, right, op, str_rep: str, eval_kwargs):
149148
import pandas.core.computation.expressions as expressions
150149

151150
try:
152-
result = expressions.evaluate(op, str_rep, left, right, **eval_kwargs)
151+
result = expressions.evaluate(op, str_rep, left, right)
153152
except TypeError:
154153
result = masked_arith_op(left, right, op)
155154

156155
return missing.dispatch_fill_zeros(op, left, right, result)
157156

158157

159158
def arithmetic_op(
160-
left: Union[np.ndarray, ABCExtensionArray],
161-
right: Any,
162-
op,
163-
str_rep: str,
164-
eval_kwargs: Mapping[str, bool],
159+
left: Union[np.ndarray, ABCExtensionArray], right: Any, op, str_rep: str
165160
):
166161
"""
167162
Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...
@@ -212,7 +207,7 @@ def arithmetic_op(
212207

213208
else:
214209
with np.errstate(all="ignore"):
215-
res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep, eval_kwargs)
210+
res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep)
216211

217212
return res_values
218213

0 commit comments

Comments
 (0)