Skip to content

Commit 3c76318

Browse files
jbrockmendeljreback
authored andcommitted
CLN: require extracting .values before expressions calls (#31373)
1 parent 54a6490 commit 3c76318

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

pandas/core/computation/expressions.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
from pandas._config import get_option
1414

15-
from pandas._libs.lib import values_from_object
16-
1715
from pandas.core.dtypes.generic import ABCDataFrame
1816

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

124122

125123
def _where_standard(cond, a, b):
126-
return np.where(
127-
values_from_object(cond), values_from_object(a), values_from_object(b)
128-
)
124+
# Caller is responsible for calling values_from_object if necessary
125+
return np.where(cond, a, b)
129126

130127

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

134132
if _can_use_numexpr(None, "where", a, b, "where"):
135-
cond_value = getattr(cond, "values", cond)
136-
a_value = getattr(a, "values", a)
137-
b_value = getattr(b, "values", b)
138133

139134
result = ne.evaluate(
140135
"where(cond_value, a_value, b_value)",
141-
local_dict={
142-
"cond_value": cond_value,
143-
"a_value": a_value,
144-
"b_value": b_value,
145-
},
136+
local_dict={"cond_value": cond, "a_value": a, "b_value": b},
146137
casting="safe",
147138
)
148139

pandas/core/internals/blocks.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,7 @@ def where(
13901390
if not hasattr(cond, "shape"):
13911391
raise ValueError("where must have a condition that is ndarray like")
13921392

1393-
# our where function
1394-
def func(cond, values, other):
1393+
def where_func(cond, values, other):
13951394

13961395
if not (
13971396
(self.is_integer or self.is_bool)
@@ -1402,8 +1401,11 @@ def func(cond, values, other):
14021401
if not self._can_hold_element(other):
14031402
raise TypeError
14041403
if lib.is_scalar(other) and isinstance(values, np.ndarray):
1404+
# convert datetime to datetime64, timedelta to timedelta64
14051405
other = convert_scalar(values, other)
14061406

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

@@ -1413,7 +1415,7 @@ def func(cond, values, other):
14131415
# see if we can operate on the entire block, or need item-by-item
14141416
# or if we are a single block (ndim == 1)
14151417
try:
1416-
result = func(cond, values, other)
1418+
result = where_func(cond, values, other)
14171419
except TypeError:
14181420

14191421
# we cannot coerce, return a compat dtype

0 commit comments

Comments
 (0)