Skip to content

Commit 0eb1587

Browse files
Terry Santegoedssantegoeds
Terry Santegoeds
authored andcommitted
BUG: Fix csv_read bugs when using empty input. GH10467 & GH10413
1 parent 9da54ad commit 0eb1587

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

doc/source/whatsnew/v0.17.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,5 @@ Bug Fixes
134134

135135
- Bug in `pandas.concat` with ``axis=0`` when column is of dtype ``category`` (:issue:`10177`)
136136
- Bug in ``read_msgpack`` where input type is not always checked (:issue:`10369`)
137+
138+
- Bug in `pandas.read_csv` with ``index_col=False`` or with ``index_col=['a', 'b']`` (:issue:`10413`, :issue:`10467`)

pandas/io/parsers.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2223,13 +2223,13 @@ def _clean_index_names(columns, index_col):
22232223
def _get_empty_meta(columns, index_col, index_names):
22242224
columns = list(columns)
22252225

2226-
if index_col is not None:
2227-
index = MultiIndex.from_arrays([[]] * len(index_col),
2228-
names=index_names)
2229-
for n in index_col:
2230-
columns.pop(n)
2231-
else:
2226+
if index_col is None or index_col is False:
22322227
index = Index([])
2228+
else:
2229+
index_col = list(index_col)
2230+
index = MultiIndex.from_arrays([[]] * len(index_col), names=index_names)
2231+
for i, n in enumerate(index_col):
2232+
columns.pop(n-i)
22332233

22342234
return index, columns, {}
22352235

pandas/io/tests/test_parsers.py

+14
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,20 @@ def test_empty_with_index(self):
23012301
expected = DataFrame([], columns=['y'], index=Index([], name='x'))
23022302
tm.assert_frame_equal(result, expected)
23032303

2304+
def test_emtpy_with_multiindex(self):
2305+
# GH 10467
2306+
data = 'x,y,z'
2307+
result = self.read_csv(StringIO(data), index_col=['x', 'y'])
2308+
expected = DataFrame([], columns=['z'], index=MultiIndex.from_arrays([[]] * 2, names=['x', 'y']))
2309+
tm.assert_frame_equal(result, expected)
2310+
2311+
def test_empty_with_index_col_false(self):
2312+
# GH 10413
2313+
data = 'x,y'
2314+
result = self.read_csv(StringIO(data), index_col=False)
2315+
expected = DataFrame([], columns=['x', 'y'])
2316+
tm.assert_frame_equal(result, expected)
2317+
23042318
def test_float_parser(self):
23052319
# GH 9565
23062320
data = '45e-1,4.5,45.,inf,-inf'

0 commit comments

Comments
 (0)