|
6 | 6 | import csv
|
7 | 7 | import datetime
|
8 | 8 | from io import BufferedIOBase, RawIOBase, StringIO, TextIOWrapper
|
| 9 | +from itertools import chain |
9 | 10 | import re
|
10 | 11 | import sys
|
11 | 12 | from textwrap import fill
|
@@ -1423,6 +1424,26 @@ def __init__(self, kwds):
|
1423 | 1424 | # keep references to file handles opened by the parser itself
|
1424 | 1425 | self.handles = []
|
1425 | 1426 |
|
| 1427 | + def _confirm_parse_dates_presence(self, columns): |
| 1428 | + """ |
| 1429 | + if user has provided names for parse_dates, check if those columns |
| 1430 | + are available. |
| 1431 | + """ |
| 1432 | + if isinstance(self.parse_dates, list): |
| 1433 | + cols_needed = self.parse_dates |
| 1434 | + elif isinstance(self.parse_dates, dict): |
| 1435 | + cols_needed = chain(*self.parse_dates.values()) |
| 1436 | + else: |
| 1437 | + cols_needed = [] |
| 1438 | + |
| 1439 | + missing_cols = ", ".join( |
| 1440 | + [col for col in cols_needed if isinstance(col, str) and col not in columns] |
| 1441 | + ) |
| 1442 | + if missing_cols: |
| 1443 | + raise ValueError( |
| 1444 | + f"Missing column provided to 'parse_dates': '{missing_cols}'" |
| 1445 | + ) |
| 1446 | + |
1426 | 1447 | def close(self):
|
1427 | 1448 | for f in self.handles:
|
1428 | 1449 | f.close()
|
@@ -1942,6 +1963,7 @@ def __init__(self, src, **kwds):
|
1942 | 1963 | if len(self.names) < len(usecols):
|
1943 | 1964 | _validate_usecols_names(usecols, self.names)
|
1944 | 1965 |
|
| 1966 | + self._confirm_parse_dates_presence(self.names) |
1945 | 1967 | self._set_noconvert_columns()
|
1946 | 1968 |
|
1947 | 1969 | self.orig_names = self.names
|
@@ -2312,6 +2334,7 @@ def __init__(self, f, **kwds):
|
2312 | 2334 | if self.index_names is None:
|
2313 | 2335 | self.index_names = index_names
|
2314 | 2336 |
|
| 2337 | + self._confirm_parse_dates_presence(self.columns) |
2315 | 2338 | if self.parse_dates:
|
2316 | 2339 | self._no_thousands_columns = self._set_no_thousands_columns()
|
2317 | 2340 | else:
|
@@ -3283,7 +3306,9 @@ def _isindex(colspec):
|
3283 | 3306 | if isinstance(colspec, int) and colspec not in data_dict:
|
3284 | 3307 | colspec = orig_names[colspec]
|
3285 | 3308 | elif colspec not in orig_names:
|
3286 |
| - raise ValueError(f"'{colspec}' is not in list") |
| 3309 | + raise ValueError( |
| 3310 | + f"Missing column provided to 'parse_dates': '{colspec}'" |
| 3311 | + ) |
3287 | 3312 | if _isindex(colspec):
|
3288 | 3313 | continue
|
3289 | 3314 | data_dict[colspec] = converter(data_dict[colspec])
|
|
0 commit comments