pandas/set-index-then-resample
| Rule | set-index-then-resample |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Setting a column as the index only to resample takes an extra step. Pass the column to resample with on=.
daily = df.set_index("time").resample("D")["load"].sum()daily = df.resample("D", on="time")["load"].sum()What the rule reports
Section titled “What the rule reports”The rule reports df.set_index("d").resample("D").sum(). The rule reports only a chain that ends in a named aggregation such as sum or mean, because asfreq raises with on=. A function passed to agg also sees other data with on=.
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.resample("D", on="d").sum(), which raises on an empty frame where the indexed form returned.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
resample takes on=, so the column does not have to become the index first. Write $X.resample($$$A, on=$COL).Browse the other rules of the pandas pack.