Skip to content

Backport PR #27773 on branch 0.25.x (BUG: _can_use_numexpr fails when passed large Series) #28017

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Numeric
^^^^^^^
- Bug in :meth:`Series.interpolate` when using a timezone aware :class:`DatetimeIndex` (:issue:`27548`)
- Bug when printing negative floating point complex numbers would raise an ``IndexError`` (:issue:`27484`)
-
- Bug where :class:`DataFrame` arithmetic operators such as :meth:`DataFrame.mul` with a :class:`Series` with axis=1 would raise an ``AttributeError`` on :class:`DataFrame` larger than the minimum threshold to invoke numexpr (:issue:`27636`)
-

Conversion
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check):

# required min elements (otherwise we are adding overhead)
if np.prod(a.shape) > _MIN_ELEMENTS:

# check for dtype compatibility
dtypes = set()
for o in [a, b]:
if hasattr(o, "dtypes"):
# Series implements dtypes, check for dimension count as well
if hasattr(o, "dtypes") and o.ndim > 1:
s = o.dtypes.value_counts()
if len(s) > 1:
return False
dtypes |= set(s.index.astype(str))
elif isinstance(o, np.ndarray):
# ndarray and Series Case
elif hasattr(o, "dtype"):
dtypes |= {o.dtype.name}

# allowed are a superset
Expand Down
29 changes: 27 additions & 2 deletions pandas/tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def run_arithmetic(self, df, other, assert_func, check_dtype=False, test_flex=Tr
operator_name = "truediv"

if test_flex:
op = lambda x, y: getattr(df, arith)(y)
op = lambda x, y: getattr(x, arith)(y)
op.__name__ = arith
else:
op = getattr(operator, operator_name)
Expand Down Expand Up @@ -318,7 +318,6 @@ def testit():
for f in [self.frame, self.frame2, self.mixed, self.mixed2]:

for cond in [True, False]:

c = np.empty(f.shape, dtype=np.bool_)
c.fill(cond)
result = expr.where(c, f.values, f.values + 1)
Expand Down Expand Up @@ -431,3 +430,29 @@ def test_bool_ops_column_name_dtype(self, test_input, expected):
# GH 22383 - .ne fails if columns containing column name 'dtype'
result = test_input.loc[:, ["a", "dtype"]].ne(test_input.loc[:, ["a", "dtype"]])
assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"arith", ("add", "sub", "mul", "mod", "truediv", "floordiv")
)
@pytest.mark.parametrize("axis", (0, 1))
def test_frame_series_axis(self, axis, arith):
# GH#26736 Dataframe.floordiv(Series, axis=1) fails
if axis == 1 and arith == "floordiv":
pytest.xfail("'floordiv' does not succeed with axis=1 #27636")

df = self.frame
if axis == 1:
other = self.frame.iloc[0, :]
else:
other = self.frame.iloc[:, 0]

expr._MIN_ELEMENTS = 0

op_func = getattr(df, arith)

expr.set_use_numexpr(False)
expected = op_func(other, axis=axis)
expr.set_use_numexpr(True)

result = op_func(other, axis=axis)
assert_frame_equal(expected, result)