Skip to content

Commit 0a44346

Browse files
Backport PR #55586 on branch 2.1.x (REGR: Groupby methods not supporting numba raising TypeError when the…) (#55686)
Backport PR #55586: REGR: Groupby methods not supporting numba raising TypeError when the… Co-authored-by: Thomas Li <[email protected]>
1 parent 87cb141 commit 0a44346

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
- Fixed regression in :meth:`DataFrame.join` where result has missing values and dtype is arrow backed string (:issue:`55348`)
1717
- Fixed regression in :meth:`DataFrame.resample` which was extrapolating back to ``origin`` when ``origin`` was outside its bounds (:issue:`55064`)
1818
- Fixed regression in :meth:`DataFrame.sort_index` which was not sorting correctly when the index was a sliced :class:`MultiIndex` (:issue:`55379`)
19+
- Fixed regression in :meth:`DataFrameGroupBy.agg` and :meth:`SeriesGroupBy.agg` where if the option ``compute.use_numba`` was set to True, groupby methods not supported by the numba engine would raise a ``TypeError`` (:issue:`55520`)
1920
- Fixed performance regression with wide DataFrames, typically involving methods where all columns were accessed individually (:issue:`55256`, :issue:`55245`)
2021
- Fixed regression in :func:`merge_asof` raising ``TypeError`` for ``by`` with datetime and timedelta dtypes (:issue:`55453`)
2122

pandas/core/groupby/generic.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,13 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
236236
kwargs = {}
237237

238238
if isinstance(func, str):
239-
if maybe_use_numba(engine):
239+
if maybe_use_numba(engine) and engine is not None:
240240
# Not all agg functions support numba, only propagate numba kwargs
241-
# if user asks for numba
241+
# if user asks for numba, and engine is not None
242+
# (if engine is None, the called function will handle the case where
243+
# numba is requested via the global option)
242244
kwargs["engine"] = engine
245+
if engine_kwargs is not None:
243246
kwargs["engine_kwargs"] = engine_kwargs
244247
return getattr(self, func)(*args, **kwargs)
245248

pandas/tests/groupby/test_numba.py

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pandas import (
44
DataFrame,
55
Series,
6+
option_context,
67
)
78
import pandas._testing as tm
89

@@ -66,3 +67,14 @@ def test_axis_1_unsupported(self, numba_supported_reductions):
6667
gb = df.groupby("a", axis=1)
6768
with pytest.raises(NotImplementedError, match="axis=1"):
6869
getattr(gb, func)(engine="numba", **kwargs)
70+
71+
def test_no_engine_doesnt_raise(self):
72+
# GH55520
73+
df = DataFrame({"a": [3, 2, 3, 2], "b": range(4), "c": range(1, 5)})
74+
gb = df.groupby("a")
75+
# Make sure behavior of functions w/out engine argument don't raise
76+
# when the global use_numba option is set
77+
with option_context("compute.use_numba", True):
78+
res = gb.agg({"b": "first"})
79+
expected = gb.agg({"b": "first"})
80+
tm.assert_frame_equal(res, expected)

0 commit comments

Comments
 (0)