File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change 11
11
defaultdict ,
12
12
)
13
13
import csv
14
+ from inspect import isfunction
14
15
import sys
15
16
from textwrap import fill
16
17
from typing import (
@@ -1516,8 +1517,10 @@ def read(self, nrows: int | None = None) -> DataFrame:
1516
1517
1517
1518
if hasattr (self , "orig_options" ):
1518
1519
dtype_arg = self .orig_options .get ("dtype" , None )
1520
+ usecols = self .orig_options ["usecols" ]
1519
1521
else :
1520
1522
dtype_arg = None
1523
+ usecols = None
1521
1524
1522
1525
if isinstance (dtype_arg , dict ):
1523
1526
dtype = defaultdict (lambda : None ) # type: ignore[var-annotated]
@@ -1530,6 +1533,18 @@ def read(self, nrows: int | None = None) -> DataFrame:
1530
1533
else :
1531
1534
dtype = None
1532
1535
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
+
1533
1548
if dtype is not None :
1534
1549
new_col_dict = {}
1535
1550
for k , v in col_dict .items ():
@@ -1548,7 +1563,6 @@ def read(self, nrows: int | None = None) -> DataFrame:
1548
1563
index = index ,
1549
1564
copy = False ,
1550
1565
)
1551
-
1552
1566
self ._currow += new_rows
1553
1567
return df
1554
1568
You can’t perform that action at this time.
0 commit comments