Skip to content

ENH: Add a name parameter to value_counts() #55121

Open
@n-splv

Description

@n-splv

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

In version 2.1 Series.value_counts() names the resulting series count or proportion depending on the value of normalize parameter.

>>> s = pd.Series([1, 2, 2], name='my_column')
>>> s.value_counts()
my_column
2    2
1    1
Name: count, dtype: int64

Previously, however, the name of the resulting series was the same as the original one. In Pandas 1.5.3:

>>> s = pd.Series([1, 2, 2], name='my_column')
>>> s.value_counts()
2    2
1    1
Name: my_column, dtype: int64

This change made some of my complicated data manipulation pipelines broken, without any warning.

Feature Description

Why don't we do something like this:

s = pd.Series([1, 2, 2], name='my_column')
s.value_counts(name='desired_name')
my_column
2    2
1    1
Name: desired_name, dtype: int64

This way the users cloud be explicitly informed about the default behavior (or its future changes) in the docstring.
Besides, I often find myself chaining the .value_counts() with .rename(), so it would probably add some convenience as well.

Alternative Solutions

Happy to hear from you

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffEnhancementNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions