pandas/apply-pd-series
| Rule | apply-pd-series |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
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.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
apply(pd.Series) builds one Series per row. Build the frame once with pd.DataFrame($S.tolist(), index=$S.index).Browse the other rules of the pandas pack.