|
9 | 9 |
|
10 | 10 | import numpy as np
|
11 | 11 | import pandas.util.testing as tm
|
12 |
| -import re |
13 | 12 |
|
14 | 13 | from pandas import DataFrame, Index
|
15 | 14 | from pandas._libs.lib import Timestamp
|
@@ -478,44 +477,52 @@ def test_uneven_length_cols(self):
|
478 | 477 | tm.assert_frame_equal(df, expected)
|
479 | 478 |
|
480 | 479 | def test_raise_on_usecols_names_mismatch(self):
|
481 |
| - ## see gh-14671 |
| 480 | + # GH 14671 |
482 | 481 | data = 'a,b,c,d\n1,2,3,4\n5,6,7,8'
|
483 |
| - msg = 'Usecols do not match names' ## from parsers.py CParserWrapper() |
484 |
| - msg2 = 'is not in list' ## from parser.py _handle_usecols() |
485 | 482 |
|
486 |
| - usecols = ['a','b','c','d'] |
| 483 | + if self.engine == 'c': |
| 484 | + msg = 'Usecols do not match names' |
| 485 | + else: |
| 486 | + msg = 'is not in list' |
| 487 | + |
| 488 | + usecols = ['a', 'b', 'c', 'd'] |
487 | 489 | df = self.read_csv(StringIO(data), usecols=usecols)
|
488 |
| - expected = DataFrame({'a': [1,5], 'b': [2,6], 'c': [3,7], 'd': [4,8]}) |
| 490 | + expected = DataFrame({'a': [1, 5], 'b': [2, 6], 'c': [3, 7], |
| 491 | + 'd': [4, 8]}) |
489 | 492 | tm.assert_frame_equal(df, expected)
|
490 | 493 |
|
491 |
| - usecols = ['a','b','c','f'] |
492 |
| - with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")): |
| 494 | + usecols = ['a', 'b', 'c', 'f'] |
| 495 | + with tm.assert_raises_regex(ValueError, msg): |
493 | 496 | self.read_csv(StringIO(data), usecols=usecols)
|
494 | 497 |
|
495 |
| - usecols = ['a','b','f'] |
496 |
| - with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")): |
| 498 | + usecols = ['a', 'b', 'f'] |
| 499 | + with tm.assert_raises_regex(ValueError, msg): |
497 | 500 | self.read_csv(StringIO(data), usecols=usecols)
|
498 | 501 |
|
499 | 502 | names = ['A', 'B', 'C', 'D']
|
500 | 503 |
|
501 | 504 | df = self.read_csv(StringIO(data), header=0, names=names)
|
502 |
| - expected = DataFrame({'A': [1,5], 'B': [2,6], 'C': [3,7], 'D': [4,8]}) |
| 505 | + expected = DataFrame({'A': [1, 5], 'B': [2, 6], 'C': [3, 7], |
| 506 | + 'D': [4, 8]}) |
503 | 507 | tm.assert_frame_equal(df, expected)
|
504 | 508 |
|
| 509 | + # TODO: https://github.com/pandas-dev/pandas/issues/16469 |
505 | 510 | # usecols = ['A','C']
|
506 |
| - # df = self.read_csv(StringIO(data), header=0, names=names, usecols=usecols) |
| 511 | + # df = self.read_csv(StringIO(data), header=0, names=names, |
| 512 | + # usecols=usecols) |
507 | 513 | # expected = DataFrame({'A': [1,5], 'C': [3,7]})
|
508 | 514 | # tm.assert_frame_equal(df, expected)
|
509 | 515 | #
|
510 | 516 | # usecols = [0,2]
|
511 |
| - # df = self.read_csv(StringIO(data), header=0, names=names, usecols=usecols) |
| 517 | + # df = self.read_csv(StringIO(data), header=0, names=names, |
| 518 | + # usecols=usecols) |
512 | 519 | # expected = DataFrame({'A': [1,5], 'C': [3,7]})
|
513 | 520 | # tm.assert_frame_equal(df, expected)
|
514 | 521 |
|
515 |
| - |
516 |
| - usecols = ['A','B','C','f'] |
517 |
| - with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")): |
518 |
| - self.read_csv(StringIO(data), header=0, names=names, usecols=usecols) |
519 |
| - usecols = ['A','B','f'] |
520 |
| - with tm.assert_raises_regex(ValueError, re.compile("'" + msg + '||' + msg2 + "'")): |
| 522 | + usecols = ['A', 'B', 'C', 'f'] |
| 523 | + with tm.assert_raises_regex(ValueError, msg): |
| 524 | + self.read_csv(StringIO(data), header=0, names=names, |
| 525 | + usecols=usecols) |
| 526 | + usecols = ['A', 'B', 'f'] |
| 527 | + with tm.assert_raises_regex(ValueError, msg): |
521 | 528 | self.read_csv(StringIO(data), names=names, usecols=usecols)
|
0 commit comments