Skip to content

Commit da01e38

Browse files
authored
TYP: read_csv's usecols (#55330)
* TYP: read_csv's usecols * adjust doc * use range
1 parent 6a83910 commit da01e38

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

pandas/_typing.py

+9
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,12 @@ def closed(self) -> bool:
509509

510510
# Offsets
511511
OffsetCalendar = Union[np.busdaycalendar, "AbstractHolidayCalendar"]
512+
513+
# read_csv: usecols
514+
UsecolsArgType = Union[
515+
SequenceNotStr[Hashable],
516+
range,
517+
AnyArrayLike,
518+
Callable[[HashableT], bool],
519+
None,
520+
]

pandas/io/parsers/readers.py

+12-42
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@
7777
DtypeArg,
7878
DtypeBackend,
7979
FilePath,
80-
HashableT,
8180
IndexLabel,
8281
ReadCsvBuffer,
8382
Self,
8483
StorageOptions,
84+
UsecolsArgType,
8585
)
8686
_doc_read_csv_and_table = (
8787
r"""
@@ -142,7 +142,7 @@
142142
Note: ``index_col=False`` can be used to force pandas to *not* use the first
143143
column as the index, e.g., when you have a malformed file with delimiters at
144144
the end of each line.
145-
usecols : list of Hashable or Callable, optional
145+
usecols : Sequence of Hashable or Callable, optional
146146
Subset of columns to select, denoted either by column labels or column indices.
147147
If list-like, all elements must either
148148
be positional (i.e. integer indices into the document columns) or strings
@@ -645,10 +645,7 @@ def read_csv(
645645
header: int | Sequence[int] | None | Literal["infer"] = ...,
646646
names: Sequence[Hashable] | None | lib.NoDefault = ...,
647647
index_col: IndexLabel | Literal[False] | None = ...,
648-
usecols: list[HashableT]
649-
| tuple[HashableT]
650-
| Callable[[Hashable], bool]
651-
| None = ...,
648+
usecols: UsecolsArgType = ...,
652649
dtype: DtypeArg | None = ...,
653650
engine: CSVEngine | None = ...,
654651
converters: Mapping[Hashable, Callable] | None = ...,
@@ -707,10 +704,7 @@ def read_csv(
707704
header: int | Sequence[int] | None | Literal["infer"] = ...,
708705
names: Sequence[Hashable] | None | lib.NoDefault = ...,
709706
index_col: IndexLabel | Literal[False] | None = ...,
710-
usecols: list[HashableT]
711-
| tuple[HashableT]
712-
| Callable[[Hashable], bool]
713-
| None = ...,
707+
usecols: UsecolsArgType = ...,
714708
dtype: DtypeArg | None = ...,
715709
engine: CSVEngine | None = ...,
716710
converters: Mapping[Hashable, Callable] | None = ...,
@@ -770,10 +764,7 @@ def read_csv(
770764
header: int | Sequence[int] | None | Literal["infer"] = ...,
771765
names: Sequence[Hashable] | None | lib.NoDefault = ...,
772766
index_col: IndexLabel | Literal[False] | None = ...,
773-
usecols: list[HashableT]
774-
| tuple[HashableT]
775-
| Callable[[Hashable], bool]
776-
| None = ...,
767+
usecols: UsecolsArgType = ...,
777768
dtype: DtypeArg | None = ...,
778769
engine: CSVEngine | None = ...,
779770
converters: Mapping[Hashable, Callable] | None = ...,
@@ -833,10 +824,7 @@ def read_csv(
833824
header: int | Sequence[int] | None | Literal["infer"] = ...,
834825
names: Sequence[Hashable] | None | lib.NoDefault = ...,
835826
index_col: IndexLabel | Literal[False] | None = ...,
836-
usecols: list[HashableT]
837-
| tuple[HashableT]
838-
| Callable[[Hashable], bool]
839-
| None = ...,
827+
usecols: UsecolsArgType = ...,
840828
dtype: DtypeArg | None = ...,
841829
engine: CSVEngine | None = ...,
842830
converters: Mapping[Hashable, Callable] | None = ...,
@@ -907,10 +895,7 @@ def read_csv(
907895
header: int | Sequence[int] | None | Literal["infer"] = "infer",
908896
names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default,
909897
index_col: IndexLabel | Literal[False] | None = None,
910-
usecols: list[HashableT]
911-
| tuple[HashableT]
912-
| Callable[[Hashable], bool]
913-
| None = None,
898+
usecols: UsecolsArgType = None,
914899
# General Parsing Configuration
915900
dtype: DtypeArg | None = None,
916901
engine: CSVEngine | None = None,
@@ -1005,10 +990,7 @@ def read_table(
1005990
header: int | Sequence[int] | None | Literal["infer"] = ...,
1006991
names: Sequence[Hashable] | None | lib.NoDefault = ...,
1007992
index_col: IndexLabel | Literal[False] | None = ...,
1008-
usecols: list[HashableT]
1009-
| tuple[HashableT]
1010-
| Callable[[Hashable], bool]
1011-
| None = ...,
993+
usecols: UsecolsArgType = ...,
1012994
dtype: DtypeArg | None = ...,
1013995
engine: CSVEngine | None = ...,
1014996
converters: Mapping[Hashable, Callable] | None = ...,
@@ -1065,10 +1047,7 @@ def read_table(
10651047
header: int | Sequence[int] | None | Literal["infer"] = ...,
10661048
names: Sequence[Hashable] | None | lib.NoDefault = ...,
10671049
index_col: IndexLabel | Literal[False] | None = ...,
1068-
usecols: list[HashableT]
1069-
| tuple[HashableT]
1070-
| Callable[[Hashable], bool]
1071-
| None = ...,
1050+
usecols: UsecolsArgType = ...,
10721051
dtype: DtypeArg | None = ...,
10731052
engine: CSVEngine | None = ...,
10741053
converters: Mapping[Hashable, Callable] | None = ...,
@@ -1125,10 +1104,7 @@ def read_table(
11251104
header: int | Sequence[int] | None | Literal["infer"] = ...,
11261105
names: Sequence[Hashable] | None | lib.NoDefault = ...,
11271106
index_col: IndexLabel | Literal[False] | None = ...,
1128-
usecols: list[HashableT]
1129-
| tuple[HashableT]
1130-
| Callable[[Hashable], bool]
1131-
| None = ...,
1107+
usecols: UsecolsArgType = ...,
11321108
dtype: DtypeArg | None = ...,
11331109
engine: CSVEngine | None = ...,
11341110
converters: Mapping[Hashable, Callable] | None = ...,
@@ -1185,10 +1161,7 @@ def read_table(
11851161
header: int | Sequence[int] | None | Literal["infer"] = ...,
11861162
names: Sequence[Hashable] | None | lib.NoDefault = ...,
11871163
index_col: IndexLabel | Literal[False] | None = ...,
1188-
usecols: list[HashableT]
1189-
| tuple[HashableT]
1190-
| Callable[[Hashable], bool]
1191-
| None = ...,
1164+
usecols: UsecolsArgType = ...,
11921165
dtype: DtypeArg | None = ...,
11931166
engine: CSVEngine | None = ...,
11941167
converters: Mapping[Hashable, Callable] | None = ...,
@@ -1258,10 +1231,7 @@ def read_table(
12581231
header: int | Sequence[int] | None | Literal["infer"] = "infer",
12591232
names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default,
12601233
index_col: IndexLabel | Literal[False] | None = None,
1261-
usecols: list[HashableT]
1262-
| tuple[HashableT]
1263-
| Callable[[Hashable], bool]
1264-
| None = None,
1234+
usecols: UsecolsArgType = None,
12651235
# General Parsing Configuration
12661236
dtype: DtypeArg | None = None,
12671237
engine: CSVEngine | None = None,

0 commit comments

Comments
 (0)