pandas/to-csv-index
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
to_csv writes the index as a first column, which read_csv reads back as Unnamed: 0. Pass index=False, or index=True when the index holds data.
df.to_csv(path)df.to_csv(path, index=False)What the rule reports
Section titled “What the rule reports”The rule reports df.to_csv(path) without index=, which writes the index as an extra column without a name, or under its name when the index has one. The rule leaves alone a call with no argument and a call that forwards **options.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes adds index=False to to_csv(...) calls that omit index=. It is unsafe because you should pass index=True instead when the index holds meaningful data.
Diagnostic message
Section titled “Diagnostic message”to_csv writes the index as a first column, and when the index has no name read_csv returns it as "Unnamed: 0". Pass index=False, or index=True if the index holds data.See all rules in the pandas pack.