Skip to content

CLN: require extracting .values before expressions calls #31373

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 3 commits into from
Jan 28, 2020
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
17 changes: 4 additions & 13 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

from pandas._config import get_option

from pandas._libs.lib import values_from_object

from pandas.core.dtypes.generic import ABCDataFrame

from pandas.core.computation.check import _NUMEXPR_INSTALLED
Expand Down Expand Up @@ -123,26 +121,19 @@ def _evaluate_numexpr(op, op_str, a, b):


def _where_standard(cond, a, b):
return np.where(
values_from_object(cond), values_from_object(a), values_from_object(b)
)
# Caller is responsible for calling values_from_object if necessary
return np.where(cond, a, b)


def _where_numexpr(cond, a, b):
# Caller is responsible for calling values_from_object if necessary
result = None

if _can_use_numexpr(None, "where", a, b, "where"):
cond_value = getattr(cond, "values", cond)
a_value = getattr(a, "values", a)
b_value = getattr(b, "values", b)

result = ne.evaluate(
"where(cond_value, a_value, b_value)",
local_dict={
"cond_value": cond_value,
"a_value": a_value,
"b_value": b_value,
},
local_dict={"cond_value": cond, "a_value": a, "b_value": b},
casting="safe",
)

Expand Down
8 changes: 5 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,8 +1379,7 @@ def where(
if not hasattr(cond, "shape"):
raise ValueError("where must have a condition that is ndarray like")

# our where function
def func(cond, values, other):
def where_func(cond, values, other):

if not (
(self.is_integer or self.is_bool)
Expand All @@ -1391,8 +1390,11 @@ def func(cond, values, other):
if not self._can_hold_element(other):
raise TypeError
if lib.is_scalar(other) and isinstance(values, np.ndarray):
# convert datetime to datetime64, timedelta to timedelta64
other = convert_scalar(values, other)

# By the time we get here, we should have all Series/Index
# args extracted to ndarray
fastres = expressions.where(cond, values, other)
return fastres

Expand All @@ -1402,7 +1404,7 @@ def func(cond, values, other):
# see if we can operate on the entire block, or need item-by-item
# or if we are a single block (ndim == 1)
try:
result = func(cond, values, other)
result = where_func(cond, values, other)
except TypeError:

# we cannot coerce, return a compat dtype
Expand Down