Skip to content

Commit 22f1486

Browse files
Scott Lasleyjreback
Scott Lasley
authored andcommitted
BUG: "index_col=False" not working when "usecols" is specified in read_csv #9082
1 parent e64328e commit 22f1486

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,4 @@ Bug Fixes
153153

154154

155155
- Fixed issue in the ``xlsxwriter`` engine where it added a default 'General' format to cells if no other format wass applied. This prevented other row or column formatting being applied. (:issue:`9167`)
156+
- Fixes issue with ``index_col=False`` when ``usecols`` is also specified in ``read_csv``. (:issue:`9082`)

pandas/io/tests/test_parsers.py

+11
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,17 @@ def test_index_col_named(self):
509509
tm.assert_frame_equal(xp, rs)
510510
self.assertEqual(xp.index.name, rs.index.name)
511511

512+
def test_usecols_index_col_False(self):
513+
# Issue 9082
514+
s = "a,b,c,d\n1,2,3,4\n5,6,7,8"
515+
s_malformed = "a,b,c,d\n1,2,3,4,\n5,6,7,8,"
516+
cols = ['a','c','d']
517+
expected = DataFrame({'a':[1,5], 'c':[3,7], 'd':[4,8]})
518+
df = self.read_csv(StringIO(s), usecols=cols, index_col=False)
519+
tm.assert_frame_equal(expected, df)
520+
df = self.read_csv(StringIO(s_malformed), usecols=cols, index_col=False)
521+
tm.assert_frame_equal(expected, df)
522+
512523
def test_converter_index_col_bug(self):
513524
# 1835
514525
data = "A;B\n1;2\n3;4"

pandas/parser.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ cdef class TextReader:
728728
# 'data has %d fields'
729729
# % (passed_count, field_count))
730730

731-
if self.has_usecols:
731+
if self.has_usecols and self.allow_leading_cols:
732732
nuse = len(self.usecols)
733733
if nuse == passed_count:
734734
self.leading_cols = 0

0 commit comments

Comments
 (0)