@@ -1065,6 +1065,20 @@ def _evaluate_usecols(usecols, names):
1065
1065
return usecols
1066
1066
1067
1067
1068
+ def _validate_usecols (usecols , names ):
1069
+ """
1070
+ Validates that all usecols are present in a given
1071
+ list of names, if not, raise a ValueError that
1072
+ shows what usecols are missing.
1073
+ """
1074
+ missing = [c for c in usecols if c not in names ]
1075
+ if len (missing ) > 0 :
1076
+ raise ValueError (
1077
+ "Usecols do not match columns, "
1078
+ "columns expected but not found: {missing}" .format (missing = missing )
1079
+ )
1080
+
1081
+
1068
1082
def _validate_skipfooter_arg (skipfooter ):
1069
1083
"""
1070
1084
Validate the 'skipfooter' parameter.
@@ -1662,22 +1676,14 @@ def __init__(self, src, **kwds):
1662
1676
# GH 14671
1663
1677
if (self .usecols_dtype == 'string' and
1664
1678
not set (usecols ).issubset (self .orig_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: {}" .format (missing )
1669
- )
1679
+ _validate_usecols (usecols , self .orig_names )
1670
1680
1671
1681
if len (self .names ) > len (usecols ):
1672
1682
self .names = [n for i , n in enumerate (self .names )
1673
1683
if (i in usecols or n in usecols )]
1674
1684
1675
1685
if len (self .names ) < len (usecols ):
1676
- missing = [c for c in usecols if c not in self .names ]
1677
- raise ValueError (
1678
- "Usecols do not match columns, "
1679
- "columns expected but not found: {}" .format (missing )
1680
- )
1686
+ _validate_usecols (usecols , self .names )
1681
1687
1682
1688
self ._set_noconvert_columns ()
1683
1689
@@ -2451,16 +2457,12 @@ def _handle_usecols(self, columns, usecols_key):
2451
2457
"be integers." )
2452
2458
col_indices = []
2453
2459
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
-
2461
2460
for col in self .usecols :
2462
2461
if isinstance (col , string_types ):
2463
- col_indices .append (usecols_key .index (col ))
2462
+ try :
2463
+ col_indices .append (usecols_key .index (col ))
2464
+ except ValueError :
2465
+ _validate_usecols (self .usecols , usecols_key )
2464
2466
else :
2465
2467
col_indices .append (col )
2466
2468
else :
0 commit comments