Skip to content

Commit ea5d6bf

Browse files
committed
series
1 parent e2482cb commit ea5d6bf

File tree

5 files changed

+101
-130
lines changed

5 files changed

+101
-130
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from typing import (
1616
ClassVar,
1717
Generic,
1818
Literal,
19+
TypeAlias,
1920
final,
2021
overload,
2122
type_check_only,
@@ -29,7 +30,9 @@ from _typeshed import (
2930
_T_contra,
3031
)
3132
import numpy as np
33+
from pandas.core.arrays.base import ExtensionArray
3234
from pandas.core.arrays.boolean import BooleanArray
35+
from pandas.core.arrays.timedeltas import TimedeltaArray
3336
from pandas.core.base import (
3437
ElementOpsMixin,
3538
IndexOpsMixin,
@@ -111,6 +114,32 @@ from pandas._typing import (
111114

112115
from pandas.core.dtypes.dtypes import PeriodDtype
113116

117+
NumpyRealScalar: TypeAlias = np.bool | np.integer | np.floating
118+
IndexReal: TypeAlias = Index[bool] | Index[int] | Index[float] # ty: ignore[unresolved-reference]
119+
ScalarArrayIndexReal: TypeAlias = (
120+
float
121+
| Sequence[float | NumpyRealScalar]
122+
| NumpyRealScalar
123+
| np_ndarray[tuple[int, ...], NumpyRealScalar]
124+
| ExtensionArray # TODO: NumpyExtensionArray after pandas-dev/pandas-stubs#1469
125+
| IndexReal
126+
)
127+
NumpyComplexScalar: TypeAlias = NumpyRealScalar | np.complexfloating
128+
IndexComplex: TypeAlias = IndexReal | Index[complex] # ty: ignore[unresolved-reference]
129+
ScalarArrayIndexComplex: TypeAlias = (
130+
complex
131+
| Sequence[complex | NumpyComplexScalar]
132+
| NumpyComplexScalar
133+
| np_ndarray[tuple[int, ...], NumpyComplexScalar]
134+
| ExtensionArray # TODO: NumpyExtensionArray after pandas-dev/pandas-stubs#1469
135+
| IndexComplex
136+
)
137+
138+
ArrayIndexTimedeltaNoSeq: TypeAlias = np_ndarray_td | TimedeltaArray | TimedeltaIndex
139+
ScalarArrayIndexTimedelta: TypeAlias = (
140+
timedelta | Sequence[timedelta | np.timedelta64] | ArrayIndexTimedeltaNoSeq
141+
)
142+
114143
class InvalidIndexError(Exception): ...
115144

116145
class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
@@ -1124,7 +1153,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
11241153
),
11251154
) -> Index: ...
11261155
@overload
1127-
def __rfloordiv__(self: Index[bool] | Index[int] | Index[float], other: Index[Never]) -> Index: ... # type: ignore[overload-overlap]
1156+
def __rfloordiv__( # type: ignore[overload-overlap]
1157+
self: Index[bool] | Index[int] | Index[float], other: Index[Never]
1158+
) -> Index: ...
11281159
@overload
11291160
def __rfloordiv__(
11301161
self: Index[int] | Index[float],

pandas-stubs/core/series.pyi

Lines changed: 48 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ from pandas.core.groupby.generic import SeriesGroupBy
8383
from pandas.core.groupby.groupby import BaseGroupBy
8484
from pandas.core.indexers import BaseIndexer
8585
from pandas.core.indexes.accessors import DtDescriptor
86+
from pandas.core.indexes.base import (
87+
ArrayIndexTimedeltaNoSeq,
88+
ScalarArrayIndexComplex,
89+
ScalarArrayIndexReal,
90+
ScalarArrayIndexTimedelta,
91+
)
8692
from pandas.core.indexes.category import CategoricalIndex
8793
from pandas.core.indexes.datetimes import DatetimeIndex
8894
from pandas.core.indexes.interval import IntervalIndex
@@ -302,30 +308,15 @@ _DataLike: TypeAlias = ArrayLike | dict[str, np.ndarray] | SequenceNotStr[S1]
302308
_DataLikeS1: TypeAlias = (
303309
ArrayLike | dict[_str, np.ndarray] | Sequence[S1] | IndexOpsMixin[S1]
304310
)
305-
_OtherFloorDivNumPyGeneric: TypeAlias = np.bool | np.integer | np.floating
306-
_OtherFloorDiv: TypeAlias = (
307-
float
308-
| _OtherFloorDivNumPyGeneric
309-
| Sequence[float | _OtherFloorDivNumPyGeneric]
310-
| np_ndarray[tuple[int, ...], _OtherFloorDivNumPyGeneric]
311-
| Index[bool]
312-
| Index[int]
313-
| Index[float]
314-
| Series[bool]
315-
| Series[int]
316-
| Series[float]
317-
)
318-
_OtherTrueDivNumPyGeneric: TypeAlias = _OtherFloorDivNumPyGeneric | np.complex128
319-
_OtherTrueDiv: TypeAlias = (
320-
_OtherFloorDiv
321-
| complex
322-
| _OtherTrueDivNumPyGeneric
323-
| Sequence[complex | _OtherTrueDivNumPyGeneric]
324-
| np_ndarray_complex
325-
| Index[complex]
326-
| Series[complex]
311+
312+
SeriesReal: TypeAlias = Series[bool] | Series[int] | Series[float] # ty: ignore[unresolved-reference]
313+
ScalarArrayIndexSeriesReal: TypeAlias = ScalarArrayIndexReal | SeriesReal
314+
SeriesComplex: TypeAlias = SeriesReal | Series[complex] # ty: ignore[unresolved-reference]
315+
ScalarArrayIndexSeriesComplex: TypeAlias = ScalarArrayIndexComplex | SeriesComplex
316+
ArrayIndexSeriesTimedeltaNoSeq: TypeAlias = ArrayIndexTimedeltaNoSeq | Series[Timedelta] # ty: ignore[unresolved-reference]
317+
ScalarArrayIndexSeriesTimedelta: TypeAlias = (
318+
ScalarArrayIndexTimedelta | Series[Timedelta] # ty: ignore[unresolved-reference]
327319
)
328-
_OtherRTrueDiv: TypeAlias = _OtherTrueDiv
329320

330321
class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
331322
# Define __index__ because mypy thinks Series follows protocol `SupportsIndex` https://github.com/pandas-dev/pandas-stubs/pull/1332#discussion_r2285648790
@@ -2183,7 +2174,9 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
21832174
other: np_ndarray_complex | np_ndarray_dt | np_ndarray_td,
21842175
) -> Never: ...
21852176
@overload
2186-
def __floordiv__(self: Series[Never], other: _OtherFloorDiv) -> Series: ...
2177+
def __floordiv__(
2178+
self: Series[Never], other: ScalarArrayIndexSeriesReal
2179+
) -> Series: ...
21872180
@overload
21882181
def __floordiv__(
21892182
self: Series[bool] | Series[int] | Series[float] | Series[Timedelta],
@@ -2244,8 +2237,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
22442237
) -> Series[Timedelta]: ...
22452238
@overload
22462239
def __floordiv__(
2247-
self: Series[Timedelta],
2248-
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
2240+
self: Series[Timedelta], other: ArrayIndexSeriesTimedeltaNoSeq
22492241
) -> Series[int]: ...
22502242
@overload
22512243
def floordiv(
@@ -2258,7 +2250,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
22582250
@overload
22592251
def floordiv(
22602252
self: Series[Never],
2261-
other: _OtherFloorDiv,
2253+
other: ScalarArrayIndexSeriesReal,
22622254
level: Level | None = ...,
22632255
fill_value: float | None = None,
22642256
axis: AxisIndex | None = 0,
@@ -2342,27 +2334,14 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
23422334
@overload
23432335
def floordiv(
23442336
self: Series[Timedelta],
2345-
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
2337+
other: ArrayIndexSeriesTimedeltaNoSeq,
23462338
level: Level | None = ...,
23472339
fill_value: float | None = None,
23482340
axis: AxisIndex | None = 0,
23492341
) -> Series[int]: ...
23502342
@overload
2351-
def __rfloordiv__( # type: ignore[overload-overlap]
2352-
self: Series[Never],
2353-
other: (
2354-
float
2355-
| Sequence[float]
2356-
| np_ndarray_bool
2357-
| np_ndarray_anyint
2358-
| np_ndarray_float
2359-
| Index[bool]
2360-
| Index[int]
2361-
| Index[float]
2362-
| Series[bool]
2363-
| Series[int]
2364-
| Series[float]
2365-
),
2343+
def __rfloordiv__(
2344+
self: Series[Never], other: ScalarArrayIndexSeriesReal
23662345
) -> Series: ...
23672346
@overload
23682347
def __rfloordiv__(self, other: np_ndarray_complex | np_ndarray_dt) -> Never: ...
@@ -2422,29 +2401,16 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
24222401
@overload
24232402
def __rfloordiv__(
24242403
self: Series[int] | Series[float],
2425-
other: timedelta | np_ndarray_td | TimedeltaIndex | Series[Timedelta],
2404+
other: timedelta | np.timedelta64 | ArrayIndexSeriesTimedeltaNoSeq,
24262405
) -> Series[Timedelta]: ...
24272406
@overload
24282407
def __rfloordiv__(
2429-
self: Series[Timedelta],
2430-
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
2408+
self: Series[Timedelta], other: ArrayIndexSeriesTimedeltaNoSeq
24312409
) -> Series[int]: ...
24322410
@overload
24332411
def rfloordiv(
24342412
self: Series[Never],
2435-
other: (
2436-
float
2437-
| Sequence[float]
2438-
| np_ndarray_bool
2439-
| np_ndarray_anyint
2440-
| np_ndarray_float
2441-
| Index[bool]
2442-
| Index[int]
2443-
| Index[float]
2444-
| Series[bool]
2445-
| Series[int]
2446-
| Series[float]
2447-
),
2413+
other: ScalarArrayIndexSeriesReal,
24482414
level: Level | None = ...,
24492415
fill_value: float | None = None,
24502416
axis: AxisIndex | None = 0,
@@ -2524,7 +2490,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
25242490
@overload
25252491
def rfloordiv(
25262492
self: Series[Timedelta],
2527-
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
2493+
other: ArrayIndexSeriesTimedeltaNoSeq,
25282494
level: Level | None = ...,
25292495
fill_value: float | None = None,
25302496
axis: AxisIndex = ...,
@@ -3641,19 +3607,17 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
36413607
def __truediv__(self, other: np_ndarray_dt) -> Never: ...
36423608
@overload
36433609
def __truediv__( # type: ignore[overload-overlap]
3644-
self: Series[Never], other: _OtherTrueDiv
3610+
self: Series[Never], other: ScalarArrayIndexSeriesComplex
36453611
) -> Series: ...
36463612
@overload
3647-
def __truediv__(
3648-
self: Series[Never], other: np_ndarray_td | TimedeltaIndex
3649-
) -> Never: ...
3613+
def __truediv__(self: Series[Never], other: ArrayIndexTimedeltaNoSeq) -> Never: ...
36503614
@overload
36513615
def __truediv__(self: Series[T_COMPLEX], other: np_ndarray_td) -> Never: ...
36523616
@overload
36533617
def __truediv__(self: Series[bool], other: np_ndarray_bool) -> Never: ...
36543618
@overload
36553619
def __truediv__(
3656-
self: Series[T_COMPLEX], other: Index[Never] | Series[Never]
3620+
self: SeriesComplex | Series[Timedelta], other: Index[Never] | Series[Never]
36573621
) -> Series: ...
36583622
@overload
36593623
def __truediv__(
@@ -3723,7 +3687,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
37233687
) -> Series[T_COMPLEX]: ...
37243688
@overload
37253689
def __truediv__(
3726-
self: Series[T_COMPLEX],
3690+
self: SeriesComplex,
37273691
other: (
37283692
Just[complex]
37293693
| Sequence[Just[complex]]
@@ -3750,30 +3714,29 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
37503714
) -> Series[Timedelta]: ...
37513715
@overload
37523716
def __truediv__(
3753-
self: Series[Timedelta],
3754-
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
3717+
self: Series[Timedelta], other: ArrayIndexSeriesTimedeltaNoSeq
37553718
) -> Series[float]: ...
37563719
@overload
37573720
def __truediv__(self: Series[_str], other: Path) -> Series: ...
37583721
@overload
3759-
def truediv(
3722+
def truediv( # type: ignore[overload-overlap]
37603723
self: Series[Never],
3761-
other: _OtherTrueDiv,
3724+
other: ScalarArrayIndexSeriesComplex,
37623725
level: Level | None = None,
37633726
fill_value: float | None = None,
37643727
axis: AxisIndex = 0,
37653728
) -> Series: ...
37663729
@overload
37673730
def truediv(
37683731
self: Series[Never],
3769-
other: np_ndarray_td | TimedeltaIndex,
3732+
other: ArrayIndexTimedeltaNoSeq,
37703733
level: Level | None = None,
37713734
fill_value: float | None = None,
37723735
axis: AxisIndex = 0,
37733736
) -> Never: ...
37743737
@overload
37753738
def truediv(
3776-
self: Series[bool] | Series[int] | Series[float] | Series[complex],
3739+
self: SeriesComplex | Series[Timedelta],
37773740
other: Index[Never] | Series[Never],
37783741
level: Level | None = None,
37793742
fill_value: float | None = None,
@@ -3863,7 +3826,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
38633826
) -> Series[T_COMPLEX]: ...
38643827
@overload
38653828
def truediv(
3866-
self: Series[T_COMPLEX],
3829+
self: SeriesComplex,
38673830
other: (
38683831
Just[complex]
38693832
| Sequence[Just[complex]]
@@ -3897,7 +3860,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
38973860
@overload
38983861
def truediv(
38993862
self: Series[Timedelta],
3900-
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
3863+
other: ArrayIndexSeriesTimedeltaNoSeq,
39013864
level: Level | None = None,
39023865
fill_value: float | None = None,
39033866
axis: AxisIndex = 0,
@@ -3916,16 +3879,18 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
39163879
@overload
39173880
def __rtruediv__(
39183881
self: Series[Never],
3919-
other: timedelta | Sequence[timedelta] | np_ndarray_td | TimedeltaIndex,
3920-
) -> Never: ...
3921-
@overload
3922-
def __rtruediv__(self: Series[Never], other: _OtherRTrueDiv) -> Series: ...
3882+
other: ScalarArrayIndexSeriesComplex | ScalarArrayIndexSeriesTimedelta,
3883+
) -> Series: ...
39233884
@overload
39243885
def __rtruediv__(
39253886
self: Series[bool] | Series[int] | Series[float] | Series[complex],
39263887
other: Index[Never] | Series[Never],
39273888
) -> Series: ...
39283889
@overload
3890+
def __rtruediv__(
3891+
self: Series[int] | Series[float], other: Sequence[timedelta | np.timedelta64]
3892+
) -> Series: ...
3893+
@overload
39293894
def __rtruediv__(
39303895
self: Supports_ProtoRTrueDiv[_T_contra, S2],
39313896
other: _T_contra | Sequence[_T_contra],
@@ -3987,7 +3952,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
39873952
) -> Series[T_COMPLEX]: ...
39883953
@overload
39893954
def __rtruediv__(
3990-
self: Series[T_COMPLEX],
3955+
self: SeriesComplex,
39913956
other: (
39923957
Just[complex]
39933958
| Sequence[Just[complex]]
@@ -3998,8 +3963,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
39983963
) -> Series[complex]: ...
39993964
@overload
40003965
def __rtruediv__(
4001-
self: Series[Timedelta],
4002-
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
3966+
self: Series[Timedelta], other: ArrayIndexSeriesTimedeltaNoSeq
40033967
) -> Series[float]: ...
40043968
@overload
40053969
def __rtruediv__(
@@ -4017,20 +3981,12 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
40173981
@overload
40183982
def rtruediv(
40193983
self: Series[Never],
4020-
other: _OtherRTrueDiv,
3984+
other: ScalarArrayIndexSeriesComplex | ScalarArrayIndexSeriesTimedelta,
40213985
level: Level | None = None,
40223986
fill_value: float | None = None,
40233987
axis: AxisIndex = 0,
40243988
) -> Series: ...
40253989
@overload
4026-
def rtruediv(
4027-
self: Series[Never],
4028-
other: timedelta | Sequence[timedelta] | np_ndarray_td | TimedeltaIndex,
4029-
level: Level | None = None,
4030-
fill_value: float | None = None,
4031-
axis: AxisIndex = 0,
4032-
) -> Never: ...
4033-
@overload
40343990
def rtruediv(
40353991
self: Series[bool] | Series[int] | Series[float] | Series[complex],
40363992
other: Index[Never] | Series[Never],
@@ -4122,7 +4078,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
41224078
) -> Series[T_COMPLEX]: ...
41234079
@overload
41244080
def rtruediv(
4125-
self: Series[T_COMPLEX],
4081+
self: SeriesComplex,
41264082
other: (
41274083
Just[complex]
41284084
| Sequence[Just[complex]]
@@ -4137,7 +4093,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
41374093
@overload
41384094
def rtruediv(
41394095
self: Series[Timedelta],
4140-
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
4096+
other: ArrayIndexSeriesTimedeltaNoSeq,
41414097
level: Level | None = None,
41424098
fill_value: float | None = None,
41434099
axis: AxisIndex = 0,

tests/series/arithmetic/float/test_truediv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_truediv_py_sequence(left: "pd.Series[float]") -> None:
114114
check(assert_type(c / left, "pd.Series[complex]"), pd.Series, np.complexfloating)
115115
if TYPE_CHECKING_INVALID_USAGE:
116116
_14 = s / left # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
117-
check(assert_type(d / left, "pd.Series[pd.Timedelta]"), pd.Series, timedelta)
117+
check(assert_type(d / left, pd.Series), pd.Series, timedelta) # dtype object
118118

119119
check(assert_type(left.truediv(b), "pd.Series[float]"), pd.Series, np.floating)
120120
check(assert_type(left.truediv(i), "pd.Series[float]"), pd.Series, np.floating)

0 commit comments

Comments
 (0)