Skip to content

Commit 89fecc6

Browse files
committed
TST: parser test coverage
1 parent d076438 commit 89fecc6

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

pandas/io/parsers.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,22 @@ def __iter__(self):
258258

259259
def _get_index_name(self):
260260
columns = self.columns
261-
passed_names = self.names is not None
262261

263262
try:
264263
line = self._next_line()
265264
except StopIteration:
266265
line = None
267266

268267
# implicitly index_col=0 b/c 1 fewer column names
269-
implicit_first_col = (line is not None and
270-
len(line) == len(columns) + 1)
268+
if line is not None:
269+
implicit_first_cols = len(line) - len(columns)
270+
else:
271+
implicit_first_cols = 0
271272

272273
index_name = None
273-
if implicit_first_col:
274+
if implicit_first_cols > 0:
274275
if self.index_col is None:
275-
self.index_col = 0
276+
self.index_col = range(implicit_first_cols)
276277
index_name = None
277278
elif np.isscalar(self.index_col):
278279
index_name = columns.pop(self.index_col)

pandas/io/tests/test_parsers.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from cStringIO import StringIO
22
from datetime import datetime
3+
import csv
34
import os
45
import unittest
56

@@ -9,7 +10,7 @@
910
import numpy as np
1011

1112
from pandas import DataFrame, Index
12-
from pandas.io.parsers import read_csv, read_table, ExcelFile
13+
from pandas.io.parsers import read_csv, read_table, ExcelFile, TextParser
1314
from pandas.util.testing import assert_almost_equal, assert_frame_equal
1415
import pandas._tseries as lib
1516

@@ -246,6 +247,17 @@ def test_iterator(self):
246247
last_chunk = reader.get_chunk(5)
247248
assert_frame_equal(last_chunk, df[3:])
248249

250+
# pass list
251+
lines = list(csv.reader(StringIO(self.data1)))
252+
parser = TextParser(lines, index_col=0, chunksize=2)
253+
254+
df = read_csv(StringIO(self.data1), index_col=0)
255+
256+
chunks = list(parser)
257+
assert_frame_equal(chunks[0], df[:2])
258+
assert_frame_equal(chunks[1], df[2:4])
259+
assert_frame_equal(chunks[2], df[4:])
260+
249261
def test_header_not_first_line(self):
250262
data = """got,to,ignore,this,line
251263
got,to,ignore,this,line
@@ -290,7 +302,19 @@ def test_pass_names_with_index(self):
290302
assert_frame_equal(df, expected)
291303

292304
def test_multi_index_no_level_names(self):
293-
pass
305+
data = """index1,index2,A,B,C,D
306+
foo,one,2,3,4,5
307+
foo,two,7,8,9,10
308+
foo,three,12,13,14,15
309+
bar,one,12,13,14,15
310+
bar,two,12,13,14,15
311+
"""
312+
lines = data.split('\n')
313+
no_header = '\n'.join(lines[1:])
314+
names = ['A', 'B', 'C', 'D']
315+
df = read_csv(StringIO(no_header), index_col=[0, 1], names=names)
316+
expected = read_csv(StringIO(data), index_col=[0, 1])
317+
assert_frame_equal(df, expected)
294318

295319
class TestParseSQL(unittest.TestCase):
296320

0 commit comments

Comments
 (0)