Skip to content

ENH: Add a Series method which checks whether a Series is constant #58806

Open
@nathanjmcdougall

Description

@nathanjmcdougall

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

In the cookbook, a recipe is given for checking that a Series only contains constant values in a performant way:

https://pandas.pydata.org/docs/user_guide/cookbook.html#constant-series

is_constant = v.shape[0] == 0 or (s[0] == s).all()

To me, this has poor readability and is difficult to learn as an idiom because it requires the programmer to remember to check the edge case of .shape[0] == [0], and to remember to check the cases of missing values / NaN values, which need to be handled differently (as explained in the cookbook).

Feature Description

It would be nice to have a convenience function which provided a performant is_constant check on a Series.

It could have optional arguments to configure how missing values are handled.

Alternative Solutions

The alternative is just to require the user to detect the poorly performant code, possibly automatically with a linter (see below), and come up with a performant solution for their case, possibly using the cookbook. Otherwise, the simple .nunique(dropna=...) <= 1 solution is convenient enough for when performance is not a concern.

Additional Context

I came across this when using a pandas-vet rule via ruff: PD101

I like the linter to detect performance issues like this one; but I prefer that they don't harm readability if possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds 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