Skip to content

Commit 1877b98

Browse files
committed
fix(comment): #1360
1 parent 5780e7b commit 1877b98

File tree

6 files changed

+44
-57
lines changed

6 files changed

+44
-57
lines changed

pandas-stubs/core/base.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections.abc import (
22
Hashable,
33
Iterator,
4+
Sequence,
45
)
56
from typing import (
67
Any,
@@ -34,10 +35,24 @@ from pandas._typing import (
3435
SequenceNotStr,
3536
SupportsDType,
3637
np_1darray,
38+
np_ndarray_anyint,
39+
np_ndarray_bool,
40+
np_ndarray_complex,
41+
np_ndarray_float,
3742
)
3843
from pandas.util._decorators import cache_readonly
3944

4045
_ListLike: TypeAlias = ArrayLike | dict[str, np.ndarray] | SequenceNotStr[S1]
46+
NumListLike: TypeAlias = (
47+
ExtensionArray
48+
| np_ndarray_bool
49+
| np_ndarray_anyint
50+
| np_ndarray_float
51+
| np_ndarray_complex
52+
| dict[str, np.ndarray]
53+
| Sequence[complex]
54+
| IndexOpsMixin[complex]
55+
)
4156

4257
class NoNewAttributesMixin:
4358
def __setattr__(self, key: str, value: Any) -> None: ...

pandas-stubs/core/indexes/base.pyi

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@ from pandas import (
3131
PeriodDtype,
3232
PeriodIndex,
3333
Series,
34-
Timedelta,
3534
TimedeltaIndex,
36-
Timestamp,
3735
)
3836
from pandas.core.arrays import ExtensionArray
39-
from pandas.core.base import IndexOpsMixin
37+
from pandas.core.base import (
38+
IndexOpsMixin,
39+
NumListLike,
40+
_ListLike,
41+
)
4042
from pandas.core.strings.accessor import StringMethods
4143
from typing_extensions import (
4244
Never,
4345
Self,
44-
TypeAlias,
4546
)
4647

4748
from pandas._libs.interval import _OrderableT
@@ -80,24 +81,11 @@ from pandas._typing import (
8081
np_ndarray_complex,
8182
np_ndarray_float,
8283
np_ndarray_str,
83-
np_ndarray_td,
8484
type_t,
8585
)
8686

8787
class InvalidIndexError(Exception): ...
8888

89-
_ListLike: TypeAlias = ArrayLike | dict[_str, np.ndarray] | SequenceNotStr[S1]
90-
_NumListLike: TypeAlias = (
91-
ExtensionArray
92-
| np_ndarray_bool
93-
| np_ndarray_anyint
94-
| np_ndarray_float
95-
| np_ndarray_complex
96-
| dict[_str, np.ndarray]
97-
| Sequence[complex]
98-
| IndexOpsMixin[complex]
99-
)
100-
10189
class Index(IndexOpsMixin[S1]):
10290
__hash__: ClassVar[None] # type: ignore[assignment]
10391
# overloads with additional dtypes
@@ -642,9 +630,9 @@ class Index(IndexOpsMixin[S1]):
642630
@overload
643631
def __sub__(self: Index[Never], other: DatetimeIndex) -> Never: ...
644632
@overload
645-
def __sub__(self: Index[Never], other: complex | _NumListLike | Index) -> Index: ...
633+
def __sub__(self: Index[Never], other: complex | NumListLike | Index) -> Index: ...
646634
@overload
647-
def __sub__(self, other: Index[Never]) -> Index: ... # type: ignore[overload-overlap]
635+
def __sub__(self, other: Index[Never]) -> Index: ...
648636
@overload
649637
def __sub__(
650638
self: Index[bool],
@@ -709,21 +697,9 @@ class Index(IndexOpsMixin[S1]):
709697
),
710698
) -> Index[complex]: ...
711699
@overload
712-
def __sub__(
713-
self: Index[Timestamp],
714-
other: timedelta | np.timedelta64 | np_ndarray_td | TimedeltaIndex,
715-
) -> DatetimeIndex: ...
716-
@overload
717-
def __sub__(
718-
self: Index[Timedelta],
719-
other: timedelta | np.timedelta64 | np_ndarray_td | TimedeltaIndex,
720-
) -> TimedeltaIndex: ...
721-
@overload
722700
def __rsub__(self: Index[Never], other: DatetimeIndex) -> Never: ... # type: ignore[misc]
723701
@overload
724-
def __rsub__(
725-
self: Index[Never], other: complex | _NumListLike | Index
726-
) -> Index: ...
702+
def __rsub__(self: Index[Never], other: complex | NumListLike | Index) -> Index: ...
727703
@overload
728704
def __rsub__(self, other: Index[Never]) -> Index: ...
729705
@overload

