Skip to content

Commit 7531ef8

Browse files
authored
use UnknownSeries in series.pyi (#1177)
* use UnknownSeries in series.pyi * add `test_unknown`
1 parent d572c11 commit 7531ef8

File tree

2 files changed

+65
-36
lines changed

2 files changed

+65
-36
lines changed

pandas-stubs/core/series.pyi

+55-36
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ from pandas.core.indexes.accessors import (
6464
TimedeltaProperties,
6565
TimestampProperties,
6666
)
67+
from pandas.core.indexes.base import UnknownIndex
6768
from pandas.core.indexes.category import CategoricalIndex
6869
from pandas.core.indexes.datetimes import DatetimeIndex
6970
from pandas.core.indexes.interval import IntervalIndex
@@ -258,7 +259,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
258259
dtype: Dtype = ...,
259260
name: Hashable = ...,
260261
copy: bool = ...,
261-
) -> Series[Any]: ...
262+
) -> UnknownSeries: ...
262263
@overload
263264
def __new__(
264265
cls,
@@ -424,7 +425,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
424425
dtype: Dtype = ...,
425426
name: Hashable = ...,
426427
copy: bool = ...,
427-
) -> Series: ...
428+
) -> UnknownSeries: ...
428429
@property
429430
def hasnans(self) -> bool: ...
430431
def div(
@@ -748,7 +749,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
748749
@overload
749750
def groupby(
750751
self,
751-
by: CategoricalIndex | Index | Series,
752+
by: CategoricalIndex | UnknownIndex | UnknownSeries,
752753
axis: AxisIndex = ...,
753754
level: IndexLabel | None = ...,
754755
as_index: _bool = ...,
@@ -831,15 +832,15 @@ class Series(IndexOpsMixin[S1], NDFrame):
831832
self, other: ArrayLike | dict[_str, np.ndarray] | Sequence[S1] | Index[S1]
832833
) -> np.ndarray: ...
833834
@overload
834-
def __matmul__(self, other: Series) -> Scalar: ...
835+
def __matmul__(self, other: UnknownSeries) -> Scalar: ...
835836
@overload
836-
def __matmul__(self, other: DataFrame) -> Series: ...
837+
def __matmul__(self, other: DataFrame) -> UnknownSeries: ...
837838
@overload
838839
def __matmul__(self, other: np.ndarray) -> np.ndarray: ...
839840
@overload
840-
def __rmatmul__(self, other: Series) -> Scalar: ...
841+
def __rmatmul__(self, other: UnknownSeries) -> Scalar: ...
841842
@overload
842-
def __rmatmul__(self, other: DataFrame) -> Series: ...
843+
def __rmatmul__(self, other: DataFrame) -> UnknownSeries: ...
843844
@overload
844845
def __rmatmul__(self, other: np.ndarray) -> np.ndarray: ...
845846
@overload
@@ -859,15 +860,15 @@ class Series(IndexOpsMixin[S1], NDFrame):
859860
@overload
860861
def compare(
861862
self,
862-
other: Series,
863+
other: UnknownSeries,
863864
align_axis: AxisIndex,
864865
keep_shape: bool = ...,
865866
keep_equal: bool = ...,
866-
) -> Series: ...
867+
) -> UnknownSeries: ...
867868
@overload
868869
def compare(
869870
self,
870-
other: Series,
871+
other: UnknownSeries,
871872
align_axis: AxisColumn = ...,
872873
keep_shape: bool = ...,
873874
keep_equal: bool = ...,
@@ -986,7 +987,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
986987
axis: AxisIndex = ...,
987988
*args: Any,
988989
**kwargs: Any,
989-
) -> Series: ...
990+
) -> UnknownSeries: ...
990991
agg = aggregate
991992
@overload
992993
def transform(
@@ -1013,7 +1014,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
10131014
convertDType: _bool = ...,
10141015
args: tuple = ...,
10151016
**kwargs: Any,
1016-
) -> Series: ...
1017+
) -> UnknownSeries: ...
10171018
@overload
10181019
def apply(
10191020
self,
@@ -1025,20 +1026,20 @@ class Series(IndexOpsMixin[S1], NDFrame):
10251026
@overload
10261027
def apply(
10271028
self,
1028-
func: Callable[..., Series],
1029+
func: Callable[..., UnknownSeries],
10291030
convertDType: _bool = ...,
10301031
args: tuple = ...,
10311032
**kwargs: Any,
10321033
) -> DataFrame: ...
10331034
def align(
10341035
self,
1035-
other: DataFrame | Series,
1036+
other: DataFrame | UnknownSeries,
10361037
join: JoinHow = ...,
10371038
axis: Axis | None = ...,
10381039
level: Level | None = ...,
10391040
copy: _bool = ...,
10401041
fill_value: Scalar | NAType | None = ...,
1041-
) -> tuple[Series, Series]: ...
1042+
) -> tuple[UnknownSeries, UnknownSeries]: ...
10421043
@overload
10431044
def rename(
10441045
self,
@@ -1133,7 +1134,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
11331134
freq: Frequency | timedelta | None = ...,
11341135
axis: AxisIndex = ...,
11351136
fill_value: object | None = ...,
1136-
) -> Series: ...
1137+
) -> UnknownSeries: ...
11371138
def info(
11381139
self,
11391140
verbose: bool | None = ...,
@@ -1506,11 +1507,15 @@ class Series(IndexOpsMixin[S1], NDFrame):
15061507
tuple[
15071508
Sequence[bool]
15081509
| Series[bool]
1509-
| Callable[[Series], Series | np.ndarray | Sequence[bool]],
1510-
ListLikeU | Scalar | Callable[[Series], Series | np.ndarray],
1510+
| Callable[
1511+
[UnknownSeries], UnknownSeries | np.ndarray | Sequence[bool]
1512+
],
1513+
ListLikeU
1514+
| Scalar
1515+
| Callable[[UnknownSeries], UnknownSeries | np.ndarray],
15111516
],
15121517
],
1513-
) -> Series: ...
1518+
) -> UnknownSeries: ...
15141519
def truncate(
15151520
self,
15161521
before: date | _str | int | None = ...,
@@ -1581,8 +1586,16 @@ class Series(IndexOpsMixin[S1], NDFrame):
15811586
@overload
15821587
def __add__(
15831588
self,
1584-
other: num | _str | timedelta | Timedelta | _ListLike | Series | np.timedelta64,
1585-
) -> Series: ...
1589+
other: (
1590+
num
1591+
| _str
1592+
| timedelta
1593+
| Timedelta
1594+
| _ListLike
1595+
| UnknownSeries
1596+
| np.timedelta64
1597+
),
1598+
) -> UnknownSeries: ...
15861599
# ignore needed for mypy as we want different results based on the arguments
15871600
@overload # type: ignore[override]
15881601
def __and__( # pyright: ignore[reportOverlappingOverload]
@@ -1613,7 +1626,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
16131626
self, other: timedelta | Timedelta | TimedeltaSeries | np.timedelta64
16141627
) -> TimedeltaSeries: ...
16151628
@overload
1616-
def __mul__(self, other: num | _ListLike | Series) -> Series: ...
1629+
def __mul__(self, other: num | _ListLike | UnknownSeries) -> UnknownSeries: ...
16171630
def __mod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
16181631
def __ne__(self, other: object) -> Series[_bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
16191632
def __pow__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
@@ -1629,7 +1642,9 @@ class Series(IndexOpsMixin[S1], NDFrame):
16291642
@overload
16301643
def __radd__(self, other: S1 | Series[S1]) -> Self: ...
16311644
@overload
1632-
def __radd__(self, other: num | _str | _ListLike | Series) -> Series: ...
1645+
def __radd__(
1646+
self, other: num | _str | _ListLike | UnknownSeries
1647+
) -> UnknownSeries: ...
16331648
# ignore needed for mypy as we want different results based on the arguments
16341649
@overload # type: ignore[override]
16351650
def __rand__( # pyright: ignore[reportOverlappingOverload]
@@ -1648,7 +1663,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
16481663
self, other: timedelta | Timedelta | TimedeltaSeries | np.timedelta64
16491664
) -> TimedeltaSeries: ...
16501665
@overload
1651-
def __rmul__(self, other: num | _ListLike | Series) -> Series: ...
1666+
def __rmul__(self, other: num | _ListLike | UnknownSeries) -> UnknownSeries: ...
16521667
def __rnatmul__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
16531668
def __rpow__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
16541669
# ignore needed for mypy as we want different results based on the arguments
@@ -1660,8 +1675,10 @@ class Series(IndexOpsMixin[S1], NDFrame):
16601675
def __ror__( # pyright: ignore[reportIncompatibleMethodOverride]
16611676
self, other: int | np_ndarray_anyint | Series[int]
16621677
) -> Series[int]: ...
1663-
def __rsub__(self, other: num | _ListLike | Series[S1]) -> Series: ...
1664-
def __rtruediv__(self, other: num | _ListLike | Series[S1] | Path) -> Series: ...
1678+
def __rsub__(self, other: num | _ListLike | Series[S1]) -> UnknownSeries: ...
1679+
def __rtruediv__(
1680+
self, other: num | _ListLike | Series[S1] | Path
1681+
) -> UnknownSeries: ...
16651682
# ignore needed for mypy as we want different results based on the arguments
16661683
@overload # type: ignore[override]
16671684
def __rxor__( # pyright: ignore[reportOverlappingOverload]
@@ -1686,8 +1703,10 @@ class Series(IndexOpsMixin[S1], NDFrame):
16861703
self, other: Timestamp | datetime | TimestampSeries
16871704
) -> TimedeltaSeries: ...
16881705
@overload
1689-
def __sub__(self, other: num | _ListLike | Series) -> Series: ...
1690-
def __truediv__(self, other: num | _ListLike | Series[S1] | Path) -> Series: ...
1706+
def __sub__(self, other: num | _ListLike | UnknownSeries) -> UnknownSeries: ...
1707+
def __truediv__(
1708+
self, other: num | _ListLike | Series[S1] | Path
1709+
) -> UnknownSeries: ...
16911710
# ignore needed for mypy as we want different results based on the arguments
16921711
@overload # type: ignore[override]
16931712
def __xor__( # pyright: ignore[reportOverlappingOverload]
@@ -1792,12 +1811,12 @@ class Series(IndexOpsMixin[S1], NDFrame):
17921811
min_periods: int = ...,
17931812
adjust: _bool = ...,
17941813
ignore_na: _bool = ...,
1795-
) -> ExponentialMovingWindow[Series]: ...
1814+
) -> ExponentialMovingWindow[UnknownSeries]: ...
17961815
def expanding(
17971816
self,
17981817
min_periods: int = ...,
17991818
method: CalculationMethod = ...,
1800-
) -> Expanding[Series]: ...
1819+
) -> Expanding[UnknownSeries]: ...
18011820
def floordiv(
18021821
self,
18031822
other: num | _ListLike | Series[S1],
@@ -1900,11 +1919,11 @@ class Series(IndexOpsMixin[S1], NDFrame):
19001919
@overload
19011920
def mul(
19021921
self,
1903-
other: num | _ListLike | Series,
1922+
other: num | _ListLike | UnknownSeries,
19041923
level: Level | None = ...,
19051924
fill_value: float | None = ...,
19061925
axis: AxisIndex | None = ...,
1907-
) -> Series: ...
1926+
) -> UnknownSeries: ...
19081927
def multiply(
19091928
self,
19101929
other: num | _ListLike | Series[S1],
@@ -1984,11 +2003,11 @@ class Series(IndexOpsMixin[S1], NDFrame):
19842003
@overload
19852004
def rmul(
19862005
self,
1987-
other: num | _ListLike | Series,
2006+
other: num | _ListLike | UnknownSeries,
19882007
level: Level | None = ...,
19892008
fill_value: float | None = ...,
19902009
axis: AxisIndex = ...,
1991-
) -> Series: ...
2010+
) -> UnknownSeries: ...
19922011
@overload
19932012
def rolling(
19942013
self,
@@ -2001,7 +2020,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
20012020
method: CalculationMethod = ...,
20022021
*,
20032022
win_type: _str,
2004-
) -> Window[Series]: ...
2023+
) -> Window[UnknownSeries]: ...
20052024
@overload
20062025
def rolling(
20072026
self,
@@ -2014,7 +2033,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
20142033
method: CalculationMethod = ...,
20152034
*,
20162035
win_type: None = ...,
2017-
) -> Rolling[Series]: ...
2036+
) -> Rolling[UnknownSeries]: ...
20182037
def rpow(
20192038
self,
20202039
other: Series[S1] | Scalar,

tests/test_series.py

+10
Original file line numberDiff line numberDiff line change
@@ -3589,3 +3589,13 @@ def test_align() -> None:
35893589

35903590
check(assert_type(aligned_s0, pd.Series), pd.Series)
35913591
check(assert_type(aligned_s1, pd.Series), pd.Series)
3592+
3593+
3594+
def test_unknown() -> None:
3595+
s = pd.Series(pd.NA)
3596+
3597+
def foo(sf: pd.Series) -> None:
3598+
pass
3599+
3600+
foo(s)
3601+
check(assert_type(s + pd.Series([1]), pd.Series), pd.Series)

0 commit comments

Comments
 (0)