pandas/apply-pd-series
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
apply(pd.Series) builds one Series for every row to split a column of lists or dicts. Build the frame once with pd.DataFrame(s.tolist(), index=s.index).
parts = df["pair"].apply(pd.Series)parts = pd.DataFrame(df["pair"].tolist(), index=df["pair"].index)What the rule reports
Section titled “What the rule reports”The rule reports df["pair"].apply(pd.Series), which builds one Series per row to split a column of lists or dicts. It reports only a column taken by a text label, because on a frame apply works per column.
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.DataFrame(df["pair"].tolist(), index=df["pair"].index).
The fix is unsafe, because the old form turned whole numbers into floats when a row also held floats, and the fix keeps them whole.
For lists of unequal length, check how the result pads short rows. The two forms pad them the same way when the first row is the longest.
Diagnostic message
Section titled “Diagnostic message”apply(pd.Series) builds one Series per row. Build the frame once with pd.DataFrame($S.tolist(), index=$S.index).See all rules in the pandas pack.