Skip to content

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))

The rule reports df.to_csv(path, mode="a") without header=, which writes the header again on every call.

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.

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.