pandas/concat-then-category
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
pandas 3.1+ |
Casting to category after concat makes a second pass over the data. On pandas 3.1 and later, pass union_categories=True to concat instead.
combined = pd.concat(parts).astype("category")combined = pd.concat(parts, union_categories=True)What the rule reports
Section titled “What the rule reports”The rule reports pd.concat(parts).astype("category") on pandas 3.1 and later.
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 pd.concat(parts, union_categories=True).
The fix is unsafe, because the order of the categories differs. Parts that are not categorical also stay as they are.
Diagnostic message
Section titled “Diagnostic message”concat(..., union_categories=True) keeps the categorical dtype without a second pass over the data.See all rules in the pandas pack.