Skip to content

REF: eliminate eval_kwargs #29611

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 12 commits into from
Nov 14, 2019
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
13 changes: 6 additions & 7 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand All @@ -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)


Expand Down
41 changes: 2 additions & 39 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
19 changes: 7 additions & 12 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ExtensionArrays.
"""
import operator
from typing import Any, Mapping, Union
from typing import Any, Union

import numpy as np

Expand Down Expand Up @@ -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.

Expand All @@ -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
-------
Expand All @@ -149,19 +148,15 @@ 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)

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


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 `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...
Expand Down Expand Up @@ -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

Expand Down