pandas/filter-items
| Rule | filter-items |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
filter(items=) skips a missing name without an error, and pandas 3.1 plans to deprecate it. Select with df[names], which raises on a missing name.
selected = df.filter(items=output_columns)selected = df[output_columns]What the rule reports
Section titled “What the rule reports”The rule reports df.filter(items=cols).
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[cols].
The fix is unsafe, because df[cols] raises KeyError on a label that is absent, where filter skipped the label.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
filter(items=) skips a name that is missing without an error, and pandas 3.1 says it will be deprecated. Write $X[$L], which raises on a missing name.Browse the other rules of the pandas pack.