Skip to content

Commit f20b87b

Browse files
committed
Review comments
1 parent 972d72b commit f20b87b

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Indexing
5353
I/O
5454
^^^
5555

56+
- Bug in :func:`read_csv` not raising an exception with nonexistent columns in ``usecols`` when it had the correct length (:issue:`14671`)
5657
- Bug that would force importing of the clipboard routines unnecessarily, potentially causing an import error on startup (:issue:`16288`)
5758

5859

pandas/io/parsers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1621,10 +1621,10 @@ def __init__(self, src, **kwds):
16211621
if self.usecols:
16221622
usecols = _evaluate_usecols(self.usecols, self.orig_names)
16231623

1624-
#gh-14671
1625-
if (self.usecols_dtype == 'string') and \
1626-
(not set(usecols).issubset(self.orig_names)):
1627-
raise ValueError("Usecols do not match names.")
1624+
# GH 14671
1625+
if (self.usecols_dtype == 'string' and
1626+
not set(usecols).issubset(self.orig_names)):
1627+
raise ValueError("Usecols do not match names.")
16281628

16291629
if len(self.names) > len(usecols):
16301630
self.names = [n for i, n in enumerate(self.names)

pandas/tests/io/parser/usecols.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import numpy as np
1111
import pandas.util.testing as tm
12-
import re
1312

1413
from pandas import DataFrame, Index
1514
from pandas._libs.lib import Timestamp
@@ -478,44 +477,52 @@ def test_uneven_length_cols(self):
478477
tm.assert_frame_equal(df, expected)
479478

480479
def test_raise_on_usecols_names_mismatch(self):
481-
## see gh-14671
480+
# GH 14671
482481
data = 'a,b,c,d\n1,2,3,4\n5,6,7,8'
483-
msg = 'Usecols do not match names' ## from parsers.py CParserWrapper()
484-
msg2 = 'is not in list' ## from parser.py _handle_usecols()
485482

486-
usecols = ['a','b','c','d']
483+
if self.engine == 'c':
484+
msg = 'Usecols do not match names'
485+
else:
486+
msg = 'is not in list'
487+
488+
usecols = ['a', 'b', 'c', 'd']
487489
df = self.read_csv(StringIO(data), usecols=usecols)
488-
expected = DataFrame({'a': [1,5], 'b': [2,6], 'c': [3,7], 'd': [4,8]})
490+
expected = DataFrame({'a': [1, 5], 'b': [2, 6], 'c': [3, 7],
491+
'd': [4, 8]})
489492
tm.assert_frame_equal(df, expected)
490493

491-
usecols = ['a','b','c','f']
492-
with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")):
494+
usecols = ['a', 'b', 'c', 'f']
495+
with tm.assert_raises_regex(ValueError, msg):
493496
self.read_csv(StringIO(data), usecols=usecols)
494497

495-
usecols = ['a','b','f']
496-
with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")):
498+
usecols = ['a', 'b', 'f']
499+
with tm.assert_raises_regex(ValueError, msg):
497500
self.read_csv(StringIO(data), usecols=usecols)
498501

499502
names = ['A', 'B', 'C', 'D']
500503

501504
df = self.read_csv(StringIO(data), header=0, names=names)
502-
expected = DataFrame({'A': [1,5], 'B': [2,6], 'C': [3,7], 'D': [4,8]})
505+
expected = DataFrame({'A': [1, 5], 'B': [2, 6], 'C': [3, 7],
506+
'D': [4, 8]})
503507
tm.assert_frame_equal(df, expected)
504508

509+
# TODO: https://github.com/pandas-dev/pandas/issues/16469
505510
# usecols = ['A','C']
506-
# df = self.read_csv(StringIO(data), header=0, names=names, usecols=usecols)
511+
# df = self.read_csv(StringIO(data), header=0, names=names,
512+
# usecols=usecols)
507513
# expected = DataFrame({'A': [1,5], 'C': [3,7]})
508514
# tm.assert_frame_equal(df, expected)
509515
#
510516
# usecols = [0,2]
511-
# df = self.read_csv(StringIO(data), header=0, names=names, usecols=usecols)
517+
# df = self.read_csv(StringIO(data), header=0, names=names,
518+
# usecols=usecols)
512519
# expected = DataFrame({'A': [1,5], 'C': [3,7]})
513520
# tm.assert_frame_equal(df, expected)
514521

515-
516-
usecols = ['A','B','C','f']
517-
with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")):
518-
self.read_csv(StringIO(data), header=0, names=names, usecols=usecols)
519-
usecols = ['A','B','f']
520-
with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")):
522+
usecols = ['A', 'B', 'C', 'f']
523+
with tm.assert_raises_regex(ValueError, msg):
524+
self.read_csv(StringIO(data), header=0, names=names,
525+
usecols=usecols)
526+
usecols = ['A', 'B', 'f']
527+
with tm.assert_raises_regex(ValueError, msg):
521528
self.read_csv(StringIO(data), names=names, usecols=usecols)

0 commit comments

Comments
 (0)