Skip to content

Missing Series name when using count() on groupby object #6265

Closed
@andygoldschmidt

Description

@andygoldschmidt

Consider this very basic group by operation:

df = pd.DataFrame({'col1': ['a', 'b', 'a', 'c'], 'col2': [1, 3, 2, 5]})
df_grp = df.groupby('col1')['col2']

When aggregating using mean, min, ... (everything but count) the resulting Series has col2 as its name:

df_grp.mean()

col1
a       1.5
b       3.0
c       5.0
Name: col2, dtype: float64

However, if I use count for aggregation, the name is not set:

df_grp.count()

col1
a       2
b       1
c       1
dtype: int64

Not a big problem for a simple case like that, but I stumbled over that while working with a MultiIndex that needed to be reindexed and led to a KeyError due to that missing column name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugGroupbyIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions