pandas/repeated-reset-index
| Rule | repeated-reset-index |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
A function that calls reset_index three times or more keeps moving keys between the index and the columns. Keep the keys as columns from the start.
def summary(rows): group = rows.groupby(KEY, sort=False) keys = group.size().reset_index()[KEY] labels = group[LABELS].first().reset_index(drop=True) return keys.join(labels).reset_index(drop=True) return rows.groupby(KEY, sort=False, as_index=False)[LABELS].first()What the rule reports
Section titled “What the rule reports”The rule reports a function that calls reset_index three times or more.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Each call moves the same keys between the index and the columns and copies the frame. A reader
has to track after every line whether a key is an index level or a column. With
as_index=False the keys stay columns, and merge and sort_values(ignore_index=True) leave no
index to reset.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This function calls reset_index three times or more. Keep the keys as columns from the start, with groupby(keys, as_index=False), merge, and ignore_index=True, so that no index has to be reset.Browse the other rules of the pandas pack.