Skip to content

Commit d3b7e64

Browse files
REGR: Fixed handling of Categorical in cython ops (#31668)
Fixed by falling back to the wrapped Python version when the cython version raises NotImplementedError. Closes #31450
1 parent 1996b17 commit d3b7e64

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v1.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Fixed regressions
1919
- Fixed regression when indexing a ``Series`` or ``DataFrame`` indexed by ``DatetimeIndex`` with a slice containg a :class:`datetime.date` (:issue:`31501`)
2020
- Fixed regression in ``DataFrame.__setitem__`` raising an ``AttributeError`` with a :class:`MultiIndex` and a non-monotonic indexer (:issue:`31449`)
2121
- Fixed regression in :class:`Series` multiplication when multiplying a numeric :class:`Series` with >10000 elements with a timedelta-like scalar (:issue:`31457`)
22+
- Fixed regression in ``.groupby()`` aggregations with categorical dtype using Cythonized reduction functions (e.g. ``first``) (:issue:`31450`)
2223
- Fixed regression in :meth:`GroupBy.apply` if called with a function which returned a non-pandas non-scalar object (e.g. a list or numpy array) (:issue:`31441`)
2324
- Fixed regression in :meth:`DataFrame.groupby` whereby taking the minimum or maximum of a column with period dtype would raise a ``TypeError``. (:issue:`31471`)
2425
- Fixed regression in :meth:`to_datetime` when parsing non-nanosecond resolution datetimes (:issue:`31491`)

pandas/core/groupby/groupby.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,9 @@ def func(self, numeric_only=numeric_only, min_count=min_count):
13941394
except DataError:
13951395
pass
13961396
except NotImplementedError as err:
1397-
if "function is not implemented for this dtype" in str(err):
1397+
if "function is not implemented for this dtype" in str(
1398+
err
1399+
) or "category dtype not supported" in str(err):
13981400
# raised in _get_cython_function, in some cases can
13991401
# be trimmed by implementing cython funcs for more dtypes
14001402
pass

pandas/tests/groupby/aggregate/test_aggregate.py

+16
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,22 @@ def test_agg_index_has_complex_internals(index):
377377
tm.assert_frame_equal(result, expected)
378378

379379

380+
def test_agg_cython_category_not_implemented_fallback():
381+
# https://github.com/pandas-dev/pandas/issues/31450
382+
df = pd.DataFrame({"col_num": [1, 1, 2, 3]})
383+
df["col_cat"] = df["col_num"].astype("category")
384+
385+
result = df.groupby("col_num").col_cat.first()
386+
expected = pd.Series(
387+
[1, 2, 3], index=pd.Index([1, 2, 3], name="col_num"), name="col_cat"
388+
)
389+
tm.assert_series_equal(result, expected)
390+
391+
result = df.groupby("col_num").agg({"col_cat": "first"})
392+
expected = expected.to_frame()
393+
tm.assert_frame_equal(result, expected)
394+
395+
380396
class TestNamedAggregationSeries:
381397
def test_series_named_agg(self):
382398
df = pd.Series([1, 2, 3, 4])

0 commit comments

Comments
 (0)