Skip to content

Commit b0e102a

Browse files
ENH: better error message if usecols doesn't match
GH17301: Improving the error message given when usecols doesn't match with the columns provided. Signed-off-by: Aaron Critchley <[email protected]>
1 parent 91c2f1f commit b0e102a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pandas/io/parsers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,14 +1662,22 @@ def __init__(self, src, **kwds):
16621662
# GH 14671
16631663
if (self.usecols_dtype == 'string' and
16641664
not set(usecols).issubset(self.orig_names)):
1665-
raise ValueError("Usecols do not match names.")
1665+
missing = [c for c in usecols if c not in self.orig_names]
1666+
raise ValueError(
1667+
"Usecols do not match columns, "
1668+
"columns expected but not found: %s" % missing
1669+
)
16661670

16671671
if len(self.names) > len(usecols):
16681672
self.names = [n for i, n in enumerate(self.names)
16691673
if (i in usecols or n in usecols)]
16701674

16711675
if len(self.names) < len(usecols):
1672-
raise ValueError("Usecols do not match names.")
1676+
missing = [c for c in usecols if c not in self.orig_names]
1677+
raise ValueError(
1678+
"Usecols do not match columns, "
1679+
"columns expected but not found: %s" % missing
1680+
)
16731681

16741682
self._set_noconvert_columns()
16751683

pandas/tests/io/parser/usecols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def test_raise_on_usecols_names_mismatch(self):
481481
data = 'a,b,c,d\n1,2,3,4\n5,6,7,8'
482482

483483
if self.engine == 'c':
484-
msg = 'Usecols do not match names'
484+
msg = "do not match columns, columns expected but not found"
485485
else:
486486
msg = 'is not in list'
487487

0 commit comments

Comments
 (0)