Skip to content

Commit 33b816d

Browse files
committed
BUG: fix TextParser with list buglet, enable parsing of DataFrame output with index names
1 parent a37e666 commit 33b816d

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

pandas/io/parsers.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,24 @@ def _get_index_name(self):
345345
except StopIteration:
346346
line = None
347347

348+
try:
349+
next_line = self._next_line()
350+
except StopIteration:
351+
next_line = None
352+
353+
index_name = None
354+
348355
# implicitly index_col=0 b/c 1 fewer column names
356+
implicit_first_cols = 0
349357
if line is not None:
350358
implicit_first_cols = len(line) - len(columns)
351-
else:
352-
implicit_first_cols = 0
359+
if next_line is not None:
360+
if len(next_line) == len(line) + len(columns):
361+
implicit_first_cols = 0
362+
self.index_col = range(len(line))
363+
self.buf = self.buf[1:]
364+
return line
353365

354-
index_name = None
355366
if implicit_first_cols > 0:
356367
if self.index_col is None:
357368
if implicit_first_cols == 1:
@@ -463,7 +474,7 @@ def _get_lines(self, rows=None):
463474
rows -= len(self.buf)
464475

465476
if isinstance(source, list):
466-
if self.pos >= len(source):
477+
if self.pos > len(source):
467478
raise StopIteration
468479
if rows is None:
469480
lines.extend(source[self.pos:])

pandas/io/tests/test_parsers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,17 @@ def test_verbose_import(self):
555555
finally:
556556
sys.stdout = sys.__stdout__
557557

558+
def test_read_table_buglet_4x_multiindex(self):
559+
text = """ A B C D E
560+
one two three four
561+
a b 10.0032 5 -0.5109 -2.3358 -0.4645 0.05076 0.3640
562+
a q 20 4 0.4473 1.4152 0.2834 1.00661 0.1744
563+
x q 30 3 -0.6662 -0.5243 -0.3580 0.89145 2.5838"""
564+
565+
# it works!
566+
df = read_table(StringIO(text), sep='\s+')
567+
self.assertEquals(df.index.names, ['one', 'two', 'three', 'four'])
568+
558569
class TestParseSQL(unittest.TestCase):
559570

560571
def test_convert_sql_column_floats(self):

0 commit comments

Comments
 (0)