pandas-stubs/core/indexes/datetimes.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ from pandas._typing import (
3737
IntervalClosedType,
3838
TimeUnit,
3939
TimeZones,
40+
np_ndarray_dt,
41+
np_ndarray_td,
4042
)
4143

4244
from pandas.core.dtypes.dtypes import DatetimeTZDtype
@@ -71,11 +73,12 @@ class DatetimeIndex(
7173
def __sub__(self, other: TimedeltaSeries) -> TimestampSeries: ...
7274
@overload
7375
def __sub__(
74-
self, other: timedelta | Timedelta | TimedeltaIndex | BaseOffset
76+
self,
77+
other: timedelta | np.timedelta64 | np_ndarray_td | TimedeltaIndex | BaseOffset,
7578
) -> DatetimeIndex: ...
7679
@overload
7780
def __sub__( # pyright: ignore[reportIncompatibleMethodOverride]
78-
self, other: datetime | Timestamp | DatetimeIndex
81+
self, other: datetime | np.datetime64 | np_ndarray_dt | DatetimeIndex
7982
) -> TimedeltaIndex: ...
8083
@final
8184
def to_series(self, index=..., name: Hashable = ...) -> TimestampSeries: ...

pandas-stubs/core/indexes/timedeltas.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ from pandas._libs.tslibs import BaseOffset
3030
from pandas._typing import (
3131
AxesData,
3232
TimedeltaConvertibleTypes,
33+
np_ndarray_td,
3334
num,
3435
)
3536

@@ -59,7 +60,7 @@ class TimedeltaIndex(
5960
) -> Self: ...
6061
def __radd__(self, other: dt.datetime | Timestamp | DatetimeIndex) -> DatetimeIndex: ... # type: ignore[override]
6162
def __sub__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
62-
self, other: dt.timedelta | Timedelta | Self
63+
self, other: dt.timedelta | np.timedelta64 | np_ndarray_td | Self
6364
) -> Self: ...
6465
def __mul__(self, other: num) -> Self: ...
6566
@overload # type: ignore[override]

pandas-stubs/core/series.pyi

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ from pandas.core.arrays.base import ExtensionArray
5454
from pandas.core.arrays.categorical import CategoricalAccessor
5555
from pandas.core.arrays.datetimes import DatetimeArray
5656
from pandas.core.arrays.interval import IntervalArray
57-
from pandas.core.base import IndexOpsMixin
57+
from pandas.core.base import (
58+
IndexOpsMixin,
59+
NumListLike,
60+
_ListLike,
61+
)
5862
from pandas.core.frame import DataFrame
5963
from pandas.core.generic import NDFrame
6064
from pandas.core.groupby.generic import SeriesGroupBy
@@ -258,20 +262,9 @@ class _LocIndexerSeries(_LocIndexer, Generic[S1]):
258262
value: S1 | ArrayLike | Series[S1] | None,
259263
) -> None: ...
260264

261-
_ListLike: TypeAlias = ArrayLike | dict[_str, np.ndarray] | SequenceNotStr[S1]
262265
_ListLikeS1: TypeAlias = (
263266
ArrayLike | dict[_str, np.ndarray] | Sequence[S1] | IndexOpsMixin[S1]
264267
)
265-
_NumListLike: TypeAlias = (
266-
ExtensionArray
267-
| np_ndarray_bool
268-
| np_ndarray_anyint
269-
| np_ndarray_float
270-
| np_ndarray_complex
271-
| dict[_str, np.ndarray]
272-
| Sequence[complex]
273-
| IndexOpsMixin[complex]
274-
)
275268

