Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pandas/_typing.py: 87%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3from builtins import type as type_t # pyright: ignore[reportUnusedImport]
4from collections.abc import (
5 Callable,
6 Hashable,
7 Iterator,
8 Mapping,
9 MutableMapping,
10 Sequence,
11)
12from datetime import (
13 date,
14 datetime,
15 timedelta,
16 tzinfo,
17)
18from os import PathLike
19from types import EllipsisType
20from typing import (
21 TYPE_CHECKING,
22 Any,
23 Literal,
24 ParamSpec,
25 Protocol,
26 SupportsIndex,
27 TypeAlias,
28 TypeVar,
29 Union,
30 overload,
31)
33import numpy as np
34import numpy.typing as npt
36# To prevent import cycles place any internal imports in the branch below
37# and use a string literal forward reference to it in subsequent types
38# https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles
40# Note that Union is needed when a Union includes a pandas type
42if TYPE_CHECKING:
43 from pandas._libs import (
44 NaTType,
45 Period,
46 Timedelta,
47 Timestamp,
48 )
49 from pandas._libs.tslibs import BaseOffset
51 from pandas.core.dtypes.dtypes import ExtensionDtype
53 from pandas import (
54 DatetimeIndex,
55 Interval,
56 PeriodIndex,
57 TimedeltaIndex,
58 )
59 from pandas.arrays import (
60 DatetimeArray,
61 TimedeltaArray,
62 )
63 from pandas.core.arrays.base import ExtensionArray
64 from pandas.core.frame import DataFrame
65 from pandas.core.generic import NDFrame
66 from pandas.core.groupby.generic import (
67 DataFrameGroupBy,
68 GroupBy,
69 SeriesGroupBy,
70 )
71 from pandas.core.indexes.base import Index
72 from pandas.core.internals import (
73 BlockManager,
74 SingleBlockManager,
75 )
76 from pandas.core.resample import Resampler
77 from pandas.core.series import Series
78 from pandas.core.window.rolling import BaseWindow
80 from pandas.io.formats.format import EngFormatter
81 from pandas.tseries.holiday import AbstractHolidayCalendar
83 ScalarLike_co: TypeAlias = int | float | complex | str | bytes | np.generic
85 # numpy compatible types
86 NumpyValueArrayLike: TypeAlias = ScalarLike_co | npt.ArrayLike
87 NumpySorter: TypeAlias = npt.NDArray[np.integer] | None
90P = ParamSpec("P")
92HashableT = TypeVar("HashableT", bound=Hashable)
93HashableT2 = TypeVar("HashableT2", bound=Hashable)
94MutableMappingT = TypeVar("MutableMappingT", bound=MutableMapping)
96# array-like
98ArrayLike: TypeAlias = Union["ExtensionArray", np.ndarray]
99ArrayLikeT = TypeVar("ArrayLikeT", "ExtensionArray", np.ndarray)
100AnyArrayLike: TypeAlias = Union[ArrayLike, "Index", "Series"]
101TimeArrayLike: TypeAlias = Union["DatetimeArray", "TimedeltaArray"]
103# list-like
105# from https://github.com/hauntsaninja/useful_types
106# includes Sequence-like objects but excludes str and bytes
107_T_co = TypeVar("_T_co", covariant=True)
110class SequenceNotStr(Protocol[_T_co]):
111 __module__: str = "pandas.api.typing.aliases"
113 @overload
114 def __getitem__(self, index: SupportsIndex, /) -> _T_co: ...
116 @overload
117 def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ...
119 def __contains__(self, value: object, /) -> bool: ...
121 def __len__(self) -> int: ...
123 def __iter__(self) -> Iterator[_T_co]: ...
125 def index(self, value: Any, start: int = ..., stop: int = ..., /) -> int: ...
127 def count(self, value: Any, /) -> int: ...
129 def __reversed__(self) -> Iterator[_T_co]: ...
132ListLike: TypeAlias = AnyArrayLike | SequenceNotStr | range
134# scalars
136PythonScalar: TypeAlias = str | float | bool
137DatetimeLikeScalar: TypeAlias = Union["Period", "Timestamp", "Timedelta"]
139# aligned with pandas-stubs - typical scalars found in Series. Explicitly leaves
140# out object
141_IndexIterScalar: TypeAlias = Union[
142 str,
143 bytes,
144 date,
145 datetime,
146 timedelta,
147 np.datetime64,
148 np.timedelta64,
149 bool,
150 int,
151 float,
152 "Timestamp",
153 "Timedelta",
154]
155Scalar: TypeAlias = Union[
156 _IndexIterScalar, "Interval", complex, np.integer, np.floating, np.complexfloating
157]
159IntStrT = TypeVar("IntStrT", bound=int | str)
161# timestamp and timedelta convertible types
163TimestampConvertibleTypes: TypeAlias = Union[
164 "Timestamp", date, np.datetime64, np.int64, float, str
165]
166TimestampNonexistent: TypeAlias = (
167 Literal["shift_forward", "shift_backward", "NaT", "raise"] | timedelta
168)
170TimedeltaConvertibleTypes: TypeAlias = Union[
171 "Timedelta", timedelta, np.timedelta64, np.int64, float, str
172]
173Timezone: TypeAlias = str | tzinfo
175ToTimestampHow: TypeAlias = Literal["s", "e", "start", "end"]
177# NDFrameT is stricter and ensures that the same subclass of NDFrame always is
178# used. E.g. `def func(a: NDFrameT) -> NDFrameT: ...` means that if a
179# Series is passed into a function, a Series is always returned and if a DataFrame is
180# passed in, a DataFrame is always returned.
181NDFrameT = TypeVar("NDFrameT", bound="NDFrame")
183IndexT = TypeVar("IndexT", bound="Index")
184FreqIndexT = TypeVar("FreqIndexT", "DatetimeIndex", "PeriodIndex", "TimedeltaIndex")
185NumpyIndexT = TypeVar("NumpyIndexT", np.ndarray, "Index")
187AxisInt: TypeAlias = int
188Axis: TypeAlias = AxisInt | Literal["index", "columns", "rows"]
189IndexLabel: TypeAlias = Hashable | Sequence[Hashable]
190Level: TypeAlias = Hashable
191Shape: TypeAlias = tuple[int, ...]
192Suffixes: TypeAlias = Sequence[str | None]
193Ordered: TypeAlias = bool | None
194JSONSerializable: TypeAlias = PythonScalar | list | dict | None
195Frequency: TypeAlias = Union[str, "BaseOffset"]
196Axes: TypeAlias = ListLike
198RandomState: TypeAlias = (
199 int
200 | np.ndarray
201 | np.random.Generator
202 | np.random.BitGenerator
203 | np.random.RandomState
204)
207# dtypes
208NpDtype: TypeAlias = str | np.dtype | type[str | complex | bool | object]
209Dtype: TypeAlias = Union["ExtensionDtype", NpDtype]
210AstypeArg: TypeAlias = Union["ExtensionDtype", npt.DTypeLike]
211# DtypeArg specifies all allowable dtypes in a functions its dtype argument
212DtypeArg: TypeAlias = Dtype | Mapping[Hashable, Dtype]
213DtypeObj: TypeAlias = Union[np.dtype, "ExtensionDtype"]
215# converters
216ConvertersArg: TypeAlias = dict[Hashable, Callable[[Dtype], Dtype]]
218# parse_dates
219ParseDatesArg: TypeAlias = (
220 bool | list[Hashable] | list[list[Hashable]] | dict[Hashable, list[Hashable]]
221)
223# For functions like rename that convert one label to another
224Renamer: TypeAlias = Mapping[Any, Hashable] | Callable[[Any], Hashable]
226# to maintain type information across generic functions and parametrization
227T = TypeVar("T")
229# used in decorators to preserve the signature of the function it decorates
230# see https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
231FuncType: TypeAlias = Callable[..., Any]
232F = TypeVar("F", bound=FuncType)
233TypeT = TypeVar("TypeT", bound=type)
235# types of vectorized key functions for DataFrame::sort_values and
236# DataFrame::sort_index, among others
237ValueKeyFunc: TypeAlias = Callable[["Series"], Union["Series", AnyArrayLike]] | None
238IndexKeyFunc: TypeAlias = Callable[["Index"], Union["Index", AnyArrayLike]] | None
240# types of `func` kwarg for DataFrame.aggregate and Series.aggregate
241AggFuncTypeBase: TypeAlias = Callable | str
242AggFuncTypeDict: TypeAlias = MutableMapping[
243 Hashable, AggFuncTypeBase | list[AggFuncTypeBase]
244]
245AggFuncType: TypeAlias = AggFuncTypeBase | list[AggFuncTypeBase] | AggFuncTypeDict
246AggObjType: TypeAlias = Union[
247 "Series",
248 "DataFrame",
249 "GroupBy",
250 "SeriesGroupBy",
251 "DataFrameGroupBy",
252 "BaseWindow",
253 "Resampler",
254]
256PythonFuncType: TypeAlias = Callable[[Any], Any]
258# filenames and file-like-objects
259AnyStr_co = TypeVar("AnyStr_co", str, bytes, covariant=True)
260AnyStr_contra = TypeVar("AnyStr_contra", str, bytes, contravariant=True)
263class BaseBuffer(Protocol):
264 def seek(self, offset: int, whence: int = ..., /) -> int:
265 # with one argument: gzip.GzipFile, bz2.BZ2File
266 # with two arguments: zip.ZipFile, read_sas
267 ...
269 def seekable(self) -> bool:
270 # for bz2.BZ2File
271 ...
273 def tell(self) -> int:
274 # for zip.ZipFile, read_stata, to_stata
275 ...
278class ReadBuffer(BaseBuffer, Protocol[AnyStr_co]):
279 __module__: str = "pandas.api.typing.aliases"
281 def read(self, n: int = ..., /) -> AnyStr_co:
282 # for BytesIOWrapper, gzip.GzipFile, bz2.BZ2File
283 ...
286class WriteBuffer(BaseBuffer, Protocol[AnyStr_contra]):
287 __module__: str = "pandas.api.typing.aliases"
289 def write(self, b: AnyStr_contra, /) -> Any:
290 # for gzip.GzipFile, bz2.BZ2File
291 ...
293 def flush(self) -> Any:
294 # for gzip.GzipFile, bz2.BZ2File
295 ...
298class ReadPickleBuffer(ReadBuffer[bytes], Protocol):
299 __module__: str = "pandas.api.typing.aliases"
301 def readline(self) -> bytes: ...
304class WriteExcelBuffer(WriteBuffer[bytes], Protocol):
305 __module__: str = "pandas.api.typing.aliases"
307 def truncate(self, size: int | None = ..., /) -> int: ...
310class ReadCsvBuffer(ReadBuffer[AnyStr_co], Protocol):
311 __module__: str = "pandas.api.typing.aliases"
313 def __iter__(self) -> Iterator[AnyStr_co]:
314 # for engine=python
315 ...
317 def fileno(self) -> int:
318 # for _MMapWrapper
319 ...
321 def readline(self) -> AnyStr_co:
322 # for engine=python
323 ...
325 @property
326 def closed(self) -> bool:
327 # for engine=pyarrow
328 ...
331FilePath: TypeAlias = str | PathLike[str]
333# for arbitrary kwargs passed during reading/writing files
334StorageOptions: TypeAlias = dict[str, Any] | None
336# compression keywords and compression
337CompressionDict: TypeAlias = dict[str, Any]
338CompressionOptions: TypeAlias = (
339 Literal["infer", "gzip", "bz2", "zip", "xz", "zstd", "tar"] | CompressionDict | None
340)
341ParquetCompressionOptions: TypeAlias = (
342 Literal["snappy", "gzip", "brotli", "lz4", "zstd"] | None
343)
345# types in DataFrameFormatter
346FormattersType: TypeAlias = (
347 list[Callable] | tuple[Callable, ...] | Mapping[str | int, Callable]
348)
349ColspaceType: TypeAlias = Mapping[Hashable, str | int]
350FloatFormatType: TypeAlias = Union[str, Callable, "EngFormatter"]
351ColspaceArgType: TypeAlias = (
352 str | int | Sequence[str | int] | Mapping[Hashable, str | int]
353)
355# Arguments for fillna()
356FillnaOptions: TypeAlias = Literal["backfill", "bfill", "ffill", "pad"]
357InterpolateOptions: TypeAlias = Literal[
358 "linear",
359 "time",
360 "index",
361 "values",
362 "nearest",
363 "zero",
364 "slinear",
365 "quadratic",
366 "cubic",
367 "barycentric",
368 "polynomial",
369 "krogh",
370 "piecewise_polynomial",
371 "spline",
372 "pchip",
373 "akima",
374 "cubicspline",
375 "from_derivatives",
376]
378# internals
379Manager: TypeAlias = Union["BlockManager", "SingleBlockManager"]
381# indexing
382# PositionalIndexer -> valid 1D positional indexer, e.g. can pass
383# to ndarray.__getitem__
384# ScalarIndexer is for a single value as the index
385# SequenceIndexer is for list like or slices (but not tuples)
386# PositionalIndexerTuple is extends the PositionalIndexer for 2D arrays
387# These are used in various __getitem__ overloads
388# Using List[int] here rather than Sequence[int] to disallow tuples.
389ScalarIndexer: TypeAlias = int | np.integer
390SequenceIndexer: TypeAlias = slice | EllipsisType | list[int] | np.ndarray
391PositionalIndexer: TypeAlias = ScalarIndexer | SequenceIndexer
392PositionalIndexerTuple: TypeAlias = tuple[PositionalIndexer, PositionalIndexer]
393PositionalIndexer2D: TypeAlias = PositionalIndexer | PositionalIndexerTuple
394TakeIndexer: TypeAlias = Sequence[int] | Sequence[np.integer] | npt.NDArray[np.integer]
396# Shared by functions such as drop and astype
397IgnoreRaise: TypeAlias = Literal["ignore", "raise"]
399# Windowing rank methods
400WindowingRankType: TypeAlias = Literal["average", "min", "max"]
402# rank tie-breaking methods (Series.rank / DataFrame.rank / GroupBy.rank)
403RankMethod: TypeAlias = Literal["average", "min", "max", "first", "dense"]
404RankNaOption: TypeAlias = Literal["keep", "top", "bottom"]
406# read_csv engines
407CSVEngine: TypeAlias = Literal["c", "python", "pyarrow", "python-fwf"]
409# read_json engines
410JSONEngine: TypeAlias = Literal["ujson", "pyarrow"]
412# read_xml parsers
413XMLParsers: TypeAlias = Literal["lxml", "etree"]
415# read_html flavors
416HTMLFlavors: TypeAlias = Literal["lxml", "html5lib", "bs4"]
418# Interval closed type
419IntervalLeftRight: TypeAlias = Literal["left", "right"]
420IntervalClosedType: TypeAlias = IntervalLeftRight | Literal["both", "neither"]
422# datetime and NaTType
423DatetimeNaTType: TypeAlias = Union[datetime, "NaTType"]
424DateTimeErrorChoices: TypeAlias = Literal["raise", "coerce"]
426# sort_index
427SortKind: TypeAlias = Literal["quicksort", "mergesort", "heapsort", "stable"]
428NaPosition: TypeAlias = Literal["first", "last"]
430# Arguments for nsmallest and nlargest
431NsmallestNlargestKeep: TypeAlias = Literal["first", "last", "all"]
433# quantile interpolation
434QuantileInterpolation: TypeAlias = Literal[
435 "linear", "lower", "higher", "midpoint", "nearest"
436]
438# plotting
439PlottingOrientation: TypeAlias = Literal["horizontal", "vertical"]
441# dropna
442AnyAll: TypeAlias = Literal["any", "all"]
444# merge
445MergeHow: TypeAlias = Literal[
446 "left", "right", "inner", "outer", "cross", "left_anti", "right_anti"
447]
448MergeValidate: TypeAlias = Literal[
449 "one_to_one",
450 "1:1",
451 "one_to_many",
452 "1:m",
453 "many_to_one",
454 "m:1",
455 "many_to_many",
456 "m:m",
457]
459# join
460JoinHow: TypeAlias = Literal["left", "right", "inner", "outer"]
461JoinValidate: TypeAlias = Literal[
462 "one_to_one",
463 "1:1",
464 "one_to_many",
465 "1:m",
466 "many_to_one",
467 "m:1",
468 "many_to_many",
469 "m:m",
470]
472# reindex
473ReindexMethod: TypeAlias = FillnaOptions | Literal["nearest"]
475MatplotlibColor: TypeAlias = str | Sequence[float]
476TimeGrouperOrigin: TypeAlias = Union[
477 "Timestamp", Literal["epoch", "start", "start_day", "end", "end_day"]
478]
479TimeAmbiguous: TypeAlias = (
480 Literal["infer", "NaT", "raise"] | bool | npt.NDArray[np.bool_]
481)
482TimeNonexistent: TypeAlias = (
483 Literal["shift_forward", "shift_backward", "NaT", "raise"] | timedelta
484)
486DropKeep: TypeAlias = Literal["first", "last", False]
487CorrelationMethod: TypeAlias = (
488 Literal["pearson", "kendall", "spearman"]
489 | Callable[[np.ndarray, np.ndarray], float]
490)
492AlignJoin: TypeAlias = Literal["outer", "inner", "left", "right"]
493DtypeBackend: TypeAlias = Literal["pyarrow", "numpy_nullable"]
495TimeUnit: TypeAlias = Literal["s", "ms", "us", "ns"]
496OpenFileErrors: TypeAlias = Literal[
497 "strict",
498 "ignore",
499 "replace",
500 "surrogateescape",
501 "xmlcharrefreplace",
502 "backslashreplace",
503 "namereplace",
504]
506# update
507UpdateJoin: TypeAlias = Literal["left"]
509# applymap
510NaAction: TypeAlias = Literal["ignore"]
512# from_dict
513FromDictOrient: TypeAlias = Literal["columns", "index", "tight"]
515# to_stata
516ToStataByteorder: TypeAlias = Literal[">", "<", "little", "big"]
518# ExcelWriter
519ExcelWriterIfSheetExists: TypeAlias = Literal["error", "new", "replace", "overlay"]
520ExcelWriterMergeCells: TypeAlias = bool | Literal["columns"]
522# Offsets
523OffsetCalendar: TypeAlias = Union[np.busdaycalendar, "AbstractHolidayCalendar"]
525# read_csv: usecols
526UsecolsArgType: TypeAlias = (
527 SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[HashableT], bool] | None
528)
530# maintain the sub-type of any hashable sequence
531SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable])
533SliceType: TypeAlias = Hashable | None
536# Arrow PyCapsule Interface
537# from https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html#protocol-typehints
540class ArrowArrayExportable(Protocol):
541 """
542 An object with an ``__arrow_c_array__`` method.
544 This method indicates the object is an Arrow-compatible object implementing
545 the `Arrow PyCapsule Protocol`_ (exposing the `Arrow C Data Interface`_ in
546 Python), enabling zero-copy Arrow data interchange across libraries.
548 .. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
549 .. _Arrow C Data Interface: https://arrow.apache.org/docs/format/CDataInterface.html
551 """
553 def __arrow_c_array__(
554 self, requested_schema: object | None = None
555 ) -> tuple[object, object]: ...
558class ArrowStreamExportable(Protocol):
559 """
560 An object with an ``__arrow_c_stream__`` method.
562 This method indicates the object is an Arrow-compatible object implementing
563 the `Arrow PyCapsule Protocol`_ (exposing the `Arrow C Data Interface`_
564 for streams in Python), enabling zero-copy Arrow data interchange across
565 libraries.
567 .. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
568 .. _Arrow C Stream Interface: https://arrow.apache.org/docs/format/CStreamInterface.html
570 """
572 def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: ...
575__all__ = ["type_t"]