diff --git a/pandas-stubs/_libs/tslibs/nattype.pyi b/pandas-stubs/_libs/tslibs/nattype.pyi index 40f745497..b52dd542e 100644 --- a/pandas-stubs/_libs/tslibs/nattype.pyi +++ b/pandas-stubs/_libs/tslibs/nattype.pyi @@ -4,6 +4,7 @@ from datetime import ( timedelta, tzinfo as _tzinfo, ) +from typing import Literal import numpy as np from typing_extensions import ( @@ -13,7 +14,9 @@ from typing_extensions import ( from pandas._libs.tslibs.period import Period from pandas._typing import ( + Frequency, NpDtype, + TimestampNonexistent, TimeUnit, ) @@ -75,11 +78,31 @@ class NaTType: def now(self, tz: _tzinfo | str | None = ...) -> NaTType: ... def to_pydatetime(self) -> NaTType: ... def date(self) -> NaTType: ... - def round(self) -> NaTType: ... - def floor(self) -> NaTType: ... - def ceil(self) -> NaTType: ... + def round( + self, + freq: Frequency, + ambiguous: bool | Literal["raise"] | NaTType = ..., + nonexistent: TimestampNonexistent = ..., + ) -> NaTType: ... + def floor( + self, + freq: Frequency, + ambiguous: bool | Literal["raise"] | NaTType = ..., + nonexistent: TimestampNonexistent = ..., + ) -> NaTType: ... + def ceil( + self, + freq: Frequency, + ambiguous: bool | Literal["raise"] | NaTType = ..., + nonexistent: TimestampNonexistent = ..., + ) -> NaTType: ... def tz_convert(self) -> NaTType: ... - def tz_localize(self) -> NaTType: ... + def tz_localize( + self, + tz: _tzinfo | str | None, + ambiguous: bool | Literal["raise"] | NaTType = ..., + nonexistent: TimestampNonexistent = ..., + ) -> NaTType: ... def replace( self, year: int | None = ..., diff --git a/pandas-stubs/_libs/tslibs/timestamps.pyi b/pandas-stubs/_libs/tslibs/timestamps.pyi index 936567fdd..99de96630 100644 --- a/pandas-stubs/_libs/tslibs/timestamps.pyi +++ b/pandas-stubs/_libs/tslibs/timestamps.pyi @@ -40,15 +40,14 @@ from pandas._libs.tslibs import ( Timedelta, ) from pandas._typing import ( + TimestampNonexistent, TimeUnit, np_ndarray_bool, npt, ) _Ambiguous: TypeAlias = bool | Literal["raise", "NaT"] -_Nonexistent: TypeAlias = ( - Literal["raise", "NaT", "shift_backward", "shift_forward"] | Timedelta | timedelta -) + # Repeated from `_typing.pyi` so as to satisfy mixed strict / non-strict paths. # https://github.com/pandas-dev/pandas-stubs/pull/1151#issuecomment-2715130190 TimeZones: TypeAlias = str | _tzinfo | None | int @@ -278,7 +277,7 @@ class Timestamp(datetime, SupportsIndex): self, tz: TimeZones, ambiguous: _Ambiguous = ..., - nonexistent: _Nonexistent = ..., + nonexistent: TimestampNonexistent = ..., ) -> Self: ... def normalize(self) -> Self: ... # TODO: round/floor/ceil could return NaT? @@ -286,19 +285,19 @@ class Timestamp(datetime, SupportsIndex): self, freq: str, ambiguous: _Ambiguous = ..., - nonexistent: _Nonexistent = ..., + nonexistent: TimestampNonexistent = ..., ) -> Self: ... def floor( self, freq: str, ambiguous: _Ambiguous = ..., - nonexistent: _Nonexistent = ..., + nonexistent: TimestampNonexistent = ..., ) -> Self: ... def ceil( self, freq: str, ambiguous: _Ambiguous = ..., - nonexistent: _Nonexistent = ..., + nonexistent: TimestampNonexistent = ..., ) -> Self: ... def day_name(self, locale: str | None = ...) -> str: ... def month_name(self, locale: str | None = ...) -> str: ... diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 4873d5fc2..28ee2ffff 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -53,36 +53,126 @@ from pandas.core.dtypes.dtypes import ( from pandas.io.formats.format import EngFormatter -# `Incomplete` is equivalent to `Any`. Use it to annotate symbols that you don't -# know the type of yet and that should be changed in the future. Use `Any` only -# where it is the only acceptable type. -Incomplete: TypeAlias = Any +P = ParamSpec("P") + +HashableT = TypeVar("HashableT", bound=Hashable) +HashableT1 = TypeVar("HashableT1", bound=Hashable) +HashableT2 = TypeVar("HashableT2", bound=Hashable) +HashableT3 = TypeVar("HashableT3", bound=Hashable) +HashableT4 = TypeVar("HashableT4", bound=Hashable) +HashableT5 = TypeVar("HashableT5", bound=Hashable) + +# array-like ArrayLike: TypeAlias = ExtensionArray | np.ndarray AnyArrayLike: TypeAlias = ArrayLike | Index[Any] | Series[Any] + +# list-like + +_T_co = TypeVar("_T_co", covariant=True) + +class SequenceNotStr(Protocol[_T_co]): + @overload + def __getitem__(self, index: SupportsIndex, /) -> _T_co: ... + @overload + def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ... + def __contains__(self, value: object, /) -> bool: ... + def __len__(self) -> int: ... + def __iter__(self) -> Iterator[_T_co]: ... + def index(self, value: Any, start: int = ..., stop: int = ..., /) -> int: ... + def count(self, value: Any, /) -> int: ... + def __reversed__(self) -> Iterator[_T_co]: ... + +ListLike: TypeAlias = AnyArrayLike | SequenceNotStr[Any] | range + +# scalars + PythonScalar: TypeAlias = str | bool | complex -DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", bound=Period | Timestamp | Timedelta) -PandasScalar: TypeAlias = bytes | datetime.date | datetime.datetime | datetime.timedelta +DatetimeLikeScalar: TypeAlias = Period | Timestamp | Timedelta +PandasScalar: TypeAlias = Period | Timestamp | Timedelta | Interval + +_IndexIterScalar: TypeAlias = ( + str + | bytes + | datetime.date + | datetime.datetime + | datetime.timedelta + | np.datetime64 + | np.timedelta64 + | bool + | int + | float + | Timestamp + | Timedelta +) +# This is wider than what is in pandas +Scalar: TypeAlias = ( + _IndexIterScalar | complex | np.integer | np.floating | np.complexfloating +) IntStrT = TypeVar("IntStrT", int, str) -# Scalar: TypeAlias = PythonScalar | PandasScalar -DatetimeLike: TypeAlias = datetime.datetime | np.datetime64 | Timestamp -DateAndDatetimeLike: TypeAlias = datetime.date | DatetimeLike +# timestamp and timedelta convertible types -DatetimeDictArg: TypeAlias = ( - Sequence[int] | Sequence[float] | list[str] | tuple[Scalar, ...] | AnyArrayLike +TimestampConvertibleTypes: TypeAlias = ( + Timestamp + | datetime.datetime + | datetime.date + | np.datetime64 + | np.integer + | float + | str +) +TimestampNonexistent: TypeAlias = ( + Literal["shift_forward", "shift_backward", "NaT", "raise"] + | Timedelta + | datetime.timedelta +) +TimedeltaConvertibleTypes: TypeAlias = ( + Timedelta | datetime.timedelta | np.timedelta64 | np.integer | float | str ) -DictConvertible: TypeAlias = FulldatetimeDict | DataFrame -CorrelationMethod: TypeAlias = ( - Literal["pearson", "kendall", "spearman"] - | Callable[[np.ndarray, np.ndarray], float] +Timezone: TypeAlias = str | tzinfo # Not used in pandas or the stubs + +ToTimestampHow: TypeAlias = Literal["s", "e", "start", "end"] + +# NDFrameT is stricter and ensures that the same subclass of NDFrame always is +# used. E.g. `def func(a: NDFrameT) -> NDFrameT: ...` means that if a +# Series is passed into a function, a Series is always returned and if a DataFrame is +# passed in, a DataFrame is always returned. +NDFrameT = TypeVar("NDFrameT", bound=NDFrame) + +IndexT = TypeVar("IndexT", bound=Index) + +# From _typing.py, not used here: +# FreqIndexT = TypeVar("FreqIndexT", "DatetimeIndex", "PeriodIndex", "TimedeltaIndex") +# NumpyIndexT = TypeVar("NumpyIndexT", np.ndarray, "Index") + +AxisInt: TypeAlias = int +AxisIndex: TypeAlias = Literal["index", 0] +AxisColumn: TypeAlias = Literal["columns", 1] +Axis: TypeAlias = AxisIndex | AxisColumn # slight difference from pandas +IndexLabel: TypeAlias = Hashable | Sequence[Hashable] +Level: TypeAlias = Hashable +Shape: TypeAlias = tuple[int, ...] +Suffixes: TypeAlias = tuple[str | None, str | None] | list[str | None] +Ordered: TypeAlias = bool | None +JSONSerializable: TypeAlias = PythonScalar | list | dict +Frequency: TypeAlias = str | BaseOffset +Axes: TypeAlias = ListLike + +RandomState: TypeAlias = ( + int + | ArrayLike + | np.random.Generator + | np.random.BitGenerator + | np.random.RandomState ) + # dtypes NpDtype: TypeAlias = str | np.dtype[np.generic] | type[str | complex | bool | object] Dtype: TypeAlias = ExtensionDtype | NpDtype -DtypeArg: TypeAlias = Dtype | Mapping[Any, Dtype] -DtypeBackend: TypeAlias = Literal["pyarrow", "numpy_nullable"] + +# AstypeArg is more carefully defined here as compared to pandas # NOTE: we want to catch all the possible dtypes from np.sctypeDict # timedelta64 @@ -373,9 +463,6 @@ VoidDtypeArg: TypeAlias = ( | Literal["V", "void", "void0"] ) -# DtypeArg specifies all allowable dtypes in a functions its dtype argument -DtypeObj: TypeAlias = np.dtype[np.generic] | ExtensionDtype - AstypeArg: TypeAlias = ( BooleanDtypeArg | IntDtypeArg @@ -392,74 +479,40 @@ AstypeArg: TypeAlias = ( | DtypeObj ) -# filenames and file-like-objects -AnyStr_cov = TypeVar("AnyStr_cov", str, bytes, covariant=True) -AnyStr_con = TypeVar("AnyStr_con", str, bytes, contravariant=True) - -class BaseBuffer(Protocol): - @property - def mode(self) -> str: ... - def seek(self, offset: int, whence: int = ..., /) -> int: ... - def seekable(self) -> bool: ... - def tell(self) -> int: ... - -class ReadBuffer(BaseBuffer, Protocol[AnyStr_cov]): - def read(self, n: int = ..., /) -> AnyStr_cov: ... - -class WriteBuffer(BaseBuffer, Protocol[AnyStr_con]): - def write(self, b: AnyStr_con, /) -> Any: ... - def flush(self) -> Any: ... - -class ReadPickleBuffer(ReadBuffer[bytes], Protocol): - def readline(self) -> bytes: ... - -class ReadCsvBuffer(ReadBuffer[AnyStr_cov], Protocol[AnyStr_cov]): - def __iter__(self) -> Iterator[AnyStr_cov]: ... - def fileno(self) -> int: ... - def readline(self) -> AnyStr_cov: ... - @property - def closed(self) -> bool: ... - -class WriteExcelBuffer(WriteBuffer[bytes], Protocol): - def truncate(self, size: int | None = ..., /) -> int: ... - -FilePath: TypeAlias = str | PathLike[str] +# DtypeArg specifies all allowable dtypes in a functions its dtype argument +DtypeArg: TypeAlias = Dtype | Mapping[Hashable, Dtype] +DtypeObj: TypeAlias = np.dtype[np.generic] | ExtensionDtype -_T_co = TypeVar("_T_co", covariant=True) +# converters +ConvertersArg: TypeAlias = Mapping[Hashable, Callable[[Dtype], Dtype]] -class SequenceNotStr(Protocol[_T_co]): - @overload - def __getitem__(self, index: SupportsIndex, /) -> _T_co: ... - @overload - def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ... - def __contains__(self, value: object, /) -> bool: ... - def __len__(self) -> int: ... - def __iter__(self) -> Iterator[_T_co]: ... - def index(self, value: Any, start: int = ..., stop: int = ..., /) -> int: ... - def count(self, value: Any, /) -> int: ... - def __reversed__(self) -> Iterator[_T_co]: ... +# parse_dates +ParseDatesArg: TypeAlias = ( + bool | list[Hashable] | list[list[Hashable]] | Mapping[HashableT, list[HashableT2]] +) -IndexLabel: TypeAlias = Hashable | Sequence[Hashable] +# Not in pandas Label: TypeAlias = Hashable | None -Level: TypeAlias = Hashable | int -Shape: TypeAlias = tuple[int, ...] -Suffixes: TypeAlias = tuple[str | None, str | None] -Ordered: TypeAlias = bool | None -JSONSerializable: TypeAlias = PythonScalar | list | dict -Axes: TypeAlias = AnyArrayLike | list | dict | range | tuple + +# For functions like rename that convert one label to another Renamer: TypeAlias = Mapping[Any, Label] | Callable[[Any], Label] + +# to maintain type information across generic functions and parametrization T = TypeVar("T") -P = ParamSpec("P") + +# used in decorators to preserve the signature of the function it decorates +# see https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators FuncType: TypeAlias = Callable[..., Any] F = TypeVar("F", bound=FuncType) TypeT = TypeVar("TypeT", bound=type) -HashableT = TypeVar("HashableT", bound=Hashable) -HashableT1 = TypeVar("HashableT1", bound=Hashable) -HashableT2 = TypeVar("HashableT2", bound=Hashable) -HashableT3 = TypeVar("HashableT3", bound=Hashable) -HashableT4 = TypeVar("HashableT4", bound=Hashable) -HashableT5 = TypeVar("HashableT5", bound=Hashable) +# types of vectorized key functions for DataFrame::sort_values and +# DataFrame::sort_index, among others +ValueKeyFunc: TypeAlias = Callable[[Series], Series | AnyArrayLike] | None +IndexKeyFunc: TypeAlias = Callable[[Index], Index | AnyArrayLike] | None + +# types of `func` kwarg for DataFrame.aggregate and Series.aggregate +# More specific than what is in pandas AggFuncTypeBase: TypeAlias = Callable | str | np.ufunc AggFuncTypeDictSeries: TypeAlias = Mapping[HashableT, AggFuncTypeBase] AggFuncTypeDictFrame: TypeAlias = Mapping[ @@ -472,14 +525,280 @@ AggFuncTypeFrame: TypeAlias = ( AggFuncTypeDict: TypeAlias = AggFuncTypeDictSeries | AggFuncTypeDictFrame AggFuncType: TypeAlias = AggFuncTypeBase | list[AggFuncTypeBase] | AggFuncTypeDict +# Not used in stubs +# AggObjType = Union[ +# "Series", +# "DataFrame", +# "GroupBy", +# "SeriesGroupBy", +# "DataFrameGroupBy", +# "BaseWindow", +# "Resampler", +# ] + +# filenames and file-like-objects +AnyStr_co = TypeVar("AnyStr_co", str, bytes, covariant=True) +AnyStr_contra = TypeVar("AnyStr_contra", str, bytes, contravariant=True) + +class BaseBuffer(Protocol): + @property + def mode(self) -> str: + # for _get_filepath_or_buffer + ... + + def seek(self, offset: int, whence: int = ..., /) -> int: + # with one argument: gzip.GzipFile, bz2.BZ2File + # with two arguments: zip.ZipFile, read_sas + ... + + def seekable(self) -> bool: + # for bz2.BZ2File + ... + + def tell(self) -> int: + # for zip.ZipFile, read_stata, to_stata + ... + +class ReadBuffer(BaseBuffer, Protocol[AnyStr_co]): + def read(self, n: int = ..., /) -> AnyStr_co: + # for BytesIOWrapper, gzip.GzipFile, bz2.BZ2File + ... + +class WriteBuffer(BaseBuffer, Protocol[AnyStr_contra]): + def write(self, b: AnyStr_contra, /) -> Any: + # for gzip.GzipFile, bz2.BZ2File + ... + + def flush(self) -> Any: + # for gzip.GzipFile, bz2.BZ2File + ... + +class ReadPickleBuffer(ReadBuffer[bytes], Protocol): + def readline(self) -> bytes: ... + +class WriteExcelBuffer(WriteBuffer[bytes], Protocol): + def truncate(self, size: int | None = ..., /) -> int: ... + +class ReadCsvBuffer(ReadBuffer[AnyStr_co], Protocol): + def __iter__(self) -> Iterator[AnyStr_co]: + # for engine=python + ... + + def fileno(self) -> int: + # for _MMapWrapper + ... + + def readline(self) -> AnyStr_co: + # for engine=python + ... + + @property + def closed(self) -> bool: + # for engine=pyarrow + ... + +FilePath: TypeAlias = str | PathLike[str] + +# for arbitrary kwargs passed during reading/writing files +StorageOptions: TypeAlias = dict[str, Any] | None + +# compression keywords and compression +CompressionDict: TypeAlias = dict[str, Any] +CompressionOptions: TypeAlias = ( + None | Literal["infer", "gzip", "bz2", "zip", "xz", "zstd", "tar"] | CompressionDict +) + +# types in DataFrameFormatter +FormattersType: TypeAlias = ( + list[Callable] | tuple[Callable, ...] | Mapping[str | int, Callable] +) +# ColspaceType = Mapping[Hashable, Union[str, int]] not used in stubs +FloatFormatType: TypeAlias = str | Callable[[float], str] | EngFormatter +ColspaceArgType: TypeAlias = ( + str | int | Sequence[int | str] | Mapping[Hashable, str | int] +) + +# Arguments for fillna() +FillnaOptions: TypeAlias = Literal["backfill", "bfill", "ffill", "pad"] +InterpolateOptions: TypeAlias = Literal[ + "linear", + "time", + "index", + "values", + "nearest", + "zero", + "slinear", + "quadratic", + "cubic", + "barycentric", + "polynomial", + "krogh", + "piecewise_polynomial", + "spline", + "pchip", + "akima", + "cubicspline", + "from_derivatives", +] + +# internals +# Manager = Union["BlockManager", "SingleBlockManager"] not used in stubs + +# indexing +# PositionalIndexer -> valid 1D positional indexer, e.g. can pass +# to ndarray.__getitem__ +# ScalarIndexer is for a single value as the index +# SequenceIndexer is for list like or slices (but not tuples) +# PositionalIndexerTuple is extends the PositionalIndexer for 2D arrays +# These are used in various __getitem__ overloads +# TODO(typing#684): add Ellipsis, see +# https://github.com/python/typing/issues/684#issuecomment-548203158 +# https://bugs.python.org/issue41810 +# Using List[int] here rather than Sequence[int] to disallow tuples. + +ScalarIndexer: TypeAlias = int | np.integer +SequenceIndexer: TypeAlias = slice | list[int] | np.ndarray +PositionalIndexer: TypeAlias = ScalarIndexer | SequenceIndexer +PositionalIndexerTuple: TypeAlias = tuple[PositionalIndexer, PositionalIndexer] +# PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] Not used in stubs +TakeIndexer: TypeAlias = Sequence[int] | Sequence[np.integer] | npt.NDArray[np.integer] + +# Shared by functions such as drop and astype +IgnoreRaise: TypeAlias = Literal["ignore", "raise"] + +# Windowing rank methods +WindowingRankType: TypeAlias = Literal["average", "min", "max"] + +# read_csv engines +CSVEngine: TypeAlias = Literal["c", "python", "pyarrow", "python-fwf"] + +# read_json engines +JSONEngine: TypeAlias = Literal["ujson", "pyarrow"] + +# read_xml parsers +XMLParsers: TypeAlias = Literal["lxml", "etree"] + +# read_html flavors +HTMLFlavors: TypeAlias = Literal["lxml", "html5lib", "bs4"] + +# Interval closed type +IntervalT = TypeVar("IntervalT", bound=Interval) +IntervalLeftRight: TypeAlias = Literal["left", "right"] +IntervalClosedType: TypeAlias = IntervalLeftRight | Literal["both", "neither"] + +# datetime and NaTType +# DatetimeNaTType = datetime | NaTType not used in stubs +RaiseCoerce: TypeAlias = Literal["raise", "coerce"] +DateTimeErrorChoices: TypeAlias = RaiseCoerce + +# sort_index +SortKind: TypeAlias = Literal["quicksort", "mergesort", "heapsort", "stable"] +NaPosition: TypeAlias = Literal["first", "last"] + +# Arguments for nsmallest and nlargest +NsmallestNlargestKeep: TypeAlias = Literal["first", "last", "all"] + +# quantile interpolation +QuantileInterpolation: TypeAlias = Literal[ + "linear", "lower", "higher", "midpoint", "nearest" +] + +# plotting +# PlottingOrientation = Literal["horizontal", "vertical"] not used in stubs + +# dropna +AnyAll: TypeAlias = Literal["any", "all"] + +# merge +# defined in a different manner, but equivalent to pandas +JoinHow: TypeAlias = Literal["left", "right", "outer", "inner"] +MergeHow: TypeAlias = JoinHow | Literal["cross", "left_anti", "right_anti"] +ValidationOptions: TypeAlias = Literal[ + "one_to_one", + "1:1", + "one_to_many", + "1:m", + "many_to_one", + "m:1", + "many_to_many", + "m:m", +] +MergeValidate: TypeAlias = ValidationOptions +JoinValidate: TypeAlias = ValidationOptions + +# reindex +ReindexMethod: TypeAlias = FillnaOptions | Literal["nearest"] + +# MatplotlibColor = Union[str, Sequence[float]] # not used in stubs +TimeGrouperOrigin: TypeAlias = ( + Timestamp | Literal["epoch", "start", "start_day", "end", "end_day"] +) +TimeAmbiguous: TypeAlias = Literal["infer", "NaT", "raise"] | npt.NDArray[np.bool_] +# Note this is same as TimestampNonexistent - defined both ways in pandas +TimeNonexistent: TypeAlias = ( + Literal["shift_forward", "shift_backward", "NaT", "raise"] + | Timedelta + | datetime.timedelta +) +DropKeep: TypeAlias = Literal["first", "last", False] +CorrelationMethod: TypeAlias = ( + Literal["pearson", "kendall", "spearman"] + | Callable[[np.ndarray, np.ndarray], float] +) +AlignJoin: TypeAlias = Literal["outer", "inner", "left", "right"] +DtypeBackend: TypeAlias = Literal["pyarrow", "numpy_nullable"] + +TimeUnit: TypeAlias = Literal["s", "ms", "us", "ns"] +OpenFileErrors: TypeAlias = Literal[ + "strict", + "ignore", + "replace", + "surrogateescape", + "xmlcharrefreplace", + "backslashreplace", + "namereplace", +] + +# update +UpdateJoin: TypeAlias = Literal["left"] + +# applymap +# NaAction = Literal["ignore"] not used in stubs or pandas + +# from_dict +FromDictOrient: TypeAlias = Literal["columns", "index", "tight"] + +# to_stata +ToStataByteorder: TypeAlias = Literal[">", "<", "little", "big"] + +# ExcelWriter +ExcelWriterIfSheetExists: TypeAlias = Literal["error", "new", "replace", "overlay"] +ExcelWriterMergeCells: TypeAlias = bool | Literal["columns"] + +# Offsets +# OffsetCalendar = Union[np.busdaycalendar, "AbstractHolidayCalendar"] not used in stubs or pandas + +# read_csv: usecols +UsecolsArgType: TypeAlias = ( + SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[HashableT], bool] | None +) + +# maintain the sub-type of any hashable sequence +# SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable]) not used in stubs + +SliceType: TypeAlias = Hashable | None + +###### +## All types below this point are only used in pandas-stubs +###### + num: TypeAlias = complex -AxisInt: TypeAlias = int -AxisIndex: TypeAlias = Literal["index", 0] -AxisColumn: TypeAlias = Literal["columns", 1] -Axis: TypeAlias = AxisIndex | AxisColumn + +# AxesData is used for data for Index +AxesData: TypeAlias = Axes | dict + DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic]) KeysArgType: TypeAlias = Any -ListLike: TypeAlias = Sequence | np.ndarray | Series | Index ListLikeT = TypeVar("ListLikeT", bound=ListLike) ListLikeExceptSeriesAndStr: TypeAlias = ( MutableSequence[Any] | np.ndarray | tuple[Any, ...] | Index[Any] @@ -489,23 +808,7 @@ ListLikeHashable: TypeAlias = ( MutableSequence[HashableT] | np.ndarray | tuple[HashableT, ...] | range ) StrLike: TypeAlias = str | np.str_ -IndexIterScalar: TypeAlias = ( - str - | bytes - | datetime.date - | datetime.datetime - | datetime.timedelta - | np.datetime64 - | np.timedelta64 - | bool - | int - | float - | Timestamp - | Timedelta -) -Scalar: TypeAlias = ( - IndexIterScalar | complex | np.integer | np.floating | np.complexfloating -) + ScalarT = TypeVar("ScalarT", bound=Scalar) # Refine the definitions below in 3.9 to use the specialized type. np_ndarray_int64: TypeAlias = npt.NDArray[np.int64] @@ -518,9 +821,7 @@ np_ndarray_str: TypeAlias = npt.NDArray[np.str_] IndexType: TypeAlias = slice | np_ndarray_anyint | Index | list[int] | Series[int] MaskType: TypeAlias = Series[bool] | np_ndarray_bool | list[bool] -UsecolsArgType: TypeAlias = ( - SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[HashableT], bool] | None -) + # Scratch types for generics S1 = TypeVar( @@ -566,65 +867,6 @@ S2 = TypeVar( IndexingInt: TypeAlias = ( int | np.int_ | np.integer | np.unsignedinteger | np.signedinteger | np.int8 ) -TimestampConvertibleTypes: TypeAlias = ( - Timestamp - | datetime.datetime - | datetime.date - | np.datetime64 - | np.int64 - | float - | str -) -TimedeltaConvertibleTypes: TypeAlias = ( - Timedelta | datetime.timedelta | np.timedelta64 | np.int64 | float | str -) -# NDFrameT is stricter and ensures that the same subclass of NDFrame always is -# used. E.g. `def func(a: NDFrameT) -> NDFrameT: ...` means that if a -# Series is passed into a function, a Series is always returned and if a DataFrame is -# passed in, a DataFrame is always returned. -NDFrameT = TypeVar("NDFrameT", bound=NDFrame) - -IndexT = TypeVar("IndexT", bound=Index) - -# Interval closed type -IntervalT = TypeVar("IntervalT", bound=Interval) -IntervalClosedType: TypeAlias = Literal["left", "right", "both", "neither"] - -ScalarIndexer: TypeAlias = int | np.integer -SequenceIndexer: TypeAlias = slice | list[int] | np.ndarray -PositionalIndexer: TypeAlias = ScalarIndexer | SequenceIndexer -TakeIndexer: TypeAlias = Sequence[int] | Sequence[np.integer] | npt.NDArray[np.integer] - -RaiseCoerce: TypeAlias = Literal["raise", "coerce"] - -# Shared by functions such as drop and astype -IgnoreRaise: TypeAlias = Literal["ignore", "raise"] - -# for arbitrary kwargs passed during reading/writing files -StorageOptions: TypeAlias = dict[str, Any] | None - -# compression keywords and compression -CompressionDict: TypeAlias = dict[str, Any] -CompressionOptions: TypeAlias = ( - None | Literal["infer", "gzip", "bz2", "zip", "xz", "zstd"] | CompressionDict -) -FormattersType: TypeAlias = ( - list[Callable] | tuple[Callable, ...] | Mapping[str | int, Callable] -) -FloatFormatType: TypeAlias = str | Callable[[float], str] | EngFormatter -# converters -ConvertersArg: TypeAlias = dict[Hashable, Callable[[Dtype], Dtype]] - -# parse_dates -ParseDatesArg: TypeAlias = ( - bool | list[Hashable] | list[list[Hashable]] | dict[Hashable, list[Hashable]] -) - -# read_xml parsers -XMLParsers: TypeAlias = Literal["lxml", "etree"] - -# read_html flavors -HTMLFlavors: TypeAlias = Literal["lxml", "html5lib", "bs4"] # Any plain Python or numpy function Function: TypeAlias = np.ufunc | Callable[..., Any] @@ -699,27 +941,6 @@ StataDateFormat: TypeAlias = Literal[ "%ty", ] -FillnaOptions: TypeAlias = Literal["backfill", "bfill", "ffill", "pad"] -InterpolateOptions: TypeAlias = Literal[ - "linear", - "time", - "index", - "pad", - "nearest", - "zero", - "slinear", - "quadratic", - "cubic", - "barycentric", - "polynomial", - "krogh", - "piecewise_polynomial", - "spline", - "pchip", - "akima", - "cubicspline", - "from_derivatives", -] # Can be passed to `to_replace`, `value`, or `regex` in `Series.replace`. # `DataFrame.replace` also accepts mappings of these. ReplaceValue: TypeAlias = ( @@ -731,10 +952,7 @@ ReplaceValue: TypeAlias = ( | Series[Any] | None ) -SortKind: TypeAlias = Literal["quicksort", "mergesort", "heapsort", "stable"] -NaPosition: TypeAlias = Literal["first", "last"] -JoinHow: TypeAlias = Literal["left", "right", "outer", "inner"] -MergeHow: TypeAlias = JoinHow | Literal["cross"] + JsonFrameOrient: TypeAlias = Literal[ "split", "records", "index", "columns", "values", "table" ] @@ -742,7 +960,6 @@ JsonSeriesOrient: TypeAlias = Literal["split", "records", "index", "table"] TimestampConvention: TypeAlias = Literal["start", "end", "s", "e"] -CSVEngine: TypeAlias = Literal["c", "python", "pyarrow", "python-fwf"] # [pandas-dev/pandas-stubs/991] # Ref: https://github.com/python/cpython/blob/5a4fb7ea1c96f67dbb3df5d4ccaf3f66a1e19731/Modules/_csv.c#L88-L91 # QUOTE_MINIMAL = 0 @@ -763,12 +980,7 @@ ParquetEngine: TypeAlias = Literal["auto", "pyarrow", "fastparquet"] FileWriteMode: TypeAlias = Literal[ "a", "w", "x", "at", "wt", "xt", "ab", "wb", "xb", "w+", "w+b", "a+", "a+b" ] -ColspaceArgType: TypeAlias = ( - str | int | Sequence[int | str] | Mapping[Hashable, str | int] -) -# Windowing rank methods -WindowingRankType: TypeAlias = Literal["average", "min", "max"] WindowingEngine: TypeAlias = Literal["cython", "numba"] | None class _WindowingNumbaKwargs(TypedDict, total=False): @@ -777,9 +989,6 @@ class _WindowingNumbaKwargs(TypedDict, total=False): parallel: bool WindowingEngineKwargs: TypeAlias = _WindowingNumbaKwargs | None -QuantileInterpolation: TypeAlias = Literal[ - "linear", "lower", "higher", "midpoint", "nearest" -] class StyleExportDict(TypedDict, total=False): apply: Any @@ -793,30 +1002,6 @@ class StyleExportDict(TypedDict, total=False): CalculationMethod: TypeAlias = Literal["single", "table"] -ValidationOptions: TypeAlias = Literal[ - "one_to_one", - "1:1", - "one_to_many", - "1:m", - "many_to_one", - "m:1", - "many_to_many", - "m:m", -] - -RandomState: TypeAlias = ( - int - | ArrayLike - | np.random.Generator - | np.random.BitGenerator - | np.random.RandomState -) -Frequency: TypeAlias = str | BaseOffset -TimeUnit: TypeAlias = Literal["s", "ms", "us", "ns"] -TimeGrouperOrigin: TypeAlias = ( - Timestamp | Literal["epoch", "start", "start_day", "end", "end_day"] -) - ExcelReadEngine: TypeAlias = Literal["xlrd", "openpyxl", "odf", "pyxlsb", "calamine"] ExcelWriteEngine: TypeAlias = Literal["openpyxl", "odf", "xlsxwriter"] @@ -829,4 +1014,17 @@ IntoColumn: TypeAlias = ( AnyArrayLike | Scalar | Callable[[DataFrame], AnyArrayLike | Scalar] ) +DatetimeLike: TypeAlias = datetime.datetime | np.datetime64 | Timestamp +DateAndDatetimeLike: TypeAlias = datetime.date | DatetimeLike + +DatetimeDictArg: TypeAlias = ( + Sequence[int] | Sequence[float] | list[str] | tuple[Scalar, ...] | AnyArrayLike +) +DictConvertible: TypeAlias = FulldatetimeDict | DataFrame + +# `Incomplete` is equivalent to `Any`. Use it to annotate symbols that you don't +# know the type of yet and that should be changed in the future. Use `Any` only +# where it is the only acceptable type. +Incomplete: TypeAlias = Any + __all__ = ["npt", "type_t"] diff --git a/pandas-stubs/core/arrays/base.pyi b/pandas-stubs/core/arrays/base.pyi index d60fba4d7..6a7e2afee 100644 --- a/pandas-stubs/core/arrays/base.pyi +++ b/pandas-stubs/core/arrays/base.pyi @@ -1,4 +1,7 @@ -from typing import Any +from typing import ( + Any, + overload, +) import numpy as np from typing_extensions import Self @@ -6,6 +9,8 @@ from typing_extensions import Self from pandas._typing import ( ArrayLike, Scalar, + ScalarIndexer, + SequenceIndexer, TakeIndexer, npt, ) @@ -13,7 +18,10 @@ from pandas._typing import ( from pandas.core.dtypes.dtypes import ExtensionDtype as ExtensionDtype class ExtensionArray: - def __getitem__(self, item) -> Any: ... + @overload + def __getitem__(self, item: ScalarIndexer) -> Any: ... + @overload + def __getitem__(self, item: SequenceIndexer) -> Self: ... def __setitem__(self, key: int | slice | np.ndarray, value) -> None: ... def __len__(self) -> int: ... def __iter__(self): ... diff --git a/pandas-stubs/core/arrays/categorical.pyi b/pandas-stubs/core/arrays/categorical.pyi index cb0aeaa42..8a0396527 100644 --- a/pandas-stubs/core/arrays/categorical.pyi +++ b/pandas-stubs/core/arrays/categorical.pyi @@ -2,7 +2,10 @@ from collections.abc import ( Callable, Sequence, ) -from typing import Any +from typing import ( + Any, + overload, +) import numpy as np from pandas import Series @@ -10,13 +13,17 @@ from pandas.core.accessor import PandasDelegate as PandasDelegate from pandas.core.arrays.base import ExtensionArray as ExtensionArray from pandas.core.base import NoNewAttributesMixin as NoNewAttributesMixin from pandas.core.indexes.base import Index +from typing_extensions import Self from pandas._typing import ( ArrayLike, Dtype, ListLike, Ordered, + PositionalIndexerTuple, Scalar, + ScalarIndexer, + SequenceIndexer, TakeIndexer, np_ndarray_bool, np_ndarray_int, @@ -107,7 +114,13 @@ class Categorical(ExtensionArray): def __len__(self) -> int: ... def __iter__(self): ... def __contains__(self, key) -> bool: ... - def __getitem__(self, key): ... + @overload + def __getitem__(self, key: ScalarIndexer) -> Any: ... + @overload + def __getitem__( + self, + key: SequenceIndexer | PositionalIndexerTuple, + ) -> Self: ... def __setitem__(self, key, value) -> None: ... def min(self, *, skipna: bool = ...): ... def max(self, *, skipna: bool = ...): ... diff --git a/pandas-stubs/core/arrays/datetimelike.pyi b/pandas-stubs/core/arrays/datetimelike.pyi index cc849a108..9f31fd927 100644 --- a/pandas-stubs/core/arrays/datetimelike.pyi +++ b/pandas-stubs/core/arrays/datetimelike.pyi @@ -1,17 +1,31 @@ from collections.abc import Sequence +from typing import overload import numpy as np from pandas.core.arrays.base import ( ExtensionArray, ExtensionOpsMixin, ) -from typing_extensions import Self +from typing_extensions import ( + Self, + TypeAlias, +) from pandas._libs import ( NaT as NaT, NaTType as NaTType, ) -from pandas._typing import TimeUnit +from pandas._typing import ( + DatetimeLikeScalar, + PositionalIndexerTuple, + ScalarIndexer, + SequenceIndexer, + TimeAmbiguous, + TimeNonexistent, + TimeUnit, +) + +DTScalarOrNaT: TypeAlias = DatetimeLikeScalar | NaTType class DatelikeOps: def strftime(self, date_format): ... @@ -20,9 +34,15 @@ class TimelikeOps: @property def unit(self) -> TimeUnit: ... def as_unit(self, unit: TimeUnit) -> Self: ... - def round(self, freq, ambiguous: str = ..., nonexistent: str = ...): ... - def floor(self, freq, ambiguous: str = ..., nonexistent: str = ...): ... - def ceil(self, freq, ambiguous: str = ..., nonexistent: str = ...): ... + def round( + self, freq, ambiguous: TimeAmbiguous = ..., nonexistent: TimeNonexistent = ... + ): ... + def floor( + self, freq, ambiguous: TimeAmbiguous = ..., nonexistent: TimeNonexistent = ... + ): ... + def ceil( + self, freq, ambiguous: TimeAmbiguous = ..., nonexistent: TimeNonexistent = ... + ): ... class DatetimeLikeArrayMixin(ExtensionOpsMixin, ExtensionArray): @property @@ -40,7 +60,13 @@ class DatetimeLikeArrayMixin(ExtensionOpsMixin, ExtensionArray): @property def size(self) -> int: ... def __len__(self) -> int: ... - def __getitem__(self, key): ... + @overload + def __getitem__(self, key: ScalarIndexer) -> DTScalarOrNaT: ... + @overload + def __getitem__( + self, + key: SequenceIndexer | PositionalIndexerTuple, + ) -> Self: ... def __setitem__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] self, key: int | Sequence[int] | Sequence[bool] | slice, value ) -> None: ... diff --git a/pandas-stubs/core/arrays/datetimes.pyi b/pandas-stubs/core/arrays/datetimes.pyi index f21af1d76..762b307b1 100644 --- a/pandas-stubs/core/arrays/datetimes.pyi +++ b/pandas-stubs/core/arrays/datetimes.pyi @@ -8,6 +8,11 @@ from pandas.core.arrays.datetimelike import ( TimelikeOps, ) +from pandas._typing import ( + TimeAmbiguous, + TimeNonexistent, +) + from pandas.core.dtypes.dtypes import DatetimeTZDtype as DatetimeTZDtype def tz_to_dtype(tz): ... @@ -31,7 +36,10 @@ class DatetimeArray(DatetimeLikeArrayMixin, TimelikeOps, DatelikeOps): def astype(self, dtype, copy: bool = ...): ... def tz_convert(self, tz: TimeZones): ... def tz_localize( - self, tz: TimeZones, ambiguous: str = ..., nonexistent: str = ... + self, + tz: TimeZones, + ambiguous: TimeAmbiguous = ..., + nonexistent: TimeNonexistent = ..., ): ... def to_pydatetime(self): ... def normalize(self): ... diff --git a/pandas-stubs/core/arrays/interval.pyi b/pandas-stubs/core/arrays/interval.pyi index 9f2ad55dd..122f1cd8c 100644 --- a/pandas-stubs/core/arrays/interval.pyi +++ b/pandas-stubs/core/arrays/interval.pyi @@ -6,7 +6,10 @@ from pandas import ( Series, ) from pandas.core.arrays.base import ExtensionArray as ExtensionArray -from typing_extensions import Self +from typing_extensions import ( + Self, + TypeAlias, +) from pandas._libs.interval import ( Interval as Interval, @@ -15,10 +18,14 @@ from pandas._libs.interval import ( from pandas._typing import ( Axis, Scalar, + ScalarIndexer, + SequenceIndexer, TakeIndexer, np_ndarray_bool, ) +IntervalOrNA: TypeAlias = Interval | float + class IntervalArray(IntervalMixin, ExtensionArray): can_hold_na: bool = ... def __new__( @@ -34,7 +41,10 @@ class IntervalArray(IntervalMixin, ExtensionArray): def from_tuples(cls, data, closed: str = ..., copy: bool = ..., dtype=...): ... def __iter__(self): ... def __len__(self) -> int: ... - def __getitem__(self, value): ... + @overload + def __getitem__(self, key: ScalarIndexer) -> IntervalOrNA: ... + @overload + def __getitem__(self, key: SequenceIndexer) -> Self: ... def __setitem__(self, key, value) -> None: ... def __eq__(self, other): ... def __ne__(self, other): ... diff --git a/pandas-stubs/core/arrays/masked.pyi b/pandas-stubs/core/arrays/masked.pyi index 0ef41bc7a..ae33bc845 100644 --- a/pandas-stubs/core/arrays/masked.pyi +++ b/pandas-stubs/core/arrays/masked.pyi @@ -1,16 +1,27 @@ +from typing import ( + Any, + overload, +) + import numpy as np from pandas.core.arrays import ( ExtensionArray as ExtensionArray, ExtensionOpsMixin, ) +from typing_extensions import Self from pandas._typing import ( Scalar, + ScalarIndexer, + SequenceIndexer, npt, ) class BaseMaskedArray(ExtensionArray, ExtensionOpsMixin): - def __getitem__(self, item): ... + @overload + def __getitem__(self, item: ScalarIndexer) -> Any: ... + @overload + def __getitem__(self, item: SequenceIndexer) -> Self: ... def __iter__(self): ... def __len__(self) -> int: ... def __invert__(self): ... diff --git a/pandas-stubs/core/arrays/sparse/array.pyi b/pandas-stubs/core/arrays/sparse/array.pyi index 71b861606..1e4b1e728 100644 --- a/pandas-stubs/core/arrays/sparse/array.pyi +++ b/pandas-stubs/core/arrays/sparse/array.pyi @@ -1,8 +1,23 @@ +from enum import Enum +from typing import ( + Any, + overload, +) + import numpy as np from pandas.core.arrays import ( ExtensionArray, ExtensionOpsMixin, ) +from typing_extensions import Self + +from pandas._typing import ( + ScalarIndexer, + SequenceIndexer, +) + +class ellipsis(Enum): + Ellipsis = "..." class SparseArray(ExtensionArray, ExtensionOpsMixin): def __init__( @@ -42,7 +57,13 @@ class SparseArray(ExtensionArray, ExtensionOpsMixin): def shift(self, periods: int = ..., fill_value=...): ... def unique(self): ... def value_counts(self, dropna: bool = ...): ... - def __getitem__(self, key): ... + @overload + def __getitem__(self, key: ScalarIndexer) -> Any: ... + @overload + def __getitem__( + self, + key: SequenceIndexer | tuple[int | ellipsis, ...], + ) -> Self: ... def copy(self): ... def astype(self, dtype=..., copy: bool = ...): ... def map(self, mapper): ... diff --git a/pandas-stubs/core/base.pyi b/pandas-stubs/core/base.pyi index 1eb65d784..e49bd43c9 100644 --- a/pandas-stubs/core/base.pyi +++ b/pandas-stubs/core/base.pyi @@ -23,7 +23,7 @@ from typing_extensions import Self from pandas._typing import ( S1, AxisIndex, - NaPosition, + DropKeep, NDFrameT, Scalar, npt, @@ -110,4 +110,4 @@ class IndexOpsMixin(OpsMixin, Generic[S1]): def searchsorted( self, value, side: Literal["left", "right"] = ..., sorter=... ) -> int | list[int]: ... - def drop_duplicates(self, *, keep: NaPosition | Literal[False] = ...) -> Self: ... + def drop_duplicates(self, *, keep: DropKeep = ...) -> Self: ... diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 39bde496d..72e341c21 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -81,6 +81,8 @@ from pandas._typing import ( AggFuncTypeDictFrame, AggFuncTypeDictSeries, AggFuncTypeFrame, + AlignJoin, + AnyAll, AnyArrayLike, ArrayLike, AstypeArg, @@ -91,6 +93,7 @@ from pandas._typing import ( CalculationMethod, ColspaceArgType, CompressionOptions, + DropKeep, Dtype, FilePath, FillnaOptions, @@ -102,14 +105,16 @@ from pandas._typing import ( HashableT3, IgnoreRaise, IndexingInt, + IndexKeyFunc, IndexLabel, IndexType, InterpolateOptions, IntervalClosedType, IntervalT, IntoColumn, - JoinHow, + JoinValidate, JsonFrameOrient, + JSONSerializable, Label, Level, ListLike, @@ -117,12 +122,15 @@ from pandas._typing import ( ListLikeU, MaskType, MergeHow, + MergeValidate, NaPosition, NDFrameT, + NsmallestNlargestKeep, ParquetEngine, QuantileInterpolation, RandomState, ReadBuffer, + ReindexMethod, Renamer, ReplaceValue, Scalar, @@ -136,9 +144,13 @@ from pandas._typing import ( StrLike, Suffixes, T as _T, - TimestampConvention, + TimeAmbiguous, + TimeNonexistent, TimeUnit, - ValidationOptions, + ToStataByteorder, + ToTimestampHow, + UpdateJoin, + ValueKeyFunc, WriteBuffer, XMLParsers, npt, @@ -476,7 +488,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): *, convert_dates: dict[HashableT1, StataDateFormat] | None = ..., write_index: _bool = ..., - byteorder: Literal["<", ">", "little", "big"] | None = ..., + byteorder: ToStataByteorder | None = ..., time_stamp: dt.datetime | None = ..., data_label: _str | None = ..., variable_labels: dict[HashableT2, str] | None = ..., @@ -785,7 +797,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def align( self, other: NDFrameT, - join: JoinHow = ..., + join: AlignJoin = ..., axis: Axis | None = ..., level: Level | None = ..., copy: _bool = ..., @@ -797,7 +809,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): index: Axes | None = ..., columns: Axes | None = ..., axis: Axis | None = ..., - method: FillnaOptions | Literal["nearest"] | None = ..., + method: ReindexMethod | None = ..., copy: bool = ..., level: int | _str = ..., fill_value: Scalar | None = ..., @@ -940,7 +952,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, *, axis: Axis = ..., - how: Literal["any", "all"] = ..., + how: AnyAll = ..., thresh: int | None = ..., subset: ListLikeU | Scalar | None = ..., inplace: Literal[True], @@ -951,7 +963,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, *, axis: Axis = ..., - how: Literal["any", "all"] = ..., + how: AnyAll = ..., thresh: int | None = ..., subset: ListLikeU | Scalar | None = ..., inplace: Literal[False] = ..., @@ -962,7 +974,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, subset: Hashable | Iterable[Hashable] | None = ..., *, - keep: NaPosition | _bool = ..., + keep: DropKeep = ..., inplace: Literal[True], ignore_index: _bool = ..., ) -> None: ... @@ -971,14 +983,14 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, subset: Hashable | Iterable[Hashable] | None = ..., *, - keep: NaPosition | _bool = ..., + keep: DropKeep = ..., inplace: Literal[False] = ..., ignore_index: _bool = ..., ) -> Self: ... def duplicated( self, subset: Hashable | Iterable[Hashable] | None = ..., - keep: NaPosition | _bool = ..., + keep: DropKeep = ..., ) -> Series: ... @overload def sort_values( @@ -991,7 +1003,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): na_position: NaPosition = ..., ignore_index: _bool = ..., inplace: Literal[True], - key: Callable | None = ..., + key: ValueKeyFunc = ..., ) -> None: ... @overload def sort_values( @@ -1004,7 +1016,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): na_position: NaPosition = ..., ignore_index: _bool = ..., inplace: Literal[False] = ..., - key: Callable | None = ..., + key: ValueKeyFunc = ..., ) -> Self: ... @overload def sort_index( @@ -1018,7 +1030,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): sort_remaining: _bool = ..., ignore_index: _bool = ..., inplace: Literal[True], - key: Callable | None = ..., + key: IndexKeyFunc = ..., ) -> None: ... @overload def sort_index( @@ -1032,7 +1044,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): sort_remaining: _bool = ..., ignore_index: _bool = ..., inplace: Literal[False] = ..., - key: Callable | None = ..., + key: IndexKeyFunc = ..., ) -> Self: ... @overload def value_counts( @@ -1056,13 +1068,13 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, n: int, columns: _str | list[_str], - keep: NaPosition | Literal["all"] = ..., + keep: NsmallestNlargestKeep = ..., ) -> Self: ... def nsmallest( self, n: int, columns: _str | list[_str], - keep: NaPosition | Literal["all"] = ..., + keep: NsmallestNlargestKeep = ..., ) -> Self: ... def swaplevel(self, i: Level = ..., j: Level = ..., axis: Axis = ...) -> Self: ... def reorder_levels(self, order: list, axis: Axis = ...) -> Self: ... @@ -1085,7 +1097,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def update( self, other: DataFrame | Series, - join: _str = ..., + join: UpdateJoin = ..., overwrite: _bool = ..., filter_func: Callable | None = ..., errors: IgnoreRaise = ..., @@ -1520,7 +1532,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): lsuffix: _str = ..., rsuffix: _str = ..., sort: _bool = ..., - validate: ValidationOptions | None = ..., + validate: JoinValidate | None = ..., ) -> Self: ... def merge( self, @@ -1532,10 +1544,10 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): left_index: _bool = ..., right_index: _bool = ..., sort: _bool = ..., - suffixes: tuple[_str | None, _str | None] = ..., + suffixes: Suffixes = ..., copy: _bool = ..., indicator: _bool | _str = ..., - validate: _str | None = ..., + validate: MergeValidate | None = ..., ) -> Self: ... def round( self, decimals: int | dict | Series = ..., *args: Any, **kwargs: Any @@ -1599,7 +1611,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def to_timestamp( self, freq=..., - how: TimestampConvention = ..., + how: ToTimestampHow = ..., axis: Axis = ..., copy: _bool = ..., ) -> Self: ... @@ -2325,9 +2337,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): double_precision: int = ..., force_ascii: _bool = ..., date_unit: TimeUnit = ..., - default_handler: ( - Callable[[Any], _str | float | _bool | list | dict] | None - ) = ..., + default_handler: Callable[[Any], JSONSerializable] | None = ..., lines: Literal[True], compression: CompressionOptions = ..., index: _bool = ..., @@ -2344,9 +2354,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): double_precision: int = ..., force_ascii: _bool = ..., date_unit: TimeUnit = ..., - default_handler: ( - Callable[[Any], _str | float | _bool | list | dict] | None - ) = ..., + default_handler: Callable[[Any], JSONSerializable] | None = ..., lines: Literal[True], compression: CompressionOptions = ..., index: _bool = ..., @@ -2362,9 +2370,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): double_precision: int = ..., force_ascii: _bool = ..., date_unit: TimeUnit = ..., - default_handler: ( - Callable[[Any], _str | float | _bool | list | dict] | None - ) = ..., + default_handler: Callable[[Any], JSONSerializable] | None = ..., lines: _bool = ..., compression: CompressionOptions = ..., index: _bool = ..., @@ -2380,9 +2386,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): double_precision: int = ..., force_ascii: _bool = ..., date_unit: TimeUnit = ..., - default_handler: ( - Callable[[Any], _str | float | _bool | list | dict] | None - ) = ..., + default_handler: Callable[[Any], JSONSerializable] | None = ..., lines: _bool = ..., compression: CompressionOptions = ..., index: _bool = ..., @@ -2463,8 +2467,8 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): axis: Axis = ..., level: Level | None = ..., copy: _bool = ..., - ambiguous=..., - nonexistent: _str = ..., + ambiguous: TimeAmbiguous = ..., + nonexistent: TimeNonexistent = ..., ) -> Self: ... def var( self, diff --git a/pandas-stubs/core/generic.pyi b/pandas-stubs/core/generic.pyi index c8edf865b..5da21bc33 100644 --- a/pandas-stubs/core/generic.pyi +++ b/pandas-stubs/core/generic.pyi @@ -34,6 +34,7 @@ from pandas._typing import ( CSVQuoting, DtypeArg, DtypeBackend, + ExcelWriterMergeCells, FilePath, FileWriteMode, Frequency, @@ -43,14 +44,15 @@ from pandas._typing import ( IgnoreRaise, IndexLabel, Level, + OpenFileErrors, P, StorageOptions, T, TakeIndexer, TimedeltaConvertibleTypes, TimeGrouperOrigin, - TimestampConvention, TimestampConvertibleTypes, + ToTimestampHow, WriteBuffer, ) @@ -105,7 +107,7 @@ class NDFrame(indexing.IndexingMixin): startrow: int = ..., startcol: int = ..., engine: _str | None = ..., - merge_cells: _bool = ..., + merge_cells: ExcelWriterMergeCells = ..., inf_rep: _str = ..., freeze_panes: tuple[int, int] | None = ..., ) -> None: ... @@ -124,15 +126,7 @@ class NDFrame(indexing.IndexingMixin): nan_rep: _str | None = ..., dropna: _bool | None = ..., data_columns: Literal[True] | list[HashableT2] | None = ..., - errors: Literal[ - "strict", - "ignore", - "replace", - "surrogateescape", - "xmlcharrefreplace", - "backslashreplace", - "namereplace", - ] = ..., + errors: OpenFileErrors = ..., encoding: _str = ..., ) -> None: ... @overload @@ -276,7 +270,7 @@ class NDFrame(indexing.IndexingMixin): doublequote: _bool = ..., escapechar: _str | None = ..., decimal: _str = ..., - errors: _str = ..., + errors: OpenFileErrors = ..., storage_options: StorageOptions = ..., ) -> None: ... @overload @@ -301,7 +295,7 @@ class NDFrame(indexing.IndexingMixin): doublequote: _bool = ..., escapechar: _str | None = ..., decimal: _str = ..., - errors: _str = ..., + errors: OpenFileErrors = ..., storage_options: StorageOptions = ..., ) -> _str: ... def __delitem__(self, idx: Hashable) -> None: ... @@ -411,7 +405,7 @@ class NDFrame(indexing.IndexingMixin): axis: Axis | NoDefault = ..., closed: Literal["right", "left"] | None = ..., label: Literal["right", "left"] | None = ..., - convention: TimestampConvention = ..., + convention: ToTimestampHow = ..., kind: Literal["period", "timestamp"] | None = ..., on: Level | None = ..., level: Level | None = ..., diff --git a/pandas-stubs/core/groupby/generic.pyi b/pandas-stubs/core/groupby/generic.pyi index a687956cf..f618a5922 100644 --- a/pandas-stubs/core/groupby/generic.pyi +++ b/pandas-stubs/core/groupby/generic.pyi @@ -39,6 +39,7 @@ from pandas._typing import ( IndexLabel, Level, ListLike, + NsmallestNlargestKeep, Scalar, TakeIndexer, WindowingEngine, @@ -123,10 +124,10 @@ class SeriesGroupBy(GroupBy[Series[S1]], Generic[S1, ByT]): @property def plot(self) -> GroupByPlot[Self]: ... def nlargest( - self, n: int = ..., keep: Literal["first", "last", "all"] = ... + self, n: int = ..., keep: NsmallestNlargestKeep = ... ) -> Series[S1]: ... def nsmallest( - self, n: int = ..., keep: Literal["first", "last", "all"] = ... + self, n: int = ..., keep: NsmallestNlargestKeep = ... ) -> Series[S1]: ... def idxmin(self, skipna: bool = ...) -> Series: ... def idxmax(self, skipna: bool = ...) -> Series: ... diff --git a/pandas-stubs/core/indexes/accessors.pyi b/pandas-stubs/core/indexes/accessors.pyi index 735da79f9..5bee1f1e9 100644 --- a/pandas-stubs/core/indexes/accessors.pyi +++ b/pandas-stubs/core/indexes/accessors.pyi @@ -36,6 +36,8 @@ from pandas.core.series import ( from pandas._libs.tslibs import BaseOffset from pandas._libs.tslibs.offsets import DateOffset from pandas._typing import ( + TimeAmbiguous, + TimeNonexistent, TimestampConvention, TimeUnit, np_ndarray_bool, @@ -218,12 +220,8 @@ class _DatetimeLikeNoTZMethods( def tz_localize( self, tz: TimeZones, - ambiguous: Literal["raise", "infer", "NaT"] | np_ndarray_bool = ..., - nonexistent: ( - Literal["shift_forward", "shift_backward", "NaT", "raise"] - | timedelta - | Timedelta - ) = ..., + ambiguous: TimeAmbiguous = ..., + nonexistent: TimeNonexistent = ..., ) -> _DTNormalizeReturnType: ... def tz_convert(self, tz: TimeZones) -> _DTNormalizeReturnType: ... def normalize(self) -> _DTNormalizeReturnType: ... diff --git a/pandas-stubs/core/indexes/base.pyi b/pandas-stubs/core/indexes/base.pyi index d7961af00..b2a714b56 100644 --- a/pandas-stubs/core/indexes/base.pyi +++ b/pandas-stubs/core/indexes/base.pyi @@ -42,7 +42,9 @@ from typing_extensions import ( from pandas._libs.interval import _OrderableT from pandas._typing import ( S1, - Axes, + AnyAll, + AxesData, + DropKeep, Dtype, DtypeArg, DtypeObj, @@ -51,7 +53,8 @@ from pandas._typing import ( Label, Level, MaskType, - NaPosition, + ReindexMethod, + SliceType, TimedeltaDtypeArg, TimestampDtypeArg, np_ndarray_anyint, @@ -82,7 +85,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes, + data: AxesData, *, dtype: Literal["int"] | type_t[int | np.integer], copy: bool = ..., @@ -104,7 +107,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes, + data: AxesData, *, dtype: Literal["float"] | type_t[float | np.floating], copy: bool = ..., @@ -130,7 +133,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes, + data: AxesData, *, dtype: Literal["complex"] | type_t[complex | np.complexfloating], copy: bool = ..., @@ -153,7 +156,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes, + data: AxesData, *, dtype: TimestampDtypeArg, copy: bool = ..., @@ -175,7 +178,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes, + data: AxesData, *, dtype: PeriodDtype, copy: bool = ..., @@ -197,7 +200,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes, + data: AxesData, *, dtype: TimedeltaDtypeArg, copy: bool = ..., @@ -219,7 +222,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes, + data: AxesData, *, dtype: Literal["Interval"], copy: bool = ..., @@ -242,7 +245,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes = ..., + data: AxesData = ..., *, dtype: type[S1], copy: bool = ..., @@ -254,7 +257,7 @@ class Index(IndexOpsMixin[S1]): @overload def __new__( cls, - data: Axes, + data: AxesData, *, dtype=..., copy: bool = ..., @@ -333,12 +336,10 @@ class Index(IndexOpsMixin[S1]): def notna(self): ... notnull = ... def fillna(self, value=...): ... - def dropna(self, how: Literal["any", "all"] = ...) -> Self: ... + def dropna(self, how: AnyAll = ...) -> Self: ... def unique(self, level=...) -> Self: ... - def drop_duplicates(self, *, keep: NaPosition | Literal[False] = ...) -> Self: ... - def duplicated( - self, keep: Literal["first", "last", False] = ... - ) -> np_ndarray_bool: ... + def drop_duplicates(self, *, keep: DropKeep = ...) -> Self: ... + def duplicated(self, keep: DropKeep = ...) -> np_ndarray_bool: ... def __and__(self, other: Never) -> Never: ... def __rand__(self, other: Never) -> Never: ... def __or__(self, other: Never) -> Never: ... @@ -360,8 +361,17 @@ class Index(IndexOpsMixin[S1]): method: FillnaOptions | Literal["nearest"] | None = ..., tolerance=..., ) -> int | slice | np_ndarray_bool: ... - def get_indexer(self, target, method=..., limit=..., tolerance=...): ... - def reindex(self, target, method=..., level=..., limit=..., tolerance=...): ... + def get_indexer( + self, target, method: ReindexMethod | None = ..., limit=..., tolerance=... + ): ... + def reindex( + self, + target, + method: ReindexMethod | None = ..., + level=..., + limit=..., + tolerance=..., + ): ... def join( self, other, @@ -403,7 +413,7 @@ class Index(IndexOpsMixin[S1]): def isin(self, values, level=...) -> np_ndarray_bool: ... def slice_indexer(self, start=..., end=..., step=...): ... def get_slice_bound(self, label, side): ... - def slice_locs(self, start=..., end=..., step=...): ... + def slice_locs(self, start: SliceType = ..., end: SliceType = ..., step=...): ... def delete(self, loc) -> Self: ... def insert(self, loc, item) -> Self: ... def drop(self, labels, errors: _str = ...) -> Self: ... diff --git a/pandas-stubs/core/indexes/datetimes.pyi b/pandas-stubs/core/indexes/datetimes.pyi index 3c1e85243..9a5334da2 100644 --- a/pandas-stubs/core/indexes/datetimes.pyi +++ b/pandas-stubs/core/indexes/datetimes.pyi @@ -10,7 +10,7 @@ from datetime import ( from typing import overload from _typing import ( - Axes, + AxesData, Frequency, TimeZones, ) @@ -44,7 +44,7 @@ from pandas.tseries.offsets import BaseOffset class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties): def __init__( self, - data: Axes, + data: AxesData, freq: Frequency = ..., tz: TimeZones = ..., ambiguous: str = ..., diff --git a/pandas-stubs/core/indexes/multi.pyi b/pandas-stubs/core/indexes/multi.pyi index 0bb872908..3ab3d6862 100644 --- a/pandas-stubs/core/indexes/multi.pyi +++ b/pandas-stubs/core/indexes/multi.pyi @@ -6,7 +6,6 @@ from collections.abc import ( ) from typing import ( Any, - Literal, overload, ) @@ -17,7 +16,9 @@ from pandas.core.indexes.base import Index from typing_extensions import Self from pandas._typing import ( + AnyAll, Axes, + DropKeep, Dtype, DtypeArg, HashableT, @@ -103,8 +104,8 @@ class MultiIndex(Index[Any]): def is_monotonic_increasing(self) -> bool: ... @property def is_monotonic_decreasing(self) -> bool: ... - def duplicated(self, keep: Literal["first", "last", False] = ...): ... - def dropna(self, how: Literal["any", "all"] = ...) -> Self: ... + def duplicated(self, keep: DropKeep = ...): ... + def dropna(self, how: AnyAll = ...) -> Self: ... def get_level_values(self, level: str | int) -> Index: ... def unique(self, level=...): ... def to_frame( @@ -148,7 +149,6 @@ class MultiIndex(Index[Any]): def get_slice_bound( self, label: Hashable | Sequence[Hashable], side: str ) -> int: ... - def slice_locs(self, start=..., end=..., step=...): ... def get_loc_level(self, key, level=..., drop_level: bool = ...): ... def get_locs(self, seq): ... def truncate(self, before=..., after=...): ... diff --git a/pandas-stubs/core/indexes/timedeltas.pyi b/pandas-stubs/core/indexes/timedeltas.pyi index 1371f5dc8..6cb94df77 100644 --- a/pandas-stubs/core/indexes/timedeltas.pyi +++ b/pandas-stubs/core/indexes/timedeltas.pyi @@ -27,7 +27,7 @@ from pandas._libs import ( ) from pandas._libs.tslibs import BaseOffset from pandas._typing import ( - Axes, + AxesData, TimedeltaConvertibleTypes, num, ) @@ -35,7 +35,9 @@ from pandas._typing import ( class TimedeltaIndex(DatetimeTimedeltaMixin[Timedelta], TimedeltaIndexProperties): def __new__( cls, - data: Sequence[dt.timedelta | Timedelta | np.timedelta64 | float] | Axes = ..., + data: ( + Sequence[dt.timedelta | Timedelta | np.timedelta64 | float] | AxesData + ) = ..., freq: str | BaseOffset = ..., closed: object = ..., dtype: Literal[" DataFrame: ... @@ -45,9 +44,7 @@ def merge_ordered( left_by: Label | list[HashableT] | None = ..., right_by: Label | list[HashableT] | None = ..., fill_method: Literal["ffill"] | None = ..., - suffixes: ( - list[str | None] | tuple[str, str] | tuple[None, str] | tuple[str, None] - ) = ..., + suffixes: Suffixes = ..., how: JoinHow = ..., ) -> DataFrame: ... @overload @@ -75,9 +72,7 @@ def merge_ordered( left_by: None = ..., right_by: None = ..., fill_method: Literal["ffill"] | None = ..., - suffixes: ( - list[str | None] | tuple[str, str] | tuple[None, str] | tuple[str, None] - ) = ..., + suffixes: Suffixes = ..., how: JoinHow = ..., ) -> DataFrame: ... def merge_asof( @@ -91,9 +86,7 @@ def merge_asof( by: Label | list[HashableT] | None = ..., left_by: Label | list[HashableT] | None = ..., right_by: Label | list[HashableT] | None = ..., - suffixes: ( - list[str | None] | tuple[str, str] | tuple[None, str] | tuple[str, None] - ) = ..., + suffixes: Suffixes = ..., tolerance: int | timedelta | Timedelta | None = ..., allow_exact_matches: bool = ..., direction: Literal["backward", "forward", "nearest"] = ..., diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 3e607e65a..af786ffd1 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -28,6 +28,7 @@ from _typing import ( FloatFormatType, Label, ReplaceValue, + Suffixes, TimeZones, ) from matplotlib.axes import ( @@ -110,6 +111,7 @@ from pandas._typing import ( AggFuncTypeBase, AggFuncTypeDictFrame, AggFuncTypeSeriesToFrame, + AnyAll, AnyArrayLike, ArrayLike, Axes, @@ -122,6 +124,7 @@ from pandas._typing import ( CategoryDtypeArg, ComplexDtypeArg, CompressionOptions, + DropKeep, Dtype, DtypeObj, FilePath, @@ -131,21 +134,25 @@ from pandas._typing import ( HashableT1, IgnoreRaise, IndexingInt, + IndexKeyFunc, IndexLabel, IntDtypeArg, InterpolateOptions, IntervalClosedType, IntervalT, JoinHow, + JSONSerializable, JsonSeriesOrient, Level, ListLike, ListLikeU, MaskType, NaPosition, + NsmallestNlargestKeep, ObjectDtypeArg, QuantileInterpolation, RandomState, + ReindexMethod, Renamer, Scalar, ScalarT, @@ -155,11 +162,13 @@ from pandas._typing import ( StrDtypeArg, StrLike, T, + TimeAmbiguous, TimedeltaDtypeArg, - TimestampConvention, TimestampDtypeArg, TimeUnit, + ToTimestampHow, UIntDtypeArg, + ValueKeyFunc, VoidDtypeArg, WriteBuffer, np_ndarray_anyint, @@ -547,9 +556,7 @@ class Series(IndexOpsMixin[S1], NDFrame): double_precision: int = ..., force_ascii: _bool = ..., date_unit: TimeUnit = ..., - default_handler: ( - Callable[[Any], _str | float | _bool | list | dict] | None - ) = ..., + default_handler: Callable[[Any], JSONSerializable] | None = ..., lines: Literal[True], compression: CompressionOptions = ..., index: _bool = ..., @@ -566,9 +573,7 @@ class Series(IndexOpsMixin[S1], NDFrame): double_precision: int = ..., force_ascii: _bool = ..., date_unit: TimeUnit = ..., - default_handler: ( - Callable[[Any], _str | float | _bool | list | dict] | None - ) = ..., + default_handler: Callable[[Any], JSONSerializable] | None = ..., lines: Literal[True], compression: CompressionOptions = ..., index: _bool = ..., @@ -584,9 +589,7 @@ class Series(IndexOpsMixin[S1], NDFrame): double_precision: int = ..., force_ascii: _bool = ..., date_unit: TimeUnit = ..., - default_handler: ( - Callable[[Any], _str | float | _bool | list | dict] | None - ) = ..., + default_handler: Callable[[Any], JSONSerializable] | None = ..., lines: _bool = ..., compression: CompressionOptions = ..., index: _bool = ..., @@ -602,9 +605,7 @@ class Series(IndexOpsMixin[S1], NDFrame): double_precision: int = ..., force_ascii: _bool = ..., date_unit: TimeUnit = ..., - default_handler: ( - Callable[[Any], _str | float | _bool | list | dict] | None - ) = ..., + default_handler: Callable[[Any], JSONSerializable] | None = ..., lines: _bool = ..., compression: CompressionOptions = ..., index: _bool = ..., @@ -753,7 +754,7 @@ class Series(IndexOpsMixin[S1], NDFrame): def drop_duplicates( self, *, - keep: NaPosition | Literal[False] = ..., + keep: DropKeep = ..., inplace: Literal[True], ignore_index: _bool = ..., ) -> None: ... @@ -761,11 +762,11 @@ class Series(IndexOpsMixin[S1], NDFrame): def drop_duplicates( self, *, - keep: NaPosition | Literal[False] = ..., + keep: DropKeep = ..., inplace: Literal[False] = ..., ignore_index: _bool = ..., ) -> Series[S1]: ... - def duplicated(self, keep: NaPosition | Literal[False] = ...) -> Series[_bool]: ... + def duplicated(self, keep: DropKeep = ...) -> Series[_bool]: ... def idxmax( self, axis: AxisIndex = ..., skipna: _bool = ..., *args: Any, **kwargs: Any ) -> int | _str: ... @@ -848,6 +849,7 @@ class Series(IndexOpsMixin[S1], NDFrame): align_axis: AxisIndex, keep_shape: bool = ..., keep_equal: bool = ..., + result_names: Suffixes = ..., ) -> UnknownSeries: ... @overload def compare( @@ -856,6 +858,7 @@ class Series(IndexOpsMixin[S1], NDFrame): align_axis: AxisColumn = ..., keep_shape: bool = ..., keep_equal: bool = ..., + result_names: Suffixes = ..., ) -> DataFrame: ... def combine( self, other: Series[S1], func: Callable, fill_value: Scalar | None = ... @@ -872,7 +875,7 @@ class Series(IndexOpsMixin[S1], NDFrame): na_position: NaPosition = ..., ignore_index: _bool = ..., inplace: Literal[True], - key: Callable | None = ..., + key: ValueKeyFunc = ..., ) -> None: ... @overload def sort_values( @@ -884,7 +887,7 @@ class Series(IndexOpsMixin[S1], NDFrame): na_position: NaPosition = ..., ignore_index: _bool = ..., inplace: Literal[False] = ..., - key: Callable | None = ..., + key: ValueKeyFunc = ..., ) -> Series[S1]: ... @overload def sort_index( @@ -898,7 +901,7 @@ class Series(IndexOpsMixin[S1], NDFrame): sort_remaining: _bool = ..., ignore_index: _bool = ..., inplace: Literal[True], - key: Callable | None = ..., + key: IndexKeyFunc = ..., ) -> None: ... @overload def sort_index( @@ -912,7 +915,7 @@ class Series(IndexOpsMixin[S1], NDFrame): sort_remaining: _bool = ..., ignore_index: _bool = ..., inplace: Literal[False] = ..., - key: Callable | None = ..., + key: IndexKeyFunc = ..., ) -> Series[S1]: ... def argsort( self, @@ -921,10 +924,10 @@ class Series(IndexOpsMixin[S1], NDFrame): order: None = ..., ) -> Series[int]: ... def nlargest( - self, n: int = ..., keep: NaPosition | Literal["all"] = ... + self, n: int = ..., keep: NsmallestNlargestKeep = ... ) -> Series[S1]: ... def nsmallest( - self, n: int = ..., keep: NaPosition | Literal["all"] = ... + self, n: int = ..., keep: NsmallestNlargestKeep = ... ) -> Series[S1]: ... def swaplevel( self, i: Level = ..., j: Level = ..., copy: _bool = ... @@ -1150,7 +1153,7 @@ class Series(IndexOpsMixin[S1], NDFrame): *, axis: AxisIndex = ..., inplace: Literal[True], - how: Literal["any", "all"] | None = ..., + how: AnyAll | None = ..., ignore_index: _bool = ..., ) -> None: ... @overload @@ -1159,13 +1162,13 @@ class Series(IndexOpsMixin[S1], NDFrame): *, axis: AxisIndex = ..., inplace: Literal[False] = ..., - how: Literal["any", "all"] | None = ..., + how: AnyAll | None = ..., ignore_index: _bool = ..., ) -> Series[S1]: ... def to_timestamp( self, freq=..., - how: TimestampConvention = ..., + how: ToTimestampHow = ..., copy: _bool = ..., ) -> Series[S1]: ... def to_period(self, freq: _str | None = ..., copy: _bool = ...) -> DataFrame: ... @@ -1213,7 +1216,7 @@ class Series(IndexOpsMixin[S1], NDFrame): def reindex( self, index: Axes | None = ..., - method: FillnaOptions | Literal["nearest"] | None = ..., + method: ReindexMethod | None = ..., copy: bool = ..., level: int | _str = ..., fill_value: Scalar | None = ..., @@ -1536,7 +1539,7 @@ class Series(IndexOpsMixin[S1], NDFrame): axis: AxisIndex = ..., level: Level | None = ..., copy: _bool = ..., - ambiguous=..., + ambiguous: TimeAmbiguous = ..., nonexistent: _str = ..., ) -> Series[S1]: ... def abs(self) -> Series[S1]: ... diff --git a/pandas-stubs/core/strings.pyi b/pandas-stubs/core/strings.pyi index 4d215e82a..16eae157d 100644 --- a/pandas-stubs/core/strings.pyi +++ b/pandas-stubs/core/strings.pyi @@ -25,7 +25,7 @@ from pandas.core.base import NoNewAttributesMixin from pandas._libs.tslibs.nattype import NaTType from pandas._typing import ( - JoinHow, + AlignJoin, Scalar, T, np_ndarray_bool, @@ -59,7 +59,7 @@ class StringMethods( *, sep: str, na_rep: str | None = ..., - join: JoinHow = ..., + join: AlignJoin = ..., ) -> str: ... @overload def cat( @@ -68,7 +68,7 @@ class StringMethods( *, sep: str, na_rep: str | None = ..., - join: JoinHow = ..., + join: AlignJoin = ..., ) -> str: ... @overload def cat( @@ -78,7 +78,7 @@ class StringMethods( ), sep: str = ..., na_rep: str | None = ..., - join: JoinHow = ..., + join: AlignJoin = ..., ) -> _T_STR: ... @overload def split( diff --git a/pandas-stubs/io/excel/_base.pyi b/pandas-stubs/io/excel/_base.pyi index 5b9d5a9c4..8187a8702 100644 --- a/pandas-stubs/io/excel/_base.pyi +++ b/pandas-stubs/io/excel/_base.pyi @@ -25,6 +25,7 @@ from pandas._typing import ( DtypeBackend, ExcelReadEngine, ExcelWriteEngine, + ExcelWriterIfSheetExists, FilePath, IntStrT, ListLikeHashable, @@ -213,7 +214,7 @@ class ExcelWriter: datetime_format: str | None = ..., mode: Literal["w", "a"] = ..., storage_options: StorageOptions = ..., - if_sheet_exists: Literal["error", "new", "replace", "overlay"] | None = ..., + if_sheet_exists: ExcelWriterIfSheetExists | None = ..., engine_kwargs: dict[str, Any] | None = ..., ) -> None: ... @property diff --git a/pandas-stubs/io/formats/style.pyi b/pandas-stubs/io/formats/style.pyi index 295363421..917144823 100644 --- a/pandas-stubs/io/formats/style.pyi +++ b/pandas-stubs/io/formats/style.pyi @@ -17,6 +17,7 @@ from pandas.core.series import Series from pandas._typing import ( Axis, + ExcelWriterMergeCells, FilePath, HashableT, HashableT1, @@ -93,7 +94,7 @@ class Styler(StylerRenderer): startrow: int = ..., startcol: int = ..., engine: Literal["openpyxl", "xlsxwriter"] | None = ..., - merge_cells: bool = ..., + merge_cells: ExcelWriterMergeCells = ..., encoding: str | None = ..., inf_rep: str = ..., verbose: bool = ..., diff --git a/tests/test_series.py b/tests/test_series.py index ac6d897e0..43a98d0bf 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -20,7 +20,6 @@ TypedDict, TypeVar, Union, - cast, ) import numpy as np @@ -1680,7 +1679,7 @@ def test_cat_ctor_values() -> None: assert_type(pd.Categorical(pd.Series(["a", "b", "a"])), pd.Categorical), pd.Categorical, ) - s: Sequence = cast(Sequence, ["a", "b", "a"]) + s = ["a", "b", "a"] check(assert_type(pd.Categorical(s), pd.Categorical), pd.Categorical) # GH 107 check(