|
6 | 6 | import csv
|
7 | 7 | import datetime
|
8 | 8 | from io import BufferedIOBase, StringIO, TextIOWrapper
|
| 9 | +from itertools import chain |
9 | 10 | import re
|
10 | 11 | import sys
|
11 | 12 | from textwrap import fill
|
@@ -1419,6 +1420,26 @@ def __init__(self, kwds):
|
1419 | 1420 | # keep references to file handles opened by the parser itself
|
1420 | 1421 | self.handles = []
|
1421 | 1422 |
|
| 1423 | + def _confirm_parse_dates_presence(self, columns): |
| 1424 | + """ |
| 1425 | + if user has provided names for parse_dates, check if those columns |
| 1426 | + are available. |
| 1427 | + """ |
| 1428 | + if isinstance(self.parse_dates, list): |
| 1429 | + cols_needed = self.parse_dates |
| 1430 | + elif isinstance(self.parse_dates, dict): |
| 1431 | + cols_needed = chain(*self.parse_dates.values()) |
| 1432 | + else: |
| 1433 | + cols_needed = [] |
| 1434 | + |
| 1435 | + missing_cols = ", ".join( |
| 1436 | + [col for col in cols_needed if isinstance(col, str) and col not in columns] |
| 1437 | + ) |
| 1438 | + if missing_cols: |
| 1439 | + raise ValueError( |
| 1440 | + f"Missing column provided to 'parse_dates': '{missing_cols}'" |
| 1441 | + ) |
| 1442 | + |
1422 | 1443 | def close(self):
|
1423 | 1444 | for f in self.handles:
|
1424 | 1445 | f.close()
|
@@ -1938,6 +1959,7 @@ def __init__(self, src, **kwds):
|
1938 | 1959 | if len(self.names) < len(usecols):
|
1939 | 1960 | _validate_usecols_names(usecols, self.names)
|
1940 | 1961 |
|
| 1962 | + self._confirm_parse_dates_presence(self.names) |
1941 | 1963 | self._set_noconvert_columns()
|
1942 | 1964 |
|
1943 | 1965 | self.orig_names = self.names
|
@@ -2308,6 +2330,7 @@ def __init__(self, f, **kwds):
|
2308 | 2330 | if self.index_names is None:
|
2309 | 2331 | self.index_names = index_names
|
2310 | 2332 |
|
| 2333 | + self._confirm_parse_dates_presence(self.columns) |
2311 | 2334 | if self.parse_dates:
|
2312 | 2335 | self._no_thousands_columns = self._set_no_thousands_columns()
|
2313 | 2336 | else:
|
@@ -3279,7 +3302,9 @@ def _isindex(colspec):
|
3279 | 3302 | if isinstance(colspec, int) and colspec not in data_dict:
|
3280 | 3303 | colspec = orig_names[colspec]
|
3281 | 3304 | elif colspec not in orig_names:
|
3282 |
| - raise ValueError(f"'{colspec}' is not in list") |
| 3305 | + raise ValueError( |
| 3306 | + f"Missing column provided to 'parse_dates': '{colspec}'" |
| 3307 | + ) |
3283 | 3308 | if _isindex(colspec):
|
3284 | 3309 | continue
|
3285 | 3310 | data_dict[colspec] = converter(data_dict[colspec])
|
|
0 commit comments