From 0aca7c2f125a0d20de5c1c768630097310ef098d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 31 May 2023 09:15:25 -0400 Subject: [PATCH 1/3] Fix Series.__new__ --- pandas-stubs/core/series.pyi | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 07ada220c..344b98315 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -104,6 +104,7 @@ from pandas._typing import ( CategoryDtypeArg, ComplexDtypeArg, CompressionOptions, + Dtype, DtypeBackend, DtypeObj, FilePath, @@ -208,55 +209,52 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): _ListLike: TypeAlias = ArrayLike | dict[_str, np.ndarray] | list | tuple | Index __hash__: ClassVar[None] + # TODO: can __new__ be converted to __init__? Pandas implements __init__ @overload def __new__( cls, data: DatetimeIndex | Sequence[Timestamp | np.datetime64 | datetime], index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> TimestampSeries: ... @overload - def __new__( + def __new__( # type: ignore[misc] cls, data: _ListLike, - dtype: Literal["datetime64[ns]"], index: Axes | None = ..., + *, + dtype: Literal["datetime64[ns]"], name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> TimestampSeries: ... @overload def __new__( cls, data: PeriodIndex, index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> PeriodSeries: ... @overload def __new__( cls, data: TimedeltaIndex | Sequence[Timedelta | np.timedelta64 | timedelta], index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> TimedeltaSeries: ... @overload def __new__( cls, data: IntervalIndex[Interval[int]] | Interval[int] | Sequence[Interval[int]], index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> IntervalSeries[int]: ... @overload def __new__( @@ -265,10 +263,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): | Interval[float] | Sequence[Interval[float]], index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> IntervalSeries[float]: ... @overload def __new__( @@ -277,10 +274,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): | Interval[Timestamp] | Sequence[Interval[Timestamp]], index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> IntervalSeries[Timestamp]: ... @overload def __new__( @@ -289,10 +285,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): | Interval[Timedelta] | Sequence[Interval[Timedelta]], index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> IntervalSeries[Timedelta]: ... @overload def __new__( @@ -302,27 +297,24 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): index: Axes | None = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> Self: ... @overload def __new__( cls, data: Series[S1] | dict[int, S1] | dict[_str, S1] = ..., index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> Self: ... @overload def __new__( cls, data: object | _ListLike | None = ..., index: Axes | None = ..., - dtype=..., + dtype: Dtype = ..., name: Hashable | None = ..., copy: bool = ..., - fastpath: bool = ..., ) -> Series: ... @property def hasnans(self) -> bool: ... From 434d02b22774006d91bcef9d769431e311a9d238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 1 Jun 2023 20:07:13 -0400 Subject: [PATCH 2/3] dtype + bound --- pandas-stubs/_libs/interval.pyi | 6 ++-- pandas-stubs/_typing.pyi | 48 ++++++++++++++----------------- pandas-stubs/core/series.pyi | 50 +++++++-------------------------- 3 files changed, 34 insertions(+), 70 deletions(-) diff --git a/pandas-stubs/_libs/interval.pyi b/pandas-stubs/_libs/interval.pyi index 82c9b751d..0dd40c964 100644 --- a/pandas-stubs/_libs/interval.pyi +++ b/pandas-stubs/_libs/interval.pyi @@ -27,9 +27,9 @@ from pandas._typing import ( VALID_CLOSED: frozenset[str] -_OrderableScalarT = TypeVar("_OrderableScalarT", int, float) -_OrderableTimesT = TypeVar("_OrderableTimesT", Timestamp, Timedelta) -_OrderableT = TypeVar("_OrderableT", int, float, Timestamp, Timedelta) +_OrderableScalarT = TypeVar("_OrderableScalarT", bound=int | float) +_OrderableTimesT = TypeVar("_OrderableTimesT", bound=Timestamp | Timedelta) +_OrderableT = TypeVar("_OrderableT", bound=int | float | Timestamp | Timedelta) class _LengthDescriptor: @overload diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 0caf99b53..9c6ba7bad 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -360,13 +360,7 @@ NDFrameT = TypeVar("NDFrameT", bound=NDFrame) IndexT = TypeVar("IndexT", bound=Index) # Interval closed type -IntervalT = TypeVar( - "IntervalT", - Interval[int], - Interval[float], - Interval[Timestamp], - Interval[Timedelta], -) +IntervalT = TypeVar("IntervalT", bound=Interval) IntervalClosedType: TypeAlias = Literal["left", "right", "both", "neither"] TakeIndexer: TypeAlias = Sequence[int] | Sequence[np.integer] | npt.NDArray[np.integer] @@ -407,26 +401,26 @@ Function: TypeAlias = np.ufunc | Callable[..., Any] _HashableTa = TypeVar("_HashableTa", bound=Hashable) ByT = TypeVar( "ByT", - str, - bytes, - datetime.date, - datetime.datetime, - datetime.timedelta, - np.datetime64, - np.timedelta64, - bool, - int, - float, - complex, - Timestamp, - Timedelta, - Scalar, - Period, - Interval[int], - Interval[float], - Interval[Timestamp], - Interval[Timedelta], - tuple, + bound=str + | bytes + | datetime.date + | datetime.datetime + | datetime.timedelta + | np.datetime64 + | np.timedelta64 + | bool + | int + | float + | complex + | Timestamp + | Timedelta + | Scalar + | Period + | Interval[int] + | Interval[float] + | Interval[Timestamp] + | Interval[Timedelta] + | tuple, ) GroupByObjectNonScalar: TypeAlias = ( tuple diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index d28b539b2..c1f70dc62 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -27,6 +27,7 @@ from matplotlib.axes import ( import numpy as np from pandas import ( Period, + PeriodDtype, Timedelta, Timestamp, ) @@ -215,12 +216,12 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): cls, data: DatetimeIndex | Sequence[Timestamp | np.datetime64 | datetime], index: Axes | None = ..., - dtype: Dtype = ..., + dtype: TimestampDtypeArg = ..., name: Hashable | None = ..., copy: bool = ..., ) -> TimestampSeries: ... @overload - def __new__( # type: ignore[misc] + def __new__( cls, data: _ListLike, index: Axes | None = ..., @@ -234,7 +235,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): cls, data: PeriodIndex, index: Axes | None = ..., - dtype: Dtype = ..., + dtype: PeriodDtype = ..., name: Hashable | None = ..., copy: bool = ..., ) -> PeriodSeries: ... @@ -243,52 +244,21 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): cls, data: TimedeltaIndex | Sequence[Timedelta | np.timedelta64 | timedelta], index: Axes | None = ..., - dtype: Dtype = ..., + dtype: TimedeltaDtypeArg = ..., name: Hashable | None = ..., copy: bool = ..., ) -> TimedeltaSeries: ... @overload def __new__( cls, - data: IntervalIndex[Interval[int]] | Interval[int] | Sequence[Interval[int]], - index: Axes | None = ..., - dtype: Dtype = ..., - name: Hashable | None = ..., - copy: bool = ..., - ) -> IntervalSeries[int]: ... - @overload - def __new__( - cls, - data: IntervalIndex[Interval[float]] - | Interval[float] - | Sequence[Interval[float]], - index: Axes | None = ..., - dtype: Dtype = ..., - name: Hashable | None = ..., - copy: bool = ..., - ) -> IntervalSeries[float]: ... - @overload - def __new__( - cls, - data: IntervalIndex[Interval[Timestamp]] - | Interval[Timestamp] - | Sequence[Interval[Timestamp]], + data: IntervalIndex[Interval[_OrderableT]] + | Interval[_OrderableT] + | Sequence[Interval[_OrderableT]], index: Axes | None = ..., - dtype: Dtype = ..., - name: Hashable | None = ..., - copy: bool = ..., - ) -> IntervalSeries[Timestamp]: ... - @overload - def __new__( - cls, - data: IntervalIndex[Interval[Timedelta]] - | Interval[Timedelta] - | Sequence[Interval[Timedelta]], - index: Axes | None = ..., - dtype: Dtype = ..., + dtype: Literal["Interval"] = ..., name: Hashable | None = ..., copy: bool = ..., - ) -> IntervalSeries[Timedelta]: ... + ) -> IntervalSeries[_OrderableT]: ... @overload def __new__( cls, From 367de215f9e14be5060b7da28ba3cfacb587244c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 4 Jun 2023 16:48:33 -0400 Subject: [PATCH 3/3] Literal -> TimestampDtypeArg --- pandas-stubs/core/series.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index d3873b189..ae6daa5da 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -227,7 +227,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): data: _ListLike, index: Axes | None = ..., *, - dtype: Literal["datetime64[ns]"], + dtype: TimestampDtypeArg, name: Hashable | None = ..., copy: bool = ..., ) -> TimestampSeries: ...