Skip to content

Commit ff57979

Browse files
committed
ENH: raise a more a useful exception on empty files
1 parent 2b4fd7c commit ff57979

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ pandas 0.11.1
9696
explicitly checking a website as a proxy for seeing if there is network
9797
connectivity. Plus, new ``optional_args`` decorator factory for decorators.
9898
(:issue:`3910`, :issue:`3914`)
99+
- ``read_csv`` will now throw a more informative error message when a file
100+
contains no columns, e.g., all newline characters
99101

100102
**API Changes**
101103

doc/source/v0.11.1.txt

+3
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ I/O Enhancements
240240
import os
241241
os.remove(path)
242242

243+
- ``read_csv`` will now throw a more informative error message when a file
244+
contains no columns, e.g., all newline characters
245+
243246
Other Enhancements
244247
~~~~~~~~~~~~~~~~~~
245248

pandas/io/parsers.py

+1
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ def __init__(self, src, **kwds):
949949

950950
# #2442
951951
kwds['allow_leading_cols'] = self.index_col is not False
952+
952953
self._reader = _parser.TextReader(src, **kwds)
953954

954955
# XXX

pandas/io/tests/test_parsers.py

+13
Original file line numberDiff line numberDiff line change
@@ -2330,6 +2330,19 @@ def test_tokenize_CR_with_quoting(self):
23302330
expected = self.read_csv(StringIO(data.replace('\r', '\n')))
23312331
tm.assert_frame_equal(result, expected)
23322332

2333+
def test_raise_on_no_columns(self):
2334+
# single newline
2335+
data = """
2336+
"""
2337+
self.assertRaises(ValueError, self.read_csv, StringIO(data))
2338+
2339+
# test with more than a single newline
2340+
data = """
2341+
2342+
2343+
"""
2344+
self.assertRaises(ValueError, self.read_csv, StringIO(data))
2345+
23332346

23342347
class TestParseSQL(unittest.TestCase):
23352348

pandas/parser.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ cdef class TextReader:
476476
self.names = names
477477
self.header, self.table_width = self._get_header()
478478

479+
if not self.table_width:
480+
raise ValueError("No columns to parse from file")
481+
479482
# compute buffer_lines as function of table width
480483
heuristic = 2**20 // self.table_width
481484
self.buffer_lines = 1

0 commit comments

Comments
 (0)