@@ -1065,38 +1065,6 @@ 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
- Parameters
1075
- ----------
1076
- usecols : iterable of usecols
1077
- The columns to validate are present in names.
1078
- names : iterable of names
1079
- The column names to check against.
1080
-
1081
- Returns
1082
- -------
1083
- usecols : iterable of usecols
1084
- The `usecols` parameter if the validation succeeds.
1085
-
1086
- Raises
1087
- ------
1088
- ValueError : Columns were missing. Error message will list them.
1089
- """
1090
- missing = [c for c in usecols if c not in names ]
1091
- if len (missing ) > 0 :
1092
- raise ValueError (
1093
- "Usecols do not match columns, "
1094
- "columns expected but not found: {missing}" .format (missing = missing )
1095
- )
1096
-
1097
- return usecols
1098
-
1099
-
1100
1068
def _validate_skipfooter_arg (skipfooter ):
1101
1069
"""
1102
1070
Validate the 'skipfooter' parameter.
@@ -1128,24 +1096,31 @@ def _validate_skipfooter_arg(skipfooter):
1128
1096
return skipfooter
1129
1097
1130
1098
1131
- def _validate_usecols_arg (usecols ):
1099
+ def _validate_usecols_arg (usecols , names = None ):
1132
1100
"""
1133
1101
Validate the 'usecols' parameter.
1134
1102
1135
1103
Checks whether or not the 'usecols' parameter contains all integers
1136
1104
(column selection by index), strings (column by name) or is a callable.
1137
1105
Raises a ValueError if that is not the case.
1138
1106
1107
+ If 'names' is passed, validates that all usecols are present
1108
+ in a given list of names. If not, raise a ValueError that
1109
+ shows what usecols are missing.
1110
+
1139
1111
Parameters
1140
1112
----------
1141
1113
usecols : array-like, callable, or None
1142
1114
List of columns to use when parsing or a callable that can be used
1143
1115
to filter a list of table columns.
1116
+ names: iterable, default None
1117
+ Iterable of names to check usecols against.
1118
+
1144
1119
1145
1120
Returns
1146
1121
-------
1147
1122
usecols_tuple : tuple
1148
- A tuple of (verified_usecols, usecols_dtype).
1123
+ If names is not None, a tuple of (verified_usecols, usecols_dtype).
1149
1124
1150
1125
'verified_usecols' is either a set if an array-like is passed in or
1151
1126
'usecols' if a callable or None is passed in.
@@ -1156,16 +1131,24 @@ def _validate_usecols_arg(usecols):
1156
1131
msg = ("'usecols' must either be all strings, all unicode, "
1157
1132
"all integers or a callable" )
1158
1133
1159
- if usecols is not None :
1160
- if callable (usecols ):
1161
- return usecols , None
1162
- usecols_dtype = lib .infer_dtype (usecols )
1163
- if usecols_dtype not in ('empty' , 'integer' ,
1164
- 'string' , 'unicode' ):
1165
- raise ValueError (msg )
1134
+ if names is None :
1135
+ if usecols is not None :
1136
+ if callable (usecols ):
1137
+ return usecols , None
1138
+ usecols_dtype = lib .infer_dtype (usecols )
1139
+ if usecols_dtype not in ('empty' , 'integer' ,
1140
+ 'string' , 'unicode' ):
1141
+ raise ValueError (msg )
1166
1142
1167
- return set (usecols ), usecols_dtype
1168
- return usecols , None
1143
+ return set (usecols ), usecols_dtype
1144
+ return usecols , None
1145
+ else :
1146
+ missing = [c for c in usecols if c not in names ]
1147
+ if len (missing ) > 0 :
1148
+ raise ValueError (
1149
+ "Usecols do not match columns, columns expected "
1150
+ "but not found: {missing}" .format (missing = missing )
1151
+ )
1169
1152
1170
1153
1171
1154
def _validate_parse_dates_arg (parse_dates ):
@@ -1694,14 +1677,14 @@ def __init__(self, src, **kwds):
1694
1677
# GH 14671
1695
1678
if (self .usecols_dtype == 'string' and
1696
1679
not set (usecols ).issubset (self .orig_names )):
1697
- _validate_usecols (usecols , self .orig_names )
1680
+ _validate_usecols_arg (usecols , self .orig_names )
1698
1681
1699
1682
if len (self .names ) > len (usecols ):
1700
1683
self .names = [n for i , n in enumerate (self .names )
1701
1684
if (i in usecols or n in usecols )]
1702
1685
1703
1686
if len (self .names ) < len (usecols ):
1704
- _validate_usecols (usecols , self .names )
1687
+ _validate_usecols_arg (usecols , self .names )
1705
1688
1706
1689
self ._set_noconvert_columns ()
1707
1690
@@ -2480,7 +2463,7 @@ def _handle_usecols(self, columns, usecols_key):
2480
2463
try :
2481
2464
col_indices .append (usecols_key .index (col ))
2482
2465
except ValueError :
2483
- _validate_usecols (self .usecols , usecols_key )
2466
+ _validate_usecols_arg (self .usecols , usecols_key )
2484
2467
else :
2485
2468
col_indices .append (col )
2486
2469
else :
0 commit comments