1
1
from builtins import type as type_t
2
2
import datetime
3
- from io import (
4
- BufferedIOBase ,
5
- RawIOBase ,
6
- TextIOBase ,
7
- TextIOWrapper ,
8
- )
9
- from mmap import mmap
10
3
from os import PathLike
11
4
from typing import (
12
- IO ,
13
5
Any ,
14
- AnyStr ,
15
6
Callable ,
16
7
Hashable ,
17
8
Iterator ,
@@ -32,6 +23,7 @@ from pandas.core.generic import NDFrame
32
23
from pandas .core .groupby .grouper import Grouper
33
24
from pandas .core .indexes .base import Index
34
25
from pandas .core .series import Series
26
+ from typing_extensions import TypeAlias
35
27
36
28
from pandas ._libs .tslibs import (
37
29
Period ,
@@ -43,22 +35,28 @@ from pandas.core.dtypes.dtypes import ExtensionDtype
43
35
44
36
from pandas .io .formats .format import EngFormatter
45
37
46
- ArrayLike = Union [ExtensionArray , np .ndarray ]
47
- AnyArrayLike = Union [Index , Series , np .ndarray ]
48
- PythonScalar = Union [str , bool , complex ]
38
+ ArrayLike : TypeAlias = Union [ExtensionArray , np .ndarray ]
39
+ AnyArrayLike : TypeAlias = Union [Index , Series , np .ndarray ]
40
+ PythonScalar : TypeAlias = Union [str , bool , complex ]
49
41
DatetimeLikeScalar = TypeVar ("DatetimeLikeScalar" , Period , Timestamp , Timedelta )
50
- PandasScalar = Union [bytes , datetime .date , datetime .datetime , datetime .timedelta ]
51
- # Scalar = Union[PythonScalar, PandasScalar]
42
+ PandasScalar : TypeAlias = Union [
43
+ bytes , datetime .date , datetime .datetime , datetime .timedelta
44
+ ]
45
+ # Scalar: TypeAlias = Union[PythonScalar, PandasScalar]
52
46
53
- DatetimeLike = Union [datetime .date , datetime .datetime , np .datetime64 , Timestamp ]
47
+ DatetimeLike : TypeAlias = Union [
48
+ datetime .date , datetime .datetime , np .datetime64 , Timestamp
49
+ ]
54
50
55
51
# dtypes
56
- NpDtype = Union [str , np .dtype [np .generic ], type [Union [str , complex , bool , object ]]]
57
- Dtype = Union [ExtensionDtype , NpDtype ]
58
- AstypeArg = Union [ExtensionDtype , npt .DTypeLike ]
52
+ NpDtype : TypeAlias = Union [
53
+ str , np .dtype [np .generic ], type [Union [str , complex , bool , object ]]
54
+ ]
55
+ Dtype : TypeAlias = Union [ExtensionDtype , NpDtype ]
56
+ AstypeArg : TypeAlias = Union [ExtensionDtype , npt .DTypeLike ]
59
57
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
60
- DtypeArg = Union [Dtype , dict [Any , Dtype ]]
61
- DtypeObj = Union [np .dtype [np .generic ], ExtensionDtype ]
58
+ DtypeArg : TypeAlias = Union [Dtype , dict [Any , Dtype ]]
59
+ DtypeObj : TypeAlias = Union [np .dtype [np .generic ], ExtensionDtype ]
62
60
63
61
# filenames and file-like-objects
64
62
AnyStr_cov = TypeVar ("AnyStr_cov" , str , bytes , covariant = True )
@@ -91,47 +89,48 @@ class ReadCsvBuffer(ReadBuffer[AnyStr_cov], Protocol[AnyStr_cov]):
91
89
class WriteExcelBuffer (WriteBuffer [bytes ], Protocol ):
92
90
def truncate (self , size : int | None = ...) -> int : ...
93
91
94
- FilePath = Union [str , PathLike [str ]]
95
- FilePathOrBuffer = Union [
96
- FilePath , IO [AnyStr ], RawIOBase , BufferedIOBase , TextIOBase , TextIOWrapper , mmap
97
- ]
98
-
99
- Axis = Union [str , int ]
100
- IndexLabel = Union [Hashable , Sequence [Hashable ]]
101
- Label = Optional [Hashable ]
102
- Level = Union [Hashable , int ]
103
- Suffixes = tuple [Optional [str ], Optional [str ]]
104
- Ordered = Optional [bool ]
105
- JSONSerializable = Union [PythonScalar , list , dict ]
106
- Axes = Union [AnyArrayLike , list , dict , range ]
107
- Renamer = Union [Mapping [Any , Label ], Callable [[Any ], Label ]]
92
+ FilePath : TypeAlias = Union [str , PathLike [str ]]
93
+
94
+ Axis : TypeAlias = Union [str , int ]
95
+ IndexLabel : TypeAlias = Union [Hashable , Sequence [Hashable ]]
96
+ Label : TypeAlias = Optional [Hashable ]
97
+ Level : TypeAlias = Union [Hashable , int ]
98
+ Suffixes : TypeAlias = tuple [Optional [str ], Optional [str ]]
99
+ Ordered : TypeAlias = Optional [bool ]
100
+ JSONSerializable : TypeAlias = Union [PythonScalar , list , dict ]
101
+ Axes : TypeAlias = Union [AnyArrayLike , list , dict , range ]
102
+ Renamer : TypeAlias = Union [Mapping [Any , Label ], Callable [[Any ], Label ]]
108
103
T = TypeVar ("T" )
109
- FuncType = Callable [..., Any ]
104
+ FuncType : TypeAlias = Callable [..., Any ]
110
105
F = TypeVar ("F" , bound = FuncType )
111
106
HashableT = TypeVar ("HashableT" , bound = Hashable )
112
107
113
- AggFuncTypeBase = Union [Callable , str , np .ufunc ]
114
- AggFuncTypeDictSeries = dict [Hashable , AggFuncTypeBase ]
115
- AggFuncTypeDictFrame = dict [Hashable , Union [AggFuncTypeBase , list [AggFuncTypeBase ]]]
116
- AggFuncTypeSeriesToFrame = Union [
108
+ AggFuncTypeBase : TypeAlias = Union [Callable , str , np .ufunc ]
109
+ AggFuncTypeDictSeries : TypeAlias = dict [Hashable , AggFuncTypeBase ]
110
+ AggFuncTypeDictFrame : TypeAlias = dict [
111
+ Hashable , Union [AggFuncTypeBase , list [AggFuncTypeBase ]]
112
+ ]
113
+ AggFuncTypeSeriesToFrame : TypeAlias = Union [
117
114
list [AggFuncTypeBase ],
118
115
AggFuncTypeDictSeries ,
119
116
]
120
- AggFuncTypeFrame = Union [
117
+ AggFuncTypeFrame : TypeAlias = Union [
121
118
AggFuncTypeBase ,
122
119
list [AggFuncTypeBase ],
123
120
AggFuncTypeDictFrame ,
124
121
]
125
122
126
- num = complex
127
- SeriesAxisType = Literal ["index" , 0 ] # Restricted subset of _AxisType for series
128
- AxisType = Literal ["columns" , "index" , 0 , 1 ]
123
+ num : TypeAlias = complex
124
+ SeriesAxisType : TypeAlias = Literal [
125
+ "index" , 0
126
+ ] # Restricted subset of _AxisType for series
127
+ AxisType : TypeAlias = Literal ["columns" , "index" , 0 , 1 ]
129
128
DtypeNp = TypeVar ("DtypeNp" , bound = np .dtype [np .generic ])
130
- KeysArgType = Any
129
+ KeysArgType : TypeAlias = Any
131
130
ListLike = TypeVar ("ListLike" , Sequence , np .ndarray , "Series" , "Index" )
132
- ListLikeU = Union [Sequence , np .ndarray , Series , Index ]
133
- StrLike = Union [str , np .str_ ]
134
- Scalar = Union [
131
+ ListLikeU : TypeAlias = Union [Sequence , np .ndarray , Series , Index ]
132
+ StrLike : TypeAlias = Union [str , np .str_ ]
133
+ Scalar : TypeAlias = Union [
135
134
str ,
136
135
bytes ,
137
136
datetime .date ,
@@ -146,14 +145,14 @@ Scalar = Union[
146
145
]
147
146
ScalarT = TypeVar ("ScalarT" , bound = Scalar )
148
147
# Refine the definitions below in 3.9 to use the specialized type.
149
- np_ndarray_int64 = npt .NDArray [np .int64 ]
150
- np_ndarray_int = npt .NDArray [np .signedinteger ]
151
- np_ndarray_anyint = npt .NDArray [np .integer ]
152
- np_ndarray_bool = npt .NDArray [np .bool_ ]
153
- np_ndarray_str = npt .NDArray [np .str_ ]
154
-
155
- IndexType = Union [slice , np_ndarray_int64 , Index , list [int ], Series [int ]]
156
- MaskType = Union [Series [bool ], np_ndarray_bool , list [bool ]]
148
+ np_ndarray_int64 : TypeAlias = npt .NDArray [np .int64 ]
149
+ np_ndarray_int : TypeAlias = npt .NDArray [np .signedinteger ]
150
+ np_ndarray_anyint : TypeAlias = npt .NDArray [np .integer ]
151
+ np_ndarray_bool : TypeAlias = npt .NDArray [np .bool_ ]
152
+ np_ndarray_str : TypeAlias = npt .NDArray [np .str_ ]
153
+
154
+ IndexType : TypeAlias = Union [slice , np_ndarray_int64 , Index , list [int ], Series [int ]]
155
+ MaskType : TypeAlias = Union [Series [bool ], np_ndarray_bool , list [bool ]]
157
156
# Scratch types for generics
158
157
S1 = TypeVar (
159
158
"S1" ,
@@ -177,13 +176,13 @@ T1 = TypeVar(
177
176
)
178
177
T2 = TypeVar ("T2" , str , int )
179
178
180
- IndexingInt = Union [
179
+ IndexingInt : TypeAlias = Union [
181
180
int , np .int_ , np .integer , np .unsignedinteger , np .signedinteger , np .int8
182
181
]
183
- TimestampConvertibleTypes = Union [
182
+ TimestampConvertibleTypes : TypeAlias = Union [
184
183
Timestamp , datetime .datetime , np .datetime64 , np .int64 , float , str
185
184
]
186
- TimedeltaConvertibleTypes = Union [
185
+ TimedeltaConvertibleTypes : TypeAlias = Union [
187
186
Timedelta , datetime .timedelta , np .timedelta64 , np .int64 , float , str
188
187
]
189
188
# NDFrameT is stricter and ensures that the same subclass of NDFrame always is
@@ -196,39 +195,39 @@ IndexT = TypeVar("IndexT", bound=Index)
196
195
197
196
# Interval closed type
198
197
199
- IntervalClosedType = Literal ["left" , "right" , "both" , "neither" ]
198
+ IntervalClosedType : TypeAlias = Literal ["left" , "right" , "both" , "neither" ]
200
199
201
- DateTimeErrorChoices = Literal ["ignore" , "raise" , "coerce" ]
200
+ DateTimeErrorChoices : TypeAlias = Literal ["ignore" , "raise" , "coerce" ]
202
201
203
202
# Shared by functions such as drop and astype
204
- IgnoreRaise = Literal ["ignore" , "raise" ]
203
+ IgnoreRaise : TypeAlias = Literal ["ignore" , "raise" ]
205
204
206
205
# for arbitrary kwargs passed during reading/writing files
207
- StorageOptions = Optional [dict [str , Any ]]
206
+ StorageOptions : TypeAlias = Optional [dict [str , Any ]]
208
207
209
208
# compression keywords and compression
210
- CompressionDict = dict [str , Any ]
211
- CompressionOptions = Optional [
209
+ CompressionDict : TypeAlias = dict [str , Any ]
210
+ CompressionOptions : TypeAlias = Optional [
212
211
Union [Literal ["infer" , "gzip" , "bz2" , "zip" , "xz" , "zstd" ], CompressionDict ]
213
212
]
214
- FormattersType = Union [
213
+ FormattersType : TypeAlias = Union [
215
214
list [Callable ], tuple [Callable , ...], Mapping [Union [str , int ], Callable ]
216
215
]
217
- FloatFormatType = str | Callable | EngFormatter
216
+ FloatFormatType : TypeAlias = str | Callable | EngFormatter
218
217
# converters
219
- ConvertersArg = dict [Hashable , Callable [[Dtype ], Dtype ]]
218
+ ConvertersArg : TypeAlias = dict [Hashable , Callable [[Dtype ], Dtype ]]
220
219
221
220
# parse_dates
222
- ParseDatesArg = Union [
221
+ ParseDatesArg : TypeAlias = Union [
223
222
bool , list [Hashable ], list [list [Hashable ]], dict [Hashable , list [Hashable ]]
224
223
]
225
224
226
225
# read_xml parsers
227
- XMLParsers = Literal ["lxml" , "etree" ]
226
+ XMLParsers : TypeAlias = Literal ["lxml" , "etree" ]
228
227
229
228
# Any plain Python or numpy function
230
- Function = Union [np .ufunc , Callable [..., Any ]]
231
- GroupByObjectNonScalar = Union [
229
+ Function : TypeAlias = Union [np .ufunc , Callable [..., Any ]]
230
+ GroupByObjectNonScalar : TypeAlias = Union [
232
231
tuple ,
233
232
list [HashableT ],
234
233
Function ,
@@ -244,9 +243,9 @@ GroupByObjectNonScalar = Union[
244
243
Grouper ,
245
244
list [Grouper ],
246
245
]
247
- GroupByObject = Union [Scalar , GroupByObjectNonScalar ]
246
+ GroupByObject : TypeAlias = Union [Scalar , GroupByObjectNonScalar ]
248
247
249
- StataDateFormat = Literal [
248
+ StataDateFormat : TypeAlias = Literal [
250
249
"tc" ,
251
250
"%tc" ,
252
251
"td" ,
@@ -263,37 +262,43 @@ StataDateFormat = Literal[
263
262
"%ty" ,
264
263
]
265
264
266
- FillnaOptions = Literal ["backfill" , "bfill" , "ffill" , "pad" ]
267
- ReplaceMethod = Literal ["pad" , "ffill" , "bfill" ]
268
- SortKind = Literal ["quicksort" , "mergesort" , "heapsort" , "stable" ]
269
- NaPosition = Literal ["first" , "last" ]
270
- MergeHow = Literal ["left" , "right" , "outer" , "inner" ]
271
- JsonFrameOrient = Literal ["split" , "records" , "index" , "columns" , "values" , "table" ]
272
- JsonSeriesOrient = Literal ["split" , "records" , "index" ]
265
+ FillnaOptions : TypeAlias = Literal ["backfill" , "bfill" , "ffill" , "pad" ]
266
+ ReplaceMethod : TypeAlias = Literal ["pad" , "ffill" , "bfill" ]
267
+ SortKind : TypeAlias = Literal ["quicksort" , "mergesort" , "heapsort" , "stable" ]
268
+ NaPosition : TypeAlias = Literal ["first" , "last" ]
269
+ MergeHow : TypeAlias = Literal ["left" , "right" , "outer" , "inner" ]
270
+ JsonFrameOrient : TypeAlias = Literal [
271
+ "split" , "records" , "index" , "columns" , "values" , "table"
272
+ ]
273
+ JsonSeriesOrient : TypeAlias = Literal ["split" , "records" , "index" ]
273
274
274
- TimestampConvention = Literal ["start" , "end" , "s" , "e" ]
275
+ TimestampConvention : TypeAlias = Literal ["start" , "end" , "s" , "e" ]
275
276
276
- CSVEngine = Literal ["c" , "python" , "pyarrow" , "python-fwf" ]
277
- CSVQuoting = Literal [0 , 1 , 2 , 3 ]
277
+ CSVEngine : TypeAlias = Literal ["c" , "python" , "pyarrow" , "python-fwf" ]
278
+ CSVQuoting : TypeAlias = Literal [0 , 1 , 2 , 3 ]
278
279
279
- HDFCompLib = Literal ["zlib" , "lzo" , "bzip2" , "blosc" ]
280
- ParquetEngine = Literal ["auto" , "pyarrow" , "fastparquet" ]
281
- FileWriteMode = Literal [
280
+ HDFCompLib : TypeAlias = Literal ["zlib" , "lzo" , "bzip2" , "blosc" ]
281
+ ParquetEngine : TypeAlias = Literal ["auto" , "pyarrow" , "fastparquet" ]
282
+ FileWriteMode : TypeAlias = Literal [
282
283
"a" , "w" , "x" , "at" , "wt" , "xt" , "ab" , "wb" , "xb" , "w+" , "w+b" , "a+" , "a+b"
283
284
]
284
- ColspaceArgType = str | int | Sequence [int | str ] | Mapping [Hashable , str | int ]
285
+ ColspaceArgType : TypeAlias = (
286
+ str | int | Sequence [int | str ] | Mapping [Hashable , str | int ]
287
+ )
285
288
286
289
# Windowing rank methods
287
- WindowingRankType = Literal ["average" , "min" , "max" ]
288
- WindowingEngine = Union [Literal ["cython" , "numba" ], None ]
290
+ WindowingRankType : TypeAlias = Literal ["average" , "min" , "max" ]
291
+ WindowingEngine : TypeAlias = Union [Literal ["cython" , "numba" ], None ]
289
292
290
293
class _WindowingNumbaKwargs (TypedDict , total = False ):
291
294
nopython : bool
292
295
nogil : bool
293
296
parallel : bool
294
297
295
- WindowingEngineKwargs = Union [_WindowingNumbaKwargs , None ]
296
- QuantileInterpolation = Literal ["linear" , "lower" , "higher" , "midpoint" , "nearest" ]
298
+ WindowingEngineKwargs : TypeAlias = Union [_WindowingNumbaKwargs , None ]
299
+ QuantileInterpolation : TypeAlias = Literal [
300
+ "linear" , "lower" , "higher" , "midpoint" , "nearest"
301
+ ]
297
302
298
303
class StyleExportDict (TypedDict , total = False ):
299
304
apply : Any
@@ -305,6 +310,6 @@ class StyleExportDict(TypedDict, total=False):
305
310
hide_column_names : bool
306
311
css : dict [str , str | int ]
307
312
308
- CalculationMethod = Literal ["single" , "table" ]
313
+ CalculationMethod : TypeAlias = Literal ["single" , "table" ]
309
314
310
315
__all__ = ["npt" , "type_t" ]
0 commit comments