Skip to content

Commit 32cf991

Browse files
committed
BUG: SeriesGroupBy.value_counts index name missing
Issue pandas-dev#44324
1 parent 5f8df6b commit 32cf991

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Datetimelike
220220
- Bug in :meth:`DataFrame.quantile` with datetime-like dtypes and no rows incorrectly returning ``float64`` dtype instead of retaining datetime-like dtype (:issue:`41544`)
221221
- Bug in :func:`to_datetime` with sequences of ``np.str_`` objects incorrectly raising (:issue:`32264`)
222222
- Bug in :class:`Timestamp` construction when passing datetime components as positional arguments and ``tzinfo`` as a keyword argument incorrectly raising (:issue:`31929`)
223+
- Bug in :meth:`SeriesGroupBy.value_counts` index when passing categorical column (:issue:`44324`)
223224
-
224225

225226
Timedelta

pandas/core/groupby/generic.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,18 @@ def value_counts(
602602
ids, _, _ = self.grouper.group_info
603603
val = self.obj._values
604604

605+
names = self.grouper.names + [self.obj.name]
606+
605607
def apply_series_value_counts():
606-
return self.apply(
608+
s = self.apply(
607609
Series.value_counts,
608610
normalize=normalize,
609611
sort=sort,
610612
ascending=ascending,
611613
bins=bins,
612614
)
615+
s.index.names = names
616+
return s
613617

614618
if bins is not None:
615619
if not np.iterable(bins):
@@ -683,7 +687,6 @@ def apply_series_value_counts():
683687
levels = [ping.group_index for ping in self.grouper.groupings] + [
684688
lev # type: ignore[list-item]
685689
]
686-
names = self.grouper.names + [self.obj.name]
687690

688691
if dropna:
689692
mask = codes[-1] != -1

pandas/tests/groupby/test_value_counts.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@
2222
import pandas._testing as tm
2323

2424

25+
def tests_value_counts_index_names_category_column():
26+
# GH44324 Missing name of index category column
27+
df = DataFrame(
28+
{
29+
"gender": ["female"],
30+
"country": ["US"],
31+
}
32+
)
33+
df["gender"] = df["gender"].astype("category")
34+
result = df.groupby("country")["gender"].value_counts()
35+
36+
# Construct expected, very specific multiindex
37+
df_mi_expected = DataFrame([["US", "female"]], columns=["country", "gender"])
38+
df_mi_expected["gender"] = df_mi_expected["gender"].astype("category")
39+
mi_expected = MultiIndex.from_frame(df_mi_expected)
40+
expected = Series([1], index=mi_expected, name="gender")
41+
42+
tm.assert_series_equal(result, expected)
43+
44+
2545
# our starting frame
2646
def seed_df(seed_nans, n, m):
2747
np.random.seed(1234)

0 commit comments

Comments
 (0)