pandas/value-counts-share
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
Dividing value_counts() by the length computes shares by hand. Call value_counts(normalize=True) instead.
shares = df["region"].value_counts() / len(df["region"])shares = df["region"].value_counts(normalize=True)What the rule reports
Section titled “What the rule reports”The rule reports df["k"].value_counts() / len(df["k"]). The fix is unsafe, because it names the result proportion instead of count, and it leaves missing values out of the total.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes df["k"].value_counts(normalize=True).
Diagnostic message
Section titled “Diagnostic message”value_counts(normalize=True) returns the shares directly. Write $S.value_counts(normalize=True).See all rules in the pandas pack.