Derived from the pandas-vet linter.
What it does#
Check for uses of
.nunique() to check if a Pandas Series is constant
(i.e., contains only one unique value).
Why is this bad?#
.nunique() is computationally inefficient for checking if a Series is
Consider, for example, a Series of length
n that consists of increasing
integer values (e.g., 1, 2, 3, 4). The
.nunique() method will iterate
over the entire Series to count the number of unique values. But in this
case, we can detect that the Series is non-constant after visiting the
first two values, which are non-equal.
.nunique() requires iterating over the entire Series, while a
more efficient approach allows short-circuiting the operation as soon as a
non-equal value is found.
Instead of calling
.nunique(), convert the Series to a NumPy array, and
check if all values in the array are equal to the first observed value.