-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: series.is_constant #54033
Comments
Is checking for a constant ever a bottleneck in a workflow? This seems like something a user could easily do themselves. Given the heavy usage of |
Adding an example to the pandas cookbook seems a good way of informing users to consider implementing a check that short-circuits. I will open a PR. For completeness: Implementing |
I'll leave this open to see if others want this then. |
* Document constant check in series Closes #54033 * Update cookbook.rst * Update cookbook.rst * empty series is constant * Update cookbook.rst * Update cookbook.rst
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
Pandas Series have a property
is_unique
that returns true iff all values are distinct.In practice, one often needs to know if a Series is constant. Getting a functional result when series do not contain nans is straight forward:
(series.values[0] == series.values).all()
. However, in the wild we observe that users often simply writeseries.nunique() == 1
. This approach is more costly for larger series, as it first computes the number of unique values before comparing.My assumption is that introducing a dedicated
is_constant
property will help users choose the more performant option. Another assumption is that its acceptable that all-nan series are somewhat slower.Feature Description
(based on
is_unique
andnunique
, in the absence of nans)To extend to NA values:
If
dropna=True
: addv = remove_na_arraylike(v)
If
dropna=False
: addor not pd.notna(v).any()
(note: differs fromnp.unique
forpd.NA
andnp.nan
mixed series by lack of disambiguation between the two)Alternative Solutions
Recommend the approach above for
series.nunique() == 1
patterns in the documentation (here)Additional Context
No response
The text was updated successfully, but these errors were encountered: