Skip to content

Commit 8c0d2d4

Browse files
committed
Added check for future flag, improved testcase to check flag and more complicated usecols order
1 parent e394592 commit 8c0d2d4

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

pandas/io/parsers/readers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import numpy as np
2929

30+
from pandas._config import get_option
31+
3032
from pandas._libs import lib
3133
from pandas._libs.parsers import STR_NA_VALUES
3234
from pandas.errors import (
@@ -1533,7 +1535,7 @@ def read(self, nrows: int | None = None) -> DataFrame:
15331535
else:
15341536
dtype = None
15351537

1536-
if dtype is None:
1538+
if dtype is None and get_option("future.usecols_use_order"):
15371539
if usecols is None or isfunction(usecols):
15381540
# Doesn't change anything if function or None gets passed
15391541
pass

pandas/tests/io/parser/usecols/test_usecols_basic.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import numpy as np
99
import pytest
1010

11+
from pandas._config.config import option_context
12+
1113
from pandas.errors import ParserError
1214

1315
from pandas import (
@@ -547,25 +549,34 @@ def test_usecols_dtype(all_parsers):
547549
tm.assert_frame_equal(result, expected)
548550

549551

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
553556
parser = all_parsers
554557
data = """\
555558
a,b,c,d
556559
1,2,3,0
557560
4,5,6,0
558561
7,8,9,0
559562
10,11,12,13"""
560-
# print(usecols)
561-
# print(data)
562563

564+
msg = "The pyarrow engine does not allow 'usecols' to be integer column positions"
563565
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):
565567
parser.read_csv(StringIO(data), usecols=usecols)
566568
return
567569

568570
result = parser.read_csv(StringIO(data), usecols=usecols)
569571

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

Comments
 (0)