Skip to content

Commit

Permalink
Fix issue for pandera allowing generic Series to work (pandas-dev#492)
Browse files Browse the repository at this point in the history
* split Series[IntervalT] into separate __new__

* add test for generic series

* Update reference to GH issue
  • Loading branch information
Dr-Irv authored and twoertwein committed Apr 1, 2023
1 parent 9f52314 commit 324c079
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
36 changes: 33 additions & 3 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ from typing_extensions import (
)
import xarray as xr

from pandas._libs.interval import Interval
from pandas._libs.missing import NAType
from pandas._libs.tslibs import BaseOffset
from pandas._typing import (
Expand All @@ -95,7 +96,6 @@ from pandas._typing import (
IgnoreRaise,
IndexingInt,
IntervalClosedType,
IntervalT,
JoinHow,
JsonSeriesOrient,
Level,
Expand Down Expand Up @@ -217,13 +217,43 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
@overload
def __new__(
cls,
data: IntervalIndex[IntervalT],
data: IntervalIndex[Interval[int]],
index: Axes | None = ...,
dtype=...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> Series[IntervalT]: ...
) -> Series[Interval[int]]: ...
@overload
def __new__(
cls,
data: IntervalIndex[Interval[float]],
index: Axes | None = ...,
dtype=...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> Series[Interval[float]]: ...
@overload
def __new__(
cls,
data: IntervalIndex[Interval[Timestamp]],
index: Axes | None = ...,
dtype=...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> Series[Interval[Timestamp]]: ...
@overload
def __new__(
cls,
data: IntervalIndex[Interval[Timedelta]],
index: Axes | None = ...,
dtype=...,
name: Hashable | None = ...,
copy: bool = ...,
fastpath: bool = ...,
) -> Series[Interval[Timedelta]]: ...
@overload
def __new__(
cls,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
TYPE_CHECKING,
Any,
Dict,
Generic,
Hashable,
Iterable,
Iterator,
List,
Sequence,
TypeVar,
cast,
)

Expand Down Expand Up @@ -1362,3 +1364,16 @@ def test_AnyArrayLike_and_clip() -> None:
s2 = ser.clip(upper=ser)
check(assert_type(s1, pd.Series), pd.Series)
check(assert_type(s2, pd.Series), pd.Series)


def test_pandera_generic() -> None:
# GH 471
T = TypeVar("T")

class MySeries(pd.Series, Generic[T]):
...

def func() -> MySeries[float]:
return MySeries[float]([1, 2, 3])

func()

0 comments on commit 324c079

Please sign in to comment.