pandas/level-values-unique
| Rule | level-values-unique |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
get_level_values(level).unique() builds one value for every row before it finds the unique ones. Call index.unique(level), which was 22 times faster.
regions = df.index.get_level_values("region").unique()regions = df.index.unique("region")What the rule reports
Section titled “What the rule reports”The rule reports df.index.get_level_values("k").unique() and .nunique(), which build one value per row first. On 1,000,000 rows unique with a level ran 22 times faster. The rule doesn't run on test files.
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.index.unique("k"), df.index.unique("k").nunique().
The fix is unsafe, because a level built from a range comes back as a RangeIndex, where the old form gave an Index. The values are the same.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
get_level_values builds one value per row before unique runs. Write $I.unique($L).Browse the other rules of the pandas pack.