|
9 | 9 |
|
10 | 10 | import numpy as np
|
11 | 11 | import pandas.util.testing as tm
|
| 12 | +import re |
12 | 13 |
|
13 | 14 | from pandas import DataFrame, Index
|
14 | 15 | from pandas._libs.lib import Timestamp
|
@@ -475,3 +476,20 @@ def test_uneven_length_cols(self):
|
475 | 476 | 'C': [3, 5, 4, 3, 3, 7]})
|
476 | 477 | df = self.read_csv(StringIO(data), usecols=usecols)
|
477 | 478 | tm.assert_frame_equal(df, expected)
|
| 479 | + |
| 480 | + def test_raise_on_usecols_names_mismatch(self): |
| 481 | + # see gh-14671 |
| 482 | + data = 'a,b,c,d\n1,2,3,4\n5,6,7,8' |
| 483 | + usecols = ['a','b','c','d'] |
| 484 | + df = self.read_csv(StringIO(data), usecols=usecols) |
| 485 | + expected = DataFrame({'a': [1,5], 'b': [2,6], 'c': [3,7], 'd': [4,8]}) |
| 486 | + tm.assert_frame_equal(df, expected) |
| 487 | + |
| 488 | + msg = 'Usecols do not match names' ## from parsers.py CParserWrapper() |
| 489 | + msg2 = 'is not in list' ## from parser.py _handle_usecols() |
| 490 | + usecols = ['a','b','c','f'] |
| 491 | + with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")): |
| 492 | + self.read_csv(StringIO(data), usecols=usecols) |
| 493 | + usecols = ['a','b','f'] |
| 494 | + with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")): |
| 495 | + self.read_csv(StringIO(data), usecols=usecols) |
0 commit comments