๐ File Operations ยท 6 min read
Empty CSV File: Fixing pandas "No columns to parse from file"
A CSV with no data in it, such as a zero-byte file or one that only contains blank lines, makes pandas raise EmptyDataError: No columns to parse from file, because read_csv has no header row to turn into column names. Give the file a header, or check for emptiness before it reaches the parser, and the crash goes away.
This is not a corrupted-file problem in most cases. It is the parser asking a reasonable question: which column is which? When there is no first line to answer that question, there is nothing to build a table from.
When read_csv Raises EmptyDataError
We reproduced the behaviour on Python 3.13.5 with pandas 2.3.3, feeding read_csv four different inputs. The boundary is exact:
| Input | Result |
|---|---|
| Zero bytes (empty string) | EmptyDataError: No columns to parse from file |
| A single newline | EmptyDataError: No columns to parse from file |
Header only (id,name,price) | Reads fine, shape (0, 3) |
| Blank line, then a header | Reads fine, shape (1, 3) |
Only the first two are empty in the sense that matters. A header row with no data rows is a valid, if boring, CSV: it comes back as an empty DataFrame with the right columns. The error is specifically about the header, not about missing data.
Why the Header Decides
pandas treats the first non-blank line as the header by default. That line becomes the column names, and every line after it becomes data. If there is no first line, there are no columns, and pandas raises EmptyDataError rather than guess.
The blank-line case is worth knowing: read_csv skips blank lines by default (skip_blank_lines=True), so a file that starts with empty lines and then has a header parses normally. It is only when no header can be found anywhere that the error appears.
Two Real Causes of an Empty File
1. The export ran on zero rows
A filter matched nothing, a database query returned no records, or a scheduled job ran before new data arrived. The export finished successfully and wrote an empty file, so there is no error to catch upstream, only a blank result downstream.
2. The file was truncated
A download that dropped mid-transfer, a save that hit a full disk, or a network timeout can leave a file that is smaller than expected, sometimes zero bytes. Before you blame the parser, check the file size and open it in a plain text editor such as Notepad or VS Code.
Fixes That Stop the Crash
| Situation | What to do |
|---|---|
| Read the file in a script | if os.path.getsize(path) == 0: skip the read, or treat it as empty |
| Need a graceful fallback | catch pandas.errors.EmptyDataError and return an empty DataFrame |
| You control the exporter | always write a header row, even when there are zero data rows |
| Check a file before trusting it | run it through the analyzer to see row and column counts at a glance |
The size check is the cheapest and most reliable guard. os.path.getsize reads metadata, not the file, so it costs almost nothing and catches both the zero-byte file and the truncated download before pandas ever sees them.
If the file comes from a system you control, fix the source: an exporter that writes a header row unconditionally removes the whole class of error, because a header plus zero rows is always a valid read.
What We Measured
Everything above was reproduced rather than assumed. The script lives at /opt/data/cad_work/seo_geo_ops/nc_measure_20261112/measure.py and ran on 2026-11-12 with Python 3.13.5 and pandas 2.3.3. Its exact output:
T1a. read_csv on a zero-byte file: EmptyDataError: No columns to parse from file T1b. read_csv on a whitespace-only file (newline only): EmptyDataError: No columns to parse from file T1c. header only, no data rows: ok, shape (0, 3) | columns ['id', 'name', 'price'] T1d. a blank first line, then a header: ok, shape (1, 3) | columns ['id', 'name', 'price']
The same four inputs give the same four outcomes on any machine with pandas 2.x, which is the point: the behaviour is deterministic, so the fix can be too.
Frequently Asked Questions
Why does pandas say 'No columns to parse from file'?
Because the file has no header row for read_csv to turn into column names. A zero-byte file and a file that contains only blank lines both raise EmptyDataError: No columns to parse from file, while a file with a header and no data rows reads as an empty DataFrame with those columns.
Does an empty CSV always crash read_csv?
No. A file with a header row but no data rows reads cleanly as a DataFrame with 0 rows and the header columns. Only a file with no header at all, such as a zero-byte or whitespace-only file, raises EmptyDataError.
How do I read an empty CSV file without crashing?
Check the size first with os.path.getsize, or wrap the read in a try/except that catches pandas.errors.EmptyDataError and treats it as an empty DataFrame. Alternatively, write a header row to the file so read_csv always has columns to parse.
Why is my exported CSV empty when the data looked fine?
Usually the export ran on zero rows: a filter matched nothing, a database query returned no results, or a download was interrupted and the file was truncated. Check the file size and open it in a plain text editor before assuming the parser is wrong.
Does pandas skip blank lines in a CSV?
Yes. read_csv skips blank lines by default via skip_blank_lines=True, so a file that starts with blank lines followed by a header reads normally. The EmptyDataError only appears when no header row can be found anywhere in the file.
See If Your File Is Really Empty
Upload a CSV and get the row count, column count and empty-field breakdown before it reaches anyone who trusts the numbers.
Related reading
File Operations โ other guides that pair well with this one.
- microsoft-outage-export-your-data
- check-csv-export-after-outage
- How to Compress a CSV File
- CSV File Limit: How Many Rows Can a CSV File Have?
Browse all guides in the NoCodeCSV blog.