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 1111 defaultdict ,
1212)
1313import csv
14+ from inspect import isfunction
1415import sys
1516from textwrap import fill
1617from 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
You can’t perform that action at this time.
0 commit comments