Skip to content

Commit 400c550

Browse files
author
Connor Charles
committed
BUG: _can_use_numexpr did not handle Series case correctly
Fixes: pandas-dev#27636
1 parent a45760f commit 400c550

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

pandas/core/computation/expressions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check):
8080
# check for dtype compatibility
8181
dtypes = set()
8282
for o in [a, b]:
83-
if hasattr(o, "dtypes"):
83+
if hasattr(o, "dtypes") and o.ndim > 1:
8484
s = o.dtypes.value_counts()
8585
if len(s) > 1:
8686
return False

pandas/tests/test_expressions.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from itertools import product
12
import operator
23
import re
34

@@ -66,7 +67,7 @@ def run_arithmetic(self, df, other, assert_func, check_dtype=False, test_flex=Tr
6667
operator_name = "truediv"
6768

6869
if test_flex:
69-
op = lambda x, y: getattr(df, arith)(y)
70+
op = lambda x, y: getattr(x, arith)(y)
7071
op.__name__ = arith
7172
else:
7273
op = getattr(operator, operator_name)
@@ -431,3 +432,30 @@ def test_bool_ops_column_name_dtype(self, test_input, expected):
431432
# GH 22383 - .ne fails if columns containing column name 'dtype'
432433
result = test_input.loc[:, ["a", "dtype"]].ne(test_input.loc[:, ["a", "dtype"]])
433434
assert_frame_equal(result, expected)
435+
436+
@pytest.mark.parametrize(
437+
"axis,arith", product((0, 1), ("add", "sub", "mul", "mod", "truediv"))
438+
)
439+
def test_frame_series_axis(self, axis, arith):
440+
# Can't check floordiv here because it currently doesn't work #GH27636
441+
df = self.frame
442+
if axis == 1:
443+
other = self.frame.iloc[0, :]
444+
else:
445+
other = self.frame.iloc[:, 0]
446+
447+
expr._MIN_ELEMENTS = 0
448+
449+
op = lambda x, y: getattr(x, arith)(y, axis=axis)
450+
op.__name__ = arith
451+
452+
expr.set_use_numexpr(False)
453+
expected = op(df, other)
454+
expr.set_use_numexpr(True)
455+
456+
result = op(df, other)
457+
try:
458+
assert_frame_equal(expected, result)
459+
except Exception:
460+
pprint_thing("Failed test with operator {op.__name__!r}".format(op=op))
461+
raise

0 commit comments

Comments
 (0)