|
8 | 8 | import numpy as np
|
9 | 9 | import pytest
|
10 | 10 |
|
| 11 | +from pandas._config.config import option_context |
| 12 | + |
11 | 13 | from pandas.errors import ParserError
|
12 | 14 |
|
13 | 15 | from pandas import (
|
@@ -547,25 +549,34 @@ def test_usecols_dtype(all_parsers):
|
547 | 549 | tm.assert_frame_equal(result, expected)
|
548 | 550 |
|
549 | 551 |
|
550 |
| -@pytest.mark.parametrize("usecols", [(2, 0), ("c", "a")]) |
551 |
| -def test_usecols_order(all_parsers, usecols, request): |
552 |
| - # TODO add future flag |
| 552 | +@pytest.mark.parametrize("usecols", [(3, 0, 2), ("d", "a", "c")]) |
| 553 | +@pytest.mark.parametrize("usecols_use_order", (True, False)) |
| 554 | +def test_usecols_order(all_parsers, usecols, usecols_use_order): |
| 555 | + # TODOE add portion in doc for 3.0 transition |
553 | 556 | parser = all_parsers
|
554 | 557 | data = """\
|
555 | 558 | a,b,c,d
|
556 | 559 | 1,2,3,0
|
557 | 560 | 4,5,6,0
|
558 | 561 | 7,8,9,0
|
559 | 562 | 10,11,12,13"""
|
560 |
| - # print(usecols) |
561 |
| - # print(data) |
562 | 563 |
|
| 564 | + msg = "The pyarrow engine does not allow 'usecols' to be integer column positions" |
563 | 565 | if parser.engine == "pyarrow" and isinstance(usecols[0], int):
|
564 |
| - with pytest.raises(ValueError, match=_msg_pyarrow_requires_names): |
| 566 | + with pytest.raises(ValueError, match=msg): |
565 | 567 | parser.read_csv(StringIO(data), usecols=usecols)
|
566 | 568 | return
|
567 | 569 |
|
568 | 570 | result = parser.read_csv(StringIO(data), usecols=usecols)
|
569 | 571 |
|
570 |
| - expected = DataFrame([[3, 1], [6, 4], [9, 7], [12, 10]], columns=["c", "a"]) |
571 |
| - tm.assert_frame_equal(result, expected) |
| 572 | + if usecols_use_order: |
| 573 | + expected = DataFrame( |
| 574 | + {"d": [0, 0, 0, 13], "a": [1, 4, 7, 10], "c": [3, 6, 9, 12]} |
| 575 | + ) |
| 576 | + else: |
| 577 | + expected = DataFrame( |
| 578 | + {"a": [1, 4, 7, 10], "c": [3, 6, 9, 12], "d": [0, 0, 0, 13]} |
| 579 | + ) |
| 580 | + |
| 581 | + with option_context("future.usecols_use_order", usecols_use_order): |
| 582 | + tm.assert_frame_equal(result, expected) |
0 commit comments