pandas/to-csv-append-header
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | error | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
to_csv(mode="a") writes the header again on every call, so header rows end up among the data. Pass header=False, or write the header only on the first call.
for part in parts: part.to_csv(path, mode="a", index=False) part.to_csv(path, mode="a", index=False, header=not os.path.exists(path))What the rule reports
Section titled “What the rule reports”The rule reports df.to_csv(path, mode="a") without header=, which writes the header again on every call.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes adds header=False to to_csv(..., mode="a") calls so appending does not repeat headers. It is unsafe because the first write to a new file may instead need header=not os.path.exists(path).
The repeated header rows are read back as data, so every number in the file comes back as text.
Diagnostic message
Section titled “Diagnostic message”to_csv with mode="a" writes the header again on every call, so the file holds header rows among the data. Pass header=False, or header=not os.path.exists(path) for the first write.See all rules in the pandas pack.