Skip to content

Commit 15d4786

Browse files
WIP: Changing Python parser behaviour, better tests
1 parent b0e102a commit 15d4786

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

pandas/io/parsers.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,18 +1665,18 @@ def __init__(self, src, **kwds):
16651665
missing = [c for c in usecols if c not in self.orig_names]
16661666
raise ValueError(
16671667
"Usecols do not match columns, "
1668-
"columns expected but not found: %s" % missing
1668+
"columns expected but not found: {}".format(missing)
16691669
)
16701670

16711671
if len(self.names) > len(usecols):
16721672
self.names = [n for i, n in enumerate(self.names)
16731673
if (i in usecols or n in usecols)]
16741674

16751675
if len(self.names) < len(usecols):
1676-
missing = [c for c in usecols if c not in self.orig_names]
1676+
missing = [c for c in usecols if c not in self.names]
16771677
raise ValueError(
16781678
"Usecols do not match columns, "
1679-
"columns expected but not found: %s" % missing
1679+
"columns expected but not found: {}".format(missing)
16801680
)
16811681

16821682
self._set_noconvert_columns()
@@ -2450,6 +2450,14 @@ def _handle_usecols(self, columns, usecols_key):
24502450
raise ValueError("If using multiple headers, usecols must "
24512451
"be integers.")
24522452
col_indices = []
2453+
2454+
missing = [c for c in self.usecols if c not in usecols_key]
2455+
if len(missing) > 0:
2456+
raise ValueError(
2457+
"Usecols do not match columns, "
2458+
"columns expected but not found: {}".format(missing)
2459+
)
2460+
24532461
for col in self.usecols:
24542462
if isinstance(col, string_types):
24552463
col_indices.append(usecols_key.index(col))

pandas/tests/io/parser/usecols.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,10 @@ def test_raise_on_usecols_names_mismatch(self):
480480
# GH 14671
481481
data = 'a,b,c,d\n1,2,3,4\n5,6,7,8'
482482

483-
if self.engine == 'c':
484-
msg = "do not match columns, columns expected but not found"
485-
else:
486-
msg = 'is not in list'
483+
msg = (
484+
"Usecols do not match columns, "
485+
"columns expected but not found: {}"
486+
)
487487

488488
usecols = ['a', 'b', 'c', 'd']
489489
df = self.read_csv(StringIO(data), usecols=usecols)
@@ -492,11 +492,11 @@ def test_raise_on_usecols_names_mismatch(self):
492492
tm.assert_frame_equal(df, expected)
493493

494494
usecols = ['a', 'b', 'c', 'f']
495-
with tm.assert_raises_regex(ValueError, msg):
495+
with tm.assert_raises_regex(ValueError, msg.format(['f'])):
496496
self.read_csv(StringIO(data), usecols=usecols)
497497

498498
usecols = ['a', 'b', 'f']
499-
with tm.assert_raises_regex(ValueError, msg):
499+
with tm.assert_raises_regex(ValueError, msg.format(['f'])):
500500
self.read_csv(StringIO(data), usecols=usecols)
501501

502502
names = ['A', 'B', 'C', 'D']
@@ -520,9 +520,9 @@ def test_raise_on_usecols_names_mismatch(self):
520520
# tm.assert_frame_equal(df, expected)
521521

522522
usecols = ['A', 'B', 'C', 'f']
523-
with tm.assert_raises_regex(ValueError, msg):
523+
with tm.assert_ra(ValueError, msg.format(['f'])):
524524
self.read_csv(StringIO(data), header=0, names=names,
525525
usecols=usecols)
526526
usecols = ['A', 'B', 'f']
527-
with tm.assert_raises_regex(ValueError, msg):
527+
with tm.assert_raises_regex(ValueError, msg.format(['f'])):
528528
self.read_csv(StringIO(data), names=names, usecols=usecols)

0 commit comments

Comments
 (0)