Skip to content

pandas/sort-then-reset-index

Pack Severity Fix Versions
pandas info unsafe, with xpd check --fix --unsafe-fixes All versions

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)

The rule reports df.sort_values("b").reset_index(drop=True), also after sort_index, dropna, drop_duplicates, explode and sample.

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.

Pass ignore_index=True to $M instead of calling reset_index(drop=True) afterwards.

See all rules in the pandas pack.