Skip to content

Commit c70a3ce

Browse files
committed
series
1 parent eb648fb commit c70a3ce

File tree

4 files changed

+268
-83
lines changed

4 files changed

+268
-83
lines changed

pandas-stubs/core/series.pyi

Lines changed: 93 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,30 @@ _DataLike: TypeAlias = ArrayLike | dict[str, np.ndarray] | SequenceNotStr[S1]
302302
_DataLikeS1: TypeAlias = (
303303
ArrayLike | dict[_str, np.ndarray] | Sequence[S1] | IndexOpsMixin[S1]
304304
)
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]
327+
)
328+
_OtherRTrueDiv: TypeAlias = _OtherTrueDiv
305329

306330
class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
307331
# Define __index__ because mypy thinks Series follows protocol `SupportsIndex` https://github.com/pandas-dev/pandas-stubs/pull/1332#discussion_r2285648790
@@ -2150,33 +2174,22 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
21502174
def __and__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ...
21512175
def __eq__(self, other: object) -> Series[_bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
21522176
@overload
2153-
def __floordiv__( # type: ignore[overload-overlap]
2154-
self: Series[Never],
2155-
other: (
2156-
float
2157-
| Sequence[float]
2158-
| np_ndarray_bool
2159-
| np_ndarray_anyint
2160-
| np_ndarray_float
2161-
| Index[bool]
2162-
| Index[int]
2163-
| Index[float]
2164-
| Series[bool]
2165-
| Series[int]
2166-
| Series[float]
2167-
),
2168-
) -> Series: ...
2169-
@overload
21702177
def __floordiv__(
2171-
self: Series[bool] | Series[int] | Series[float] | Series[Timedelta],
2172-
other: Index[Never] | Series[Never],
2173-
) -> Series: ...
2178+
self: Series[Never], other: np_ndarray_td | TimedeltaIndex
2179+
) -> Never: ...
21742180
@overload
21752181
def __floordiv__(
21762182
self: Series[int] | Series[float],
21772183
other: np_ndarray_complex | np_ndarray_dt | np_ndarray_td,
21782184
) -> Never: ...
21792185
@overload
2186+
def __floordiv__(self: Series[Never], other: _OtherFloorDiv) -> Series: ...
2187+
@overload
2188+
def __floordiv__(
2189+
self: Series[bool] | Series[int] | Series[float] | Series[Timedelta],
2190+
other: Index[Never] | Series[Never],
2191+
) -> Series: ...
2192+
@overload
21802193
def __floordiv__(
21812194
self: Series[bool] | Series[complex], other: np_ndarray
21822195
) -> Never: ...
@@ -2237,19 +2250,15 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
22372250
@overload
22382251
def floordiv(
22392252
self: Series[Never],
2240-
other: (
2241-
float
2242-
| Sequence[float]
2243-
| np_ndarray_bool
2244-
| np_ndarray_anyint
2245-
| np_ndarray_float
2246-
| Index[bool]
2247-
| Index[int]
2248-
| Index[float]
2249-
| Series[bool]
2250-
| Series[int]
2251-
| Series[float]
2252-
),
2253+
other: np_ndarray_td | TimedeltaIndex,
2254+
level: Level | None = None,
2255+
fill_value: float | None = None,
2256+
axis: AxisIndex = 0,
2257+
) -> Never: ...
2258+
@overload
2259+
def floordiv(
2260+
self: Series[Never],
2261+
other: _OtherFloorDiv,
22532262
level: Level | None = ...,
22542263
fill_value: float | None = None,
22552264
axis: AxisIndex | None = 0,
@@ -2356,20 +2365,21 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
23562365
),
23572366
) -> Series: ...
23582367
@overload
2359-
def __rfloordiv__(
2360-
self: Series[bool] | Series[int] | Series[float] | Series[Timedelta],
2361-
other: Index[Never] | Series[Never],
2362-
) -> Series: ...
2368+
def __rfloordiv__(self, other: np_ndarray_complex | np_ndarray_dt) -> Never: ...
23632369
@overload
23642370
def __rfloordiv__(
2365-
self: Series[int] | Series[float],
2366-
other: np_ndarray_complex | np_ndarray_dt | np_ndarray_td,
2371+
self: Series[int] | Series[float], other: np_ndarray_td
23672372
) -> Never: ...
23682373
@overload
23692374
def __rfloordiv__(
23702375
self: Series[bool] | Series[complex], other: np_ndarray
23712376
) -> Never: ...
23722377
@overload
2378+
def __rfloordiv__(
2379+
self: Series[bool] | Series[int] | Series[float] | Series[Timedelta],
2380+
other: Index[Never] | Series[Never],
2381+
) -> Series: ...
2382+
@overload
23732383
def __rfloordiv__(
23742384
self: Supports_ProtoRFloorDiv[_T_contra, S2],
23752385
other: _T_contra | Sequence[_T_contra],
@@ -3628,17 +3638,23 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
36283638
axis: int = 0,
36293639
) -> Series[BaseOffset]: ...
36303640
@overload
3641+
def __truediv__(self, other: np_ndarray_dt) -> Never: ...
3642+
@overload
36313643
def __truediv__( # type: ignore[overload-overlap]
3632-
self: Series[Never], other: complex | NumListLike | Index | Series
3644+
self: Series[Never], other: _OtherTrueDiv
36333645
) -> Series: ...
36343646
@overload
3635-
def __truediv__(self, other: Index[Never] | Series[Never]) -> Series: ...
3647+
def __truediv__(
3648+
self: Series[Never], other: np_ndarray_td | TimedeltaIndex
3649+
) -> Never: ...
36363650
@overload
3637-
def __truediv__(self: Series[bool], other: np_ndarray_bool) -> Never: ...
3651+
def __truediv__(self: Series[T_COMPLEX], other: np_ndarray_td) -> Never: ...
36383652
@overload
3639-
def __truediv__(self, other: np_ndarray_dt) -> Never: ...
3653+
def __truediv__(self: Series[bool], other: np_ndarray_bool) -> Never: ...
36403654
@overload
3641-
def __truediv__(self: Series[T_COMPLEX], other: np_ndarray_td) -> Never: ...
3655+
def __truediv__(
3656+
self: Series[T_COMPLEX], other: Index[Never] | Series[Never]
3657+
) -> Series: ...
36423658
@overload
36433659
def __truediv__(
36443660
self: Series[Timedelta],
@@ -3738,18 +3754,26 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
37383754
other: np_ndarray_td | TimedeltaIndex | Series[Timedelta],
37393755
) -> Series[float]: ...
37403756
@overload
3741-
def __truediv__(self, other: Path) -> Series: ...
3757+
def __truediv__(self: Series[_str], other: Path) -> Series: ...
37423758
@overload
37433759
def truediv(
37443760
self: Series[Never],
3745-
other: complex | ListLike,
3761+
other: _OtherTrueDiv,
37463762
level: Level | None = None,
37473763
fill_value: float | None = None,
37483764
axis: AxisIndex = 0,
37493765
) -> Series: ...
37503766
@overload
37513767
def truediv(
3752-
self,
3768+
self: Series[Never],
3769+
other: np_ndarray_td | TimedeltaIndex,
3770+
level: Level | None = None,
3771+
fill_value: float | None = None,
3772+
axis: AxisIndex = 0,
3773+
) -> Never: ...
3774+
@overload
3775+
def truediv(
3776+
self: Series[bool] | Series[int] | Series[float] | Series[complex],
37533777
other: Index[Never] | Series[Never],
37543778
level: Level | None = None,
37553779
fill_value: float | None = None,
@@ -3880,29 +3904,28 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
38803904
) -> Series[float]: ...
38813905
@overload
38823906
def truediv(
3883-
self,
3907+
self: Series[_str],
38843908
other: Path,
38853909
level: Level | None = None,
38863910
fill_value: float | None = None,
38873911
axis: AxisIndex = 0,
38883912
) -> Series: ...
38893913
div = truediv
38903914
@overload
3891-
def __rtruediv__( # type: ignore[overload-overlap]
3892-
self: Series[Never], other: complex | NumListLike | Index | Series
3893-
) -> Series: ...
3894-
@overload
3895-
def __rtruediv__(self, other: Index[Never] | Series[Never]) -> Series: ...
3896-
@overload
3897-
def __rtruediv__(self: Series[bool], other: np_ndarray_bool) -> Never: ...
3898-
@overload
38993915
def __rtruediv__(self, other: np_ndarray_dt) -> Never: ...
39003916
@overload
39013917
def __rtruediv__(
3902-
self: Series[Timedelta],
3903-
other: np_ndarray_bool | np_ndarray_complex,
3918+
self: Series[Never],
3919+
other: timedelta | Sequence[timedelta] | np_ndarray_td | TimedeltaIndex,
39043920
) -> Never: ...
39053921
@overload
3922+
def __rtruediv__(self: Series[Never], other: _OtherRTrueDiv) -> Series: ...
3923+
@overload
3924+
def __rtruediv__(
3925+
self: Series[bool] | Series[int] | Series[float] | Series[complex],
3926+
other: Index[Never] | Series[Never],
3927+
) -> Series: ...
3928+
@overload
39063929
def __rtruediv__(
39073930
self: Supports_ProtoRTrueDiv[_T_contra, S2],
39083931
other: _T_contra | Sequence[_T_contra],
@@ -3990,18 +4013,26 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
39904013
),
39914014
) -> Series[Timedelta]: ...
39924015
@overload
3993-
def __rtruediv__(self, other: Path) -> Series: ...
4016+
def __rtruediv__(self: Series[_str], other: Path) -> Series: ...
39944017
@overload
39954018
def rtruediv(
39964019
self: Series[Never],
3997-
other: complex | ListLike,
4020+
other: _OtherRTrueDiv,
39984021
level: Level | None = None,
39994022
fill_value: float | None = None,
40004023
axis: AxisIndex = 0,
40014024
) -> Series: ...
40024025
@overload
40034026
def rtruediv(
4004-
self,
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
4034+
def rtruediv(
4035+
self: Series[bool] | Series[int] | Series[float] | Series[complex],
40054036
other: Index[Never] | Series[Never],
40064037
level: Level | None = None,
40074038
fill_value: float | None = None,
@@ -4127,7 +4158,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
41274158
) -> Series[Timedelta]: ...
41284159
@overload
41294160
def rtruediv(
4130-
self,
4161+
self: Series[_str],
41314162
other: Path,
41324163
level: Level | None = None,
41334164
fill_value: float | None = None,

tests/series/arithmetic/test_floordiv.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,14 @@ def test_floordiv_numpy_array(left_i: pd.Series) -> None:
124124
check(assert_type(left_i // b, pd.Series), pd.Series, np.integer)
125125
check(assert_type(left_i // i, pd.Series), pd.Series, np.integer)
126126
check(assert_type(left_i // f, pd.Series), pd.Series, np.floating)
127-
if TYPE_CHECKING_INVALID_USAGE:
127+
128+
def _03() -> None: # pyright: ignore[reportUnusedFunction]
128129
assert_type(left_i // c, Never)
130+
131+
def _04() -> None: # pyright: ignore[reportUnusedFunction]
129132
assert_type(left_i // s, Never)
133+
134+
def _05() -> None: # pyright: ignore[reportUnusedFunction]
130135
assert_type(left_i // d, Never)
131136

132137
# `numpy` typing gives the corresponding `ndarray`s in the static type
@@ -143,17 +148,26 @@ def test_floordiv_numpy_array(left_i: pd.Series) -> None:
143148
check(assert_type(left_i.floordiv(b), pd.Series), pd.Series, np.integer)
144149
check(assert_type(left_i.floordiv(i), pd.Series), pd.Series, np.integer)
145150
check(assert_type(left_i.floordiv(f), pd.Series), pd.Series, np.floating)
146-
if TYPE_CHECKING_INVALID_USAGE:
151+
152+
def _23() -> None: # pyright: ignore[reportUnusedFunction]
147153
left_i.floordiv(c) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
154+
155+
def _24() -> None: # pyright: ignore[reportUnusedFunction]
148156
left_i.floordiv(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
149-
# left_i.floordiv(d) # This invalid one cannot be detected by static type checking
157+
158+
def _25() -> None: # pyright: ignore[reportUnusedFunction]
159+
assert_type(left_i.floordiv(d), Never)
150160

151161
check(assert_type(left_i.rfloordiv(b), pd.Series), pd.Series, np.integer)
152162
check(assert_type(left_i.rfloordiv(i), pd.Series), pd.Series, np.integer)
153163
check(assert_type(left_i.rfloordiv(f), pd.Series), pd.Series, np.floating)
154-
if TYPE_CHECKING_INVALID_USAGE:
164+
165+
def _33() -> None: # pyright: ignore[reportUnusedFunction]
155166
left_i.rfloordiv(c) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
167+
168+
def _34() -> None: # pyright: ignore[reportUnusedFunction]
156169
left_i.rfloordiv(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
170+
157171
check(
158172
assert_type(left_i.rfloordiv(d), "pd.Series[pd.Timedelta]"),
159173
pd.Series,
@@ -176,7 +190,9 @@ def test_floordiv_pd_index(left_i: pd.Series) -> None:
176190
if TYPE_CHECKING_INVALID_USAGE:
177191
_03 = left_i // c # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
178192
_04 = left_i // s # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
179-
# _05 = left_i // d # This invalid one cannot be detected by static type checking
193+
194+
def _05() -> None: # pyright: ignore[reportUnusedFunction]
195+
assert_type(left_i // d, Never)
180196

181197
check(assert_type(b // left_i, pd.Series), pd.Series, np.integer)
182198
check(assert_type(i // left_i, pd.Series), pd.Series, np.integer)
@@ -192,7 +208,9 @@ def test_floordiv_pd_index(left_i: pd.Series) -> None:
192208
if TYPE_CHECKING_INVALID_USAGE:
193209
left_i.floordiv(c) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
194210
left_i.floordiv(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
195-
# left_i.floordiv(d) # This invalid one cannot be detected by static type checking
211+
212+
def _25() -> None: # pyright: ignore[reportUnusedFunction]
213+
assert_type(left_i.floordiv(d), Never)
196214

197215
check(assert_type(left_i.rfloordiv(b), pd.Series), pd.Series, np.integer)
198216
check(assert_type(left_i.rfloordiv(i), pd.Series), pd.Series, np.integer)
@@ -222,7 +240,7 @@ def test_floordiv_pd_series(left_i: pd.Series) -> None:
222240
if TYPE_CHECKING_INVALID_USAGE:
223241
_03 = left_i // c # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
224242
_04 = left_i // s # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
225-
# _05 = left_i // d # This invalid one cannot be detected by static type checking
243+
# left_i // d # This invalid one cannot be detected by static type checking
226244

227245
check(assert_type(b // left_i, pd.Series), pd.Series, np.integer)
228246
check(assert_type(i // left_i, pd.Series), pd.Series, np.integer)
@@ -235,8 +253,11 @@ def test_floordiv_pd_series(left_i: pd.Series) -> None:
235253
check(assert_type(left_i.floordiv(b), pd.Series), pd.Series, np.integer)
236254
check(assert_type(left_i.floordiv(i), pd.Series), pd.Series, np.integer)
237255
check(assert_type(left_i.floordiv(f), pd.Series), pd.Series, np.floating)
238-
if TYPE_CHECKING_INVALID_USAGE:
256+
257+
def _23() -> None: # pyright: ignore[reportUnusedFunction]
239258
left_i.floordiv(c) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
259+
260+
if TYPE_CHECKING_INVALID_USAGE:
240261
left_i.floordiv(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
241262
# left_i.floordiv(d) # This invalid one cannot be detected by static type checking
242263

@@ -246,6 +267,7 @@ def test_floordiv_pd_series(left_i: pd.Series) -> None:
246267
if TYPE_CHECKING_INVALID_USAGE:
247268
left_i.rfloordiv(c) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
248269
left_i.rfloordiv(s) # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]
270+
249271
check(
250272
assert_type(left_i.rfloordiv(d), "pd.Series[pd.Timedelta]"),
251273
pd.Series,

0 commit comments

Comments
 (0)