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 typing import (
20 TYPE_CHECKING,
21 Any,
22 Literal,
23 ParamSpec,
24 Protocol,
25 SupportsIndex,
26 TypeAlias,
27 TypeVar,
28 Union,
29 overload,
30)
32import numpy as np
33import numpy.typing as npt
35# To prevent import cycles place any internal imports in the branch below
36# and use a string literal forward reference to it in subsequent types
37# https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles
39# Note that Union is needed when a Union includes a pandas type
41if TYPE_CHECKING:
42 from pandas._libs import (
43 NaTType,
44 Period,
45 Timedelta,
46 Timestamp,
47 )
48 from pandas._libs.tslibs import BaseOffset
50 from pandas.core.dtypes.dtypes import ExtensionDtype
52 from pandas import (
53 DatetimeIndex,
54 Interval,
55 PeriodIndex,
56 TimedeltaIndex,
57 )
58 from pandas.arrays import (
59 DatetimeArray,
60 TimedeltaArray,
61 )
62 from pandas.core.arrays.base import ExtensionArray
63 from pandas.core.frame import DataFrame
64 from pandas.core.generic import NDFrame
65 from pandas.core.groupby.generic import (
66 DataFrameGroupBy,
67 GroupBy,
68 SeriesGroupBy,
69 )
70 from pandas.core.indexes.base import Index
71 from pandas.core.internals import (
72 BlockManager,
73 SingleBlockManager,
74 )
75 from pandas.core.resample import Resampler
76 from pandas.core.series import Series
77 from pandas.core.window.rolling import BaseWindow
79 from pandas.io.formats.format import EngFormatter
80 from pandas.tseries.holiday import AbstractHolidayCalendar
82 ScalarLike_co: TypeAlias = int | float | complex | str | bytes | np.generic
84 # numpy compatible types
85 NumpyValueArrayLike: TypeAlias = ScalarLike_co | npt.ArrayLike
86 NumpySorter: TypeAlias = npt.NDArray[np.integer] | None
89P = ParamSpec("P")
91HashableT = TypeVar("HashableT", bound=Hashable)
92HashableT2 = TypeVar("HashableT2", bound=Hashable)
93MutableMappingT = TypeVar("MutableMappingT", bound=MutableMapping)
95# array-like
97ArrayLike: TypeAlias = Union["ExtensionArray", np.ndarray]
98ArrayLikeT = TypeVar("ArrayLikeT", "ExtensionArray", np.ndarray)
99AnyArrayLike: TypeAlias = Union[ArrayLike, "Index", "Series"]
100TimeArrayLike: TypeAlias = Union["DatetimeArray", "TimedeltaArray"]
102# list-like
104# from https://github.com/hauntsaninja/useful_types
105# includes Sequence-like objects but excludes str and bytes
106_T_co = TypeVar("_T_co", covariant=True)
109class SequenceNotStr(Protocol[_T_co]):
110 __module__: str = "pandas.api.typing.aliases"
112 @overload
113 def __getitem__(self, index: SupportsIndex, /) -> _T_co: ...
115 @overload
116 def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ...
118 def __contains__(self, value: object, /) -> bool: ...
120 def __len__(self) -> int: ...
122 def __iter__(self) -> Iterator[_T_co]: ...
124 def index(self, value: Any, start: int = ..., stop: int = ..., /) -> int: ...
126 def count(self, value: Any, /) -> int: ...
128 def __reversed__(self) -> Iterator[_T_co]: ...
131ListLike: TypeAlias = AnyArrayLike | SequenceNotStr | range
133# scalars
135PythonScalar: TypeAlias = str | float | bool
136DatetimeLikeScalar: TypeAlias = Union["Period", "Timestamp", "Timedelta"]
138# aligned with pandas-stubs - typical scalars found in Series. Explicitly leaves
139# out object
140_IndexIterScalar: TypeAlias = Union[
141 str,
142 bytes,
143 date,
144 datetime,
145 timedelta,
146 np.datetime64,
147 np.timedelta64,
148 bool,
149 int,
150 float,
151 "Timestamp",
152 "Timedelta",
153]
154Scalar: TypeAlias = Union[
155 _IndexIterScalar, "Interval", complex, np.integer, np.floating, np.complexfloating
156]
158IntStrT = TypeVar("IntStrT", bound=int | str)
160# timestamp and timedelta convertible types
162TimestampConvertibleTypes: TypeAlias = Union[
163 "Timestamp", date, np.datetime64, np.int64, float, str
164]
165TimestampNonexistent: TypeAlias = (
166 Literal["shift_forward", "shift_backward", "NaT", "raise"] | timedelta
167)
169TimedeltaConvertibleTypes: TypeAlias = Union[
170 "Timedelta", timedelta, np.timedelta64, np.int64, float, str
171]
172Timezone: TypeAlias = str | tzinfo
174ToTimestampHow: TypeAlias = Literal["s", "e", "start", "end"]
176# NDFrameT is stricter and ensures that the same subclass of NDFrame always is
177# used. E.g. `def func(a: NDFrameT) -> NDFrameT: ...` means that if a
178# Series is passed into a function, a Series is always returned and if a DataFrame is
179# passed in, a DataFrame is always returned.
180NDFrameT = TypeVar("NDFrameT", bound="NDFrame")
182IndexT = TypeVar("IndexT", bound="Index")
183FreqIndexT = TypeVar("FreqIndexT", "DatetimeIndex", "PeriodIndex", "TimedeltaIndex")
184NumpyIndexT = TypeVar("NumpyIndexT", np.ndarray, "Index")
186AxisInt: TypeAlias = int
187Axis: TypeAlias = AxisInt | Literal["index", "columns", "rows"]
188IndexLabel: TypeAlias = Hashable | Sequence[Hashable]
189Level: TypeAlias = Hashable
190Shape: TypeAlias = tuple[int, ...]
191Suffixes: TypeAlias = Sequence[str | None]
192Ordered: TypeAlias = bool | None
193JSONSerializable: TypeAlias = PythonScalar | list | dict | None
194Frequency: TypeAlias = Union[str, "BaseOffset"]
195Axes: TypeAlias = ListLike
197RandomState: TypeAlias = (
198 int
199 | np.ndarray
200 | np.random.Generator
201 | np.random.BitGenerator
202 | np.random.RandomState
203)
206# dtypes
207NpDtype: TypeAlias = str | np.dtype | type[str | complex | bool | object]
208Dtype: TypeAlias = Union["ExtensionDtype", NpDtype]
209AstypeArg: TypeAlias = Union["ExtensionDtype", npt.DTypeLike]
210# DtypeArg specifies all allowable dtypes in a functions its dtype argument
211DtypeArg: TypeAlias = Dtype | Mapping[Hashable, Dtype]
212DtypeObj: TypeAlias = Union[np.dtype, "ExtensionDtype"]
214# converters
215ConvertersArg: TypeAlias = dict[Hashable, Callable[[Dtype], Dtype]]
217# parse_dates
218ParseDatesArg: TypeAlias = (
219 bool | list[Hashable] | list[list[Hashable]] | dict[Hashable, list[Hashable]]
220)
222# For functions like rename that convert one label to another
223Renamer: TypeAlias = Mapping[Any, Hashable] | Callable[[Any], Hashable]
225# to maintain type information across generic functions and parametrization
226T = TypeVar("T")
228# used in decorators to preserve the signature of the function it decorates
229# see https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
230FuncType: TypeAlias = Callable[..., Any]
231F = TypeVar("F", bound=FuncType)
232TypeT = TypeVar("TypeT", bound=type)
234# types of vectorized key functions for DataFrame::sort_values and
235# DataFrame::sort_index, among others
236ValueKeyFunc: TypeAlias = Callable[["Series"], Union["Series", AnyArrayLike]] | None
237IndexKeyFunc: TypeAlias = Callable[["Index"], Union["Index", AnyArrayLike]] | None
239# types of `func` kwarg for DataFrame.aggregate and Series.aggregate
240AggFuncTypeBase: TypeAlias = Callable | str
241AggFuncTypeDict: TypeAlias = MutableMapping[
242 Hashable, AggFuncTypeBase | list[AggFuncTypeBase]
243]
244AggFuncType: TypeAlias = AggFuncTypeBase | list[AggFuncTypeBase] | AggFuncTypeDict
245AggObjType: TypeAlias = Union[
246 "Series",
247 "DataFrame",
248 "GroupBy",
249 "SeriesGroupBy",
250 "DataFrameGroupBy",
251 "BaseWindow",
252 "Resampler",
253]
255PythonFuncType: TypeAlias = Callable[[Any], Any]
257# filenames and file-like-objects
258AnyStr_co = TypeVar("AnyStr_co", str, bytes, covariant=True)
259AnyStr_contra = TypeVar("AnyStr_contra", str, bytes, contravariant=True)
262class BaseBuffer(Protocol):
263 @property
264 def mode(self) -> str:
265 # for _get_filepath_or_buffer
266 ...
268 def seek(self, offset: int, whence: int = ..., /) -> int:
269 # with one argument: gzip.GzipFile, bz2.BZ2File
270 # with two arguments: zip.ZipFile, read_sas
271 ...
273 def seekable(self) -> bool:
274 # for bz2.BZ2File
275 ...
277 def tell(self) -> int:
278 # for zip.ZipFile, read_stata, to_stata
279 ...
282class ReadBuffer(BaseBuffer, Protocol[AnyStr_co]):
283 __module__: str = "pandas.api.typing.aliases"
285 def read(self, n: int = ..., /) -> AnyStr_co:
286 # for BytesIOWrapper, gzip.GzipFile, bz2.BZ2File
287 ...
290class WriteBuffer(BaseBuffer, Protocol[AnyStr_contra]):
291 __module__: str = "pandas.api.typing.aliases"
293 def write(self, b: AnyStr_contra, /) -> Any:
294 # for gzip.GzipFile, bz2.BZ2File
295 ...
297 def flush(self) -> Any:
298 # for gzip.GzipFile, bz2.BZ2File
299 ...
302class ReadPickleBuffer(ReadBuffer[bytes], Protocol):
303 __module__: str = "pandas.api.typing.aliases"
305 def readline(self) -> bytes: ...
308class WriteExcelBuffer(WriteBuffer[bytes], Protocol):
309 __module__: str = "pandas.api.typing.aliases"
311 def truncate(self, size: int | None = ..., /) -> int: ...
314class ReadCsvBuffer(ReadBuffer[AnyStr_co], Protocol):
315 __module__: str = "pandas.api.typing.aliases"
317 def __iter__(self) -> Iterator[AnyStr_co]:
318 # for engine=python
319 ...
321 def fileno(self) -> int:
322 # for _MMapWrapper
323 ...
325 def readline(self) -> AnyStr_co:
326 # for engine=python
327 ...
329 @property
330 def closed(self) -> bool:
331 # for engine=pyarrow
332 ...
335FilePath: TypeAlias = str | PathLike[str]
337# for arbitrary kwargs passed during reading/writing files
338StorageOptions: TypeAlias = dict[str, Any] | None
340# compression keywords and compression
341CompressionDict: TypeAlias = dict[str, Any]
342CompressionOptions: TypeAlias = (
343 Literal["infer", "gzip", "bz2", "zip", "xz", "zstd", "tar"] | CompressionDict | None
344)
345ParquetCompressionOptions: TypeAlias = (
346 Literal["snappy", "gzip", "brotli", "lz4", "zstd"] | None
347)
349# types in DataFrameFormatter
350FormattersType: TypeAlias = (
351 list[Callable] | tuple[Callable, ...] | Mapping[str | int, Callable]
352)
353ColspaceType: TypeAlias = Mapping[Hashable, str | int]
354FloatFormatType: TypeAlias = Union[str, Callable, "EngFormatter"]
355ColspaceArgType: TypeAlias = (
356 str | int | Sequence[str | int] | Mapping[Hashable, str | int]
357)
359# Arguments for fillna()
360FillnaOptions: TypeAlias = Literal["backfill", "bfill", "ffill", "pad"]
361InterpolateOptions: TypeAlias = Literal[
362 "linear",
363 "time",
364 "index",
365 "values",
366 "nearest",
367 "zero",
368 "slinear",
369 "quadratic",
370 "cubic",
371 "barycentric",
372 "polynomial",
373 "krogh",
374 "piecewise_polynomial",
375 "spline",
376 "pchip",
377 "akima",
378 "cubicspline",
379 "from_derivatives",
380]
382# internals
383Manager: TypeAlias = Union["BlockManager", "SingleBlockManager"]
385# indexing
386# PositionalIndexer -> valid 1D positional indexer, e.g. can pass
387# to ndarray.__getitem__
388# ScalarIndexer is for a single value as the index
389# SequenceIndexer is for list like or slices (but not tuples)
390# PositionalIndexerTuple is extends the PositionalIndexer for 2D arrays
391# These are used in various __getitem__ overloads
392# TODO(typing#684): add Ellipsis, see
393# https://github.com/python/typing/issues/684#issuecomment-548203158
394# https://bugs.python.org/issue41810
395# Using List[int] here rather than Sequence[int] to disallow tuples.
396ScalarIndexer: TypeAlias = int | np.integer
397SequenceIndexer: TypeAlias = slice | list[int] | np.ndarray
398PositionalIndexer: TypeAlias = ScalarIndexer | SequenceIndexer
399PositionalIndexerTuple: TypeAlias = tuple[PositionalIndexer, PositionalIndexer]
400PositionalIndexer2D: TypeAlias = PositionalIndexer | PositionalIndexerTuple
401TakeIndexer: TypeAlias = Sequence[int] | Sequence[np.integer] | npt.NDArray[np.integer]
403# Shared by functions such as drop and astype
404IgnoreRaise: TypeAlias = Literal["ignore", "raise"]
406# Windowing rank methods
407WindowingRankType: TypeAlias = Literal["average", "min", "max"]
409# read_csv engines
410CSVEngine: TypeAlias = Literal["c", "python", "pyarrow", "python-fwf"]
412# read_json engines
413JSONEngine: TypeAlias = Literal["ujson", "pyarrow"]
415# read_xml parsers
416XMLParsers: TypeAlias = Literal["lxml", "etree"]
418# read_html flavors
419HTMLFlavors: TypeAlias = Literal["lxml", "html5lib", "bs4"]
421# Interval closed type
422IntervalLeftRight: TypeAlias = Literal["left", "right"]
423IntervalClosedType: TypeAlias = IntervalLeftRight | Literal["both", "neither"]
425# datetime and NaTType
426DatetimeNaTType: TypeAlias = Union[datetime, "NaTType"]
427DateTimeErrorChoices: TypeAlias = Literal["raise", "coerce"]
429# sort_index
430SortKind: TypeAlias = Literal["quicksort", "mergesort", "heapsort", "stable"]
431NaPosition: TypeAlias = Literal["first", "last"]
433# Arguments for nsmallest and nlargest
434NsmallestNlargestKeep: TypeAlias = Literal["first", "last", "all"]
436# quantile interpolation
437QuantileInterpolation: TypeAlias = Literal[
438 "linear", "lower", "higher", "midpoint", "nearest"
439]
441# plotting
442PlottingOrientation: TypeAlias = Literal["horizontal", "vertical"]
444# dropna
445AnyAll: TypeAlias = Literal["any", "all"]
447# merge
448MergeHow: TypeAlias = Literal[
449 "left", "right", "inner", "outer", "cross", "left_anti", "right_anti"
450]
451MergeValidate: TypeAlias = Literal[
452 "one_to_one",
453 "1:1",
454 "one_to_many",
455 "1:m",
456 "many_to_one",
457 "m:1",
458 "many_to_many",
459 "m:m",
460]
462# join
463JoinHow: TypeAlias = Literal["left", "right", "inner", "outer"]
464JoinValidate: TypeAlias = Literal[
465 "one_to_one",
466 "1:1",
467 "one_to_many",
468 "1:m",
469 "many_to_one",
470 "m:1",
471 "many_to_many",
472 "m:m",
473]
475# reindex
476ReindexMethod: TypeAlias = FillnaOptions | Literal["nearest"]
478MatplotlibColor: TypeAlias = str | Sequence[float]
479TimeGrouperOrigin: TypeAlias = Union[
480 "Timestamp", Literal["epoch", "start", "start_day", "end", "end_day"]
481]
482TimeAmbiguous: TypeAlias = (
483 Literal["infer", "NaT", "raise"] | bool | npt.NDArray[np.bool_]
484)
485TimeNonexistent: TypeAlias = (
486 Literal["shift_forward", "shift_backward", "NaT", "raise"] | timedelta
487)
489DropKeep: TypeAlias = Literal["first", "last", False]
490CorrelationMethod: TypeAlias = (
491 Literal["pearson", "kendall", "spearman"]
492 | Callable[[np.ndarray, np.ndarray], float]
493)
495AlignJoin: TypeAlias = Literal["outer", "inner", "left", "right"]
496DtypeBackend: TypeAlias = Literal["pyarrow", "numpy_nullable"]
498TimeUnit: TypeAlias = Literal["s", "ms", "us", "ns"]
499OpenFileErrors: TypeAlias = Literal[
500 "strict",
501 "ignore",
502 "replace",
503 "surrogateescape",
504 "xmlcharrefreplace",
505 "backslashreplace",
506 "namereplace",
507]
509# update
510UpdateJoin: TypeAlias = Literal["left"]
512# applymap
513NaAction: TypeAlias = Literal["ignore"]
515# from_dict
516FromDictOrient: TypeAlias = Literal["columns", "index", "tight"]
518# to_stata
519ToStataByteorder: TypeAlias = Literal[">", "<", "little", "big"]
521# ExcelWriter
522ExcelWriterIfSheetExists: TypeAlias = Literal["error", "new", "replace", "overlay"]
523ExcelWriterMergeCells: TypeAlias = bool | Literal["columns"]
525# Offsets
526OffsetCalendar: TypeAlias = Union[np.busdaycalendar, "AbstractHolidayCalendar"]
528# read_csv: usecols
529UsecolsArgType: TypeAlias = (
530 SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[HashableT], bool] | None
531)
533# maintain the sub-type of any hashable sequence
534SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable])
536SliceType: TypeAlias = Hashable | None
539# Arrow PyCapsule Interface
540# from https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html#protocol-typehints
543class ArrowArrayExportable(Protocol):
544 """
545 An object with an ``__arrow_c_array__`` method.
547 This method indicates the object is an Arrow-compatible object implementing
548 the `Arrow PyCapsule Protocol`_ (exposing the `Arrow C Data Interface`_ in
549 Python), enabling zero-copy Arrow data interchange across libraries.
551 .. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
552 .. _Arrow C Data Interface: https://arrow.apache.org/docs/format/CDataInterface.html
554 """
556 def __arrow_c_array__(
557 self, requested_schema: object | None = None
558 ) -> tuple[object, object]: ...
561class ArrowStreamExportable(Protocol):
562 """
563 An object with an ``__arrow_c_stream__`` method.
565 This method indicates the object is an Arrow-compatible object implementing
566 the `Arrow PyCapsule Protocol`_ (exposing the `Arrow C Data Interface`_
567 for streams in Python), enabling zero-copy Arrow data interchange across
568 libraries.
570 .. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
571 .. _Arrow C Stream Interface: https://arrow.apache.org/docs/format/CStreamInterface.html
573 """
575 def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: ...
578__all__ = ["type_t"]