pandas/sort-then-reset-index
| Rule | sort-then-reset-index |
|---|---|
| Pack | pandas |
| Severity | info |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
reset_index(drop=True) after a sort renumbers the rows in a second step. Pass ignore_index=True to the sort instead.
ordered = df.sort_values("load").reset_index(drop=True)ordered = df.sort_values("load", ignore_index=True)What the rule reports
Section titled “What the rule reports”The rule reports df.sort_values("b").reset_index(drop=True), also after sort_index, dropna, drop_duplicates, explode and sample.
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.sort_values("b", ignore_index=True). reset_index always gives a RangeIndex, and ignore_index=True keeps the old index when the method has nothing to do, as on an empty frame.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
Pass ignore_index=True to $M instead of calling reset_index(drop=True) afterwards.Browse the other rules of the pandas pack.