Skip to content

Commit 7971351

Browse files
committed
Added simple implementation of usecols order for read_csv
1 parent b94edd5 commit 7971351

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

pandas/io/parsers/readers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
defaultdict,
1212
)
1313
import csv
14+
from inspect import isfunction
1415
import sys
1516
from textwrap import fill
1617
from typing import (
@@ -1516,8 +1517,10 @@ def read(self, nrows: int | None = None) -> DataFrame:
15161517

15171518
if hasattr(self, "orig_options"):
15181519
dtype_arg = self.orig_options.get("dtype", None)
1520+
usecols = self.orig_options["usecols"]
15191521
else:
15201522
dtype_arg = None
1523+
usecols = None
15211524

15221525
if isinstance(dtype_arg, dict):
15231526
dtype = defaultdict(lambda: None) # type: ignore[var-annotated]
@@ -1530,6 +1533,18 @@ def read(self, nrows: int | None = None) -> DataFrame:
15301533
else:
15311534
dtype = None
15321535

1536+
if dtype is None:
1537+
if usecols is None or isfunction(usecols):
1538+
# Doesn't change anything if function or None gets passed
1539+
pass
1540+
elif len(usecols) == len(columns):
1541+
# uses size of number in usecols to determine corresponding columns
1542+
usecols_sorted = sorted(
1543+
range(len(usecols)), key=lambda i: usecols[i]
1544+
)
1545+
columns = [columns[i] for i in usecols_sorted]
1546+
col_dict = {k: col_dict[k] for k in columns}
1547+
15331548
if dtype is not None:
15341549
new_col_dict = {}
15351550
for k, v in col_dict.items():
@@ -1548,7 +1563,6 @@ def read(self, nrows: int | None = None) -> DataFrame:
15481563
index=index,
15491564
copy=False,
15501565
)
1551-
15521566
self._currow += new_rows
15531567
return df
15541568

0 commit comments

Comments
 (0)