Skip to content

Commit 0ffdbe3

Browse files
dsaxtontopper-123
authored andcommitted
BUG: Keep categorical name in groupby (#28798)
* BUG: Keep categorical name in groupby
1 parent 7705cd2 commit 0ffdbe3

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ Groupby/resample/rolling
330330
- Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`)
331331
- Bug in :meth:`DataFrame.groupby` not offering selection by column name when ``axis=1`` (:issue:`27614`)
332332
- Bug in :meth:`DataFrameGroupby.agg` not able to use lambda function with named aggregation (:issue:`27519`)
333+
- Bug in :meth:`DataFrame.groupby` losing column name information when grouping by a categorical column (:issue:`28787`)
333334

334335
Reshaping
335336
^^^^^^^^^

pandas/core/groupby/grouper.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ def __init__(
330330
self._group_index = CategoricalIndex(
331331
Categorical.from_codes(
332332
codes=codes, categories=categories, ordered=self.grouper.ordered
333-
)
333+
),
334+
name=self.name,
334335
)
335336

336337
# we are done

pandas/tests/groupby/test_categorical.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def test_preserve_categories():
674674

675675
# ordered=True
676676
df = DataFrame({"A": Categorical(list("ba"), categories=categories, ordered=True)})
677-
index = CategoricalIndex(categories, categories, ordered=True)
677+
index = CategoricalIndex(categories, categories, ordered=True, name="A")
678678
tm.assert_index_equal(
679679
df.groupby("A", sort=True, observed=False).first().index, index
680680
)
@@ -684,8 +684,8 @@ def test_preserve_categories():
684684

685685
# ordered=False
686686
df = DataFrame({"A": Categorical(list("ba"), categories=categories, ordered=False)})
687-
sort_index = CategoricalIndex(categories, categories, ordered=False)
688-
nosort_index = CategoricalIndex(list("bac"), list("bac"), ordered=False)
687+
sort_index = CategoricalIndex(categories, categories, ordered=False, name="A")
688+
nosort_index = CategoricalIndex(list("bac"), list("bac"), ordered=False, name="A")
689689
tm.assert_index_equal(
690690
df.groupby("A", sort=True, observed=False).first().index, sort_index
691691
)
@@ -1193,3 +1193,17 @@ def test_groupby_categorical_axis_1(code):
11931193
result = df.groupby(cat, axis=1).mean()
11941194
expected = df.T.groupby(cat, axis=0).mean().T
11951195
assert_frame_equal(result, expected)
1196+
1197+
1198+
def test_groupby_cat_preserves_structure(observed):
1199+
# GH 28787
1200+
df = DataFrame([("Bob", 1), ("Greg", 2)], columns=["Name", "Item"])
1201+
expected = df.copy()
1202+
1203+
result = (
1204+
df.groupby("Name", observed=observed)
1205+
.agg(pd.DataFrame.sum, skipna=True)
1206+
.reset_index()
1207+
)
1208+
1209+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)