276269
class Series(IndexOpsMixin[S1], NDFrame):
277270
# Define __index__ because mypy thinks Series follows protocol `SupportsIndex` https://github.com/pandas-dev/pandas-stubs/pull/1332#discussion_r2285648790
@@ -2125,7 +2118,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
21252118
) -> Series[_bool]: ...
21262119
@overload
21272120
def __mul__(
2128-
self: Series[Never], other: complex | _NumListLike | Series
2121+
self: Series[Never], other: complex | NumListLike | Series
21292122
) -> Series: ...
21302123
@overload
21312124
def __mul__(self, other: Series[Never]) -> Series: ... # type: ignore[overload-overlap]
@@ -2321,7 +2314,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
23212314
) -> TimedeltaSeries: ...
23222315
@overload
23232316
def __rmul__(
2324-
self: Series[Never], other: complex | _NumListLike | Series
2317+
self: Series[Never], other: complex | NumListLike | Series
23252318
) -> Series: ...
23262319
@overload
23272320
def __rmul__(self, other: Series[Never]) -> Series: ... # type: ignore[overload-overlap]
@@ -2555,7 +2548,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
25552548
) -> Never: ...
25562549
@overload
25572550
def __sub__(
2558-
self: Series[Never], other: complex | _NumListLike | Index | Series
2551+
self: Series[Never], other: complex | NumListLike | Index | Series
25592552
) -> Series: ...
25602553
@overload
25612554
def __sub__(self, other: Index[Never] | Series[Never]) -> Series: ... # type: ignore[overload-overlap]
@@ -2680,7 +2673,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
26802673
@overload
26812674
def sub(
26822675
self: Series[Never],
2683-
other: complex | _NumListLike | Index | Series,
2676+
other: complex | NumListLike | Index | Series,
26842677
level: Level | None = None,
26852678
fill_value: float | None = None,
26862679
axis: int = 0,
@@ -2834,7 +2827,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
28342827
def __rsub__(self: Series[Never], other: DatetimeIndex | TimestampSeries) -> Never: ... # type: ignore[misc]
28352828
@overload
28362829
def __rsub__(
2837-
self: Series[Never], other: complex | _NumListLike | Index | Series
2830+
self: Series[Never], other: complex | NumListLike | Index | Series
28382831
) -> Series: ...
28392832
@overload
28402833
def __rsub__(self, other: Index[Never] | Series[Never]) -> Series: ...
@@ -2937,7 +2930,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
29372930
@overload
29382931
def rsub(
29392932
self: Series[Never],
2940-
other: complex | _NumListLike | Index | Series,
2933+
other: complex | NumListLike | Index | Series,
29412934
level: Level | None = None,
29422935
fill_value: float | None = None,
29432936
axis: int = 0,
@@ -3061,7 +3054,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
30613054
) -> Series[complex]: ...
30623055
@overload
30633056
def __truediv__( # type:ignore[overload-overlap]
3064-
self: Series[Never], other: complex | _NumListLike | Series
3057+
self: Series[Never], other: complex | NumListLike | Series
30653058
) -> Series: ...
30663059
@overload
30673060
def __truediv__(self, other: Series[Never]) -> Series: ...
@@ -3257,7 +3250,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
32573250
div = truediv
32583251
@overload
32593252
def __rtruediv__( # type:ignore[overload-overlap]
3260-
self: Series[Never], other: complex | _NumListLike | Series
3253+
self: Series[Never], other: complex | NumListLike | Series
32613254
) -> Series: ...
32623255
@overload
32633256
def __rtruediv__(self, other: Series[Never]) -> Series: ...

tests/indexes/arithmetic/test_sub.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
check,
1111
)
1212

13-
# left operands
13+
# left operand
1414
left_i = pd.MultiIndex.from_tuples([(1,), (2,), (3,)]).levels[0]
15-
left_str = pd.MultiIndex.from_tuples([("1",), ("2",), ("3_",)]).levels[0]
1615

1716

1817
def test_sub_i_py_scalar() -> None:

0 commit comments

Comments
 (0)