Skip to content

Commit 324c079

Browse files
Dr-Irvtwoertwein
authored andcommitted
Fix issue for pandera allowing generic Series to work (pandas-dev#492)
* split Series[IntervalT] into separate __new__ * add test for generic series * Update reference to GH issue
1 parent 9f52314 commit 324c079

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

pandas-stubs/core/series.pyi

+33-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ from typing_extensions import (
7171
)
7272
import xarray as xr
7373

74+
from pandas._libs.interval import Interval
7475
from pandas._libs.missing import NAType
7576
from pandas._libs.tslibs import BaseOffset
7677
from pandas._typing import (
@@ -95,7 +96,6 @@ from pandas._typing import (
9596
IgnoreRaise,
9697
IndexingInt,
9798
IntervalClosedType,
98-
IntervalT,
9999
JoinHow,
100100
JsonSeriesOrient,
101101
Level,
@@ -217,13 +217,43 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
217217
@overload
218218
def __new__(
219219
cls,
220-
data: IntervalIndex[IntervalT],
220+
data: IntervalIndex[Interval[int]],
221221
index: Axes | None = ...,
222222
dtype=...,
223223
name: Hashable | None = ...,
224224
copy: bool = ...,
225225
fastpath: bool = ...,
226-
) -> Series[IntervalT]: ...
226+
) -> Series[Interval[int]]: ...
227+
@overload
228+
def __new__(
229+
cls,
230+
data: IntervalIndex[Interval[float]],
231+
index: Axes | None = ...,
232+
dtype=...,
233+
name: Hashable | None = ...,
234+
copy: bool = ...,
235+
fastpath: bool = ...,
236+
) -> Series[Interval[float]]: ...
237+
@overload
238+
def __new__(
239+
cls,
240+
data: IntervalIndex[Interval[Timestamp]],
241+
index: Axes | None = ...,
242+
dtype=...,
243+
name: Hashable | None = ...,
244+
copy: bool = ...,
245+
fastpath: bool = ...,
246+
) -> Series[Interval[Timestamp]]: ...
247+
@overload
248+
def __new__(
249+
cls,
250+
data: IntervalIndex[Interval[Timedelta]],
251+
index: Axes | None = ...,
252+
dtype=...,
253+
name: Hashable | None = ...,
254+
copy: bool = ...,
255+
fastpath: bool = ...,
256+
) -> Series[Interval[Timedelta]]: ...
227257
@overload
228258
def __new__(
229259
cls,

tests/test_series.py

+15
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
TYPE_CHECKING,
88
Any,
99
Dict,
10+
Generic,
1011
Hashable,
1112
Iterable,
1213
Iterator,
1314
List,
1415
Sequence,
16+
TypeVar,
1517
cast,
1618
)
1719

@@ -1362,3 +1364,16 @@ def test_AnyArrayLike_and_clip() -> None:
13621364
s2 = ser.clip(upper=ser)
13631365
check(assert_type(s1, pd.Series), pd.Series)
13641366
check(assert_type(s2, pd.Series), pd.Series)
1367+
1368+
1369+
def test_pandera_generic() -> None:
1370+
# GH 471
1371+
T = TypeVar("T")
1372+
1373+
class MySeries(pd.Series, Generic[T]):
1374+
...
1375+
1376+
def func() -> MySeries[float]:
1377+
return MySeries[float]([1, 2, 3])
1378+
1379+
func()

0 commit comments

Comments
 (0)