Skip to content

Commit e07d186

Browse files
committed
feat(index): arithmetic subtraction
1 parent ffa88e5 commit e07d186

File tree

14 files changed

+807
-59
lines changed

14 files changed

+807
-59
lines changed

pandas-stubs/core/arraylike.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class OpsMixin:
1919
def __rxor__(self, other: Any) -> Self: ...
2020
# -------------------------------------------------------------
2121
# Arithmetic Methods
22-
def __sub__(self, other: Any) -> Self: ...
23-
def __rsub__(self, other: Any) -> Self: ...
2422
def __mul__(self, other: Any) -> Self: ...
2523
def __rmul__(self, other: Any) -> Self: ...
2624
# Handled by subclasses that specify only the valid values

pandas-stubs/core/frame.pyi

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,22 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
17881788
level: Level | None = None,
17891789
fill_value: float | None = None,
17901790
) -> Self: ...
1791+
def __sub__(self, other: Any) -> Self: ...
1792+
def sub(
1793+
self,
1794+
other: num | ListLike | DataFrame,
1795+
axis: Axis | None = ...,
1796+
level: Level | None = ...,
1797+
fill_value: float | None = None,
1798+
) -> Self: ...
1799+
def __rsub__(self, other: Any) -> Self: ...
1800+
def rsub(
1801+
self,
1802+
other,
1803+
axis: Axis = ...,
1804+
level: Level | None = ...,
1805+
fill_value: float | None = None,
1806+
) -> Self: ...
17911807
@final
17921808
def add_prefix(self, prefix: _str, axis: Axis | None = None) -> Self: ...
17931809
@final
@@ -2353,13 +2369,6 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
23532369
level: Level | None = ...,
23542370
fill_value: float | None = None,
23552371
) -> Self: ...
2356-
def rsub(
2357-
self,
2358-
other,
2359-
axis: Axis = ...,
2360-
level: Level | None = ...,
2361-
fill_value: float | None = None,
2362-
) -> Self: ...
23632372
def rtruediv(
23642373
self,
23652374
other,
@@ -2405,20 +2414,6 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
24052414
numeric_only: _bool = False,
24062415
**kwargs: Any,
24072416
) -> Series: ...
2408-
def sub(
2409-
self,
2410-
other: num | ListLike | DataFrame,
2411-
axis: Axis | None = ...,
2412-
level: Level | None = ...,
2413-
fill_value: float | None = None,
2414-
) -> Self: ...
2415-
def subtract(
2416-
self,
2417-
other: num | ListLike | DataFrame,
2418-
axis: Axis | None = ...,
2419-
level: Level | None = ...,
2420-
fill_value: float | None = None,
2421-
) -> Self: ...
24222417
def sum(
24232418
self,
24242419
axis: Axis = 0,

pandas-stubs/core/indexes/base.pyi

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ from pandas import (
3131
PeriodDtype,
3232
PeriodIndex,
3333
Series,
34+
Timedelta,
3435
TimedeltaIndex,
36+
Timestamp,
3537
)
3638
from pandas.core.arrays import ExtensionArray
3739
from pandas.core.base import IndexOpsMixin
@@ -60,6 +62,7 @@ from pandas._typing import (
6062
GenericT_co,
6163
HashableT,
6264
IgnoreRaise,
65+
Just,
6366
Label,
6467
Level,
6568
MaskType,
@@ -77,12 +80,23 @@ from pandas._typing import (
7780
np_ndarray_complex,
7881
np_ndarray_float,
7982
np_ndarray_str,
83+
np_ndarray_td,
8084
type_t,
8185
)
8286

8387
class InvalidIndexError(Exception): ...
8488

8589
_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+
)
86100

87101
class Index(IndexOpsMixin[S1]):
88102
__hash__: ClassVar[None] # type: ignore[assignment]
@@ -626,6 +640,156 @@ class Index(IndexOpsMixin[S1]):
626640
self: Index[_str], other: _str | Sequence[_str] | np_ndarray_str | Index[_str]
627641
) -> Index[_str]: ...
628642
@overload
643+
def __sub__(self: Index[Never], other: DatetimeIndex) -> Never: ...
644+
@overload
645+
def __sub__(self: Index[Never], other: complex | _NumListLike | Index) -> Index: ...
646+
@overload
647+
def __sub__(self, other: Index[Never]) -> Index: ... # type: ignore[overload-overlap]
648+
@overload
649+
def __sub__(
650+
self: Index[bool],
651+
other: Just[int] | Sequence[Just[int]] | np_ndarray_anyint | Index[int],
652+
) -> Index[int]: ...
653+
@overload
654+
def __sub__(
655+
self: Index[bool],
656+
other: Just[float] | Sequence[Just[float]] | np_ndarray_float | Index[float],
657+
) -> Index[float]: ...
658+
@overload
659+
def __sub__(
660+
self: Index[int],
661+
other: (
662+
int
663+
| Sequence[int]
664+
| np_ndarray_bool
665+
| np_ndarray_anyint
666+
| Index[bool]
667+
| Index[int]
668+
),
669+
) -> Index[int]: ...
670+
@overload
671+
def __sub__(
672+
self: Index[int],
673+
other: Just[float] | Sequence[Just[float]] | np_ndarray_float | Index[float],
674+
) -> Index[float]: ...
675+
@overload
676+
def __sub__(
677+
self: Index[float],
678+
other: (
679+
float
680+
| Sequence[float]
681+
| np_ndarray_bool
682+
| np_ndarray_anyint
683+
| np_ndarray_float
684+
| Index[bool]
685+
| Index[int]
686+
| Index[float]
687+
),
688+
) -> Index[float]: ...
689+
@overload
690+
def __sub__(
691+
self: Index[complex],
692+
other: (
693+
T_COMPLEX
694+
| Sequence[T_COMPLEX]
695+
| np_ndarray_bool
696+
| np_ndarray_anyint
697+
| np_ndarray_float
698+
| Index[T_COMPLEX]
699+
),
700+
) -> Index[complex]: ...
701+
@overload
702+
def __sub__(
703+
self: Index[T_COMPLEX],
704+
other: (
705+
Just[complex]
706+
| Sequence[Just[complex]]
707+
| np_ndarray_complex
708+
| Index[complex]
709+
),
710+
) -> Index[complex]: ...
711+
@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
722+
def __rsub__(self: Index[Never], other: DatetimeIndex) -> Never: ... # type: ignore[misc]
723+
@overload
724+
def __rsub__(
725+
self: Index[Never], other: complex | _NumListLike | Index
726+
) -> Index: ...
727+
@overload
728+
def __rsub__(self, other: Index[Never]) -> Index: ...
729+
@overload
730+
def __rsub__(
731+
self: Index[bool],
732+
other: Just[int] | Sequence[Just[int]] | np_ndarray_anyint | Index[int],
733+
) -> Index[int]: ...
734+
@overload
735+
def __rsub__(
736+
self: Index[bool],
737+
other: Just[float] | Sequence[Just[float]] | np_ndarray_float | Index[float],
738+
) -> Index[float]: ...
739+
@overload
740+
def __rsub__(
741+
self: Index[int],
742+
other: (
743+
int
744+
| Sequence[int]
745+
| np_ndarray_bool
746+
| np_ndarray_anyint
747+
| Index[bool]
748+
| Index[int]
749+
),
750+
) -> Index[int]: ...
751+
@overload
752+
def __rsub__(
753+
self: Index[int],
754+
other: Just[float] | Sequence[Just[float]] | np_ndarray_float | Index[float],
755+
) -> Index[float]: ...
756+
@overload
757+
def __rsub__(
758+
self: Index[float],
759+
other: (
760+
float
761+
| Sequence[float]
762+
| np_ndarray_bool
763+
| np_ndarray_anyint
764+
| np_ndarray_float
765+
| Index[bool]
766+
| Index[int]
767+
| Index[float]
768+
),
769+
) -> Index[float]: ...
770+
@overload
771+
def __rsub__(
772+
self: Index[complex],
773+
other: (
774+
T_COMPLEX
775+
| Sequence[T_COMPLEX]
776+
| np_ndarray_bool
777+
| np_ndarray_anyint
778+
| np_ndarray_float
779+
| Index[T_COMPLEX]
780+
),
781+
) -> Index[complex]: ...
782+
@overload
783+
def __rsub__(
784+
self: Index[T_COMPLEX],
785+
other: (
786+
Just[complex]
787+
| Sequence[Just[complex]]
788+
| np_ndarray_complex
789+
| Index[complex]
790+
),
791+
) -> Index[complex]: ...
792+
@overload
629793
def __mul__(
630794
self: Index[int] | Index[float], other: timedelta
631795
) -> TimedeltaIndex: ...

pandas-stubs/core/indexes/datetimelike.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DatetimeIndexOpsMixin(ExtensionIndex[S1, GenericT_co]):
3030
def argmax(
3131
self, axis: AxisIndex | None = None, skipna: bool = True, *args, **kwargs
3232
) -> np.int64: ...
33-
def __rsub__( # type: ignore[override]
33+
def __rsub__( # type: ignore[misc,override] # pyright: ignore[reportIncompatibleMethodOverride]
3434
self, other: DatetimeIndexOpsMixin
3535
) -> TimedeltaIndex: ...
3636

pandas-stubs/core/indexes/datetimes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ class DatetimeIndex(
6767
def __add__( # pyright: ignore[reportIncompatibleMethodOverride]
6868
self, other: timedelta | Timedelta | TimedeltaIndex | BaseOffset
6969
) -> DatetimeIndex: ...
70-
@overload
70+
@overload # type: ignore[override]
7171
def __sub__(self, other: TimedeltaSeries) -> TimestampSeries: ...
7272
@overload
7373
def __sub__(
7474
self, other: timedelta | Timedelta | TimedeltaIndex | BaseOffset
7575
) -> DatetimeIndex: ...
7676
@overload
77-
def __sub__(
77+
def __sub__( # pyright: ignore[reportIncompatibleMethodOverride]
7878
self, other: datetime | Timestamp | DatetimeIndex
7979
) -> TimedeltaIndex: ...
8080
@final

pandas-stubs/core/indexes/period.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexField
3636
) -> Self: ...
3737
@property
3838
def values(self) -> np_1darray[np.object_]: ...
39-
@overload
39+
@overload # type: ignore[override]
4040
def __sub__(self, other: Period) -> Index: ...
4141
@overload
4242
def __sub__(self, other: Self) -> Index: ...
@@ -45,7 +45,9 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexField
4545
@overload
4646
def __sub__(self, other: NaTType) -> NaTType: ...
4747
@overload
48-
def __sub__(self, other: TimedeltaIndex | pd.Timedelta) -> Self: ...
48+
def __sub__( # pyright: ignore[reportIncompatibleMethodOverride]
49+
self, other: TimedeltaIndex | pd.Timedelta
50+
) -> Self: ...
4951
@overload # type: ignore[override]
5052
def __rsub__(self, other: Period) -> Index: ...
5153
@overload

pandas-stubs/core/indexes/timedeltas.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ class TimedeltaIndex(
5858
self, other: dt.timedelta | Timedelta | Self
5959
) -> Self: ...
6060
def __radd__(self, other: dt.datetime | Timestamp | DatetimeIndex) -> DatetimeIndex: ... # type: ignore[override]
61-
def __sub__(self, other: dt.timedelta | Timedelta | Self) -> Self: ...
61+
def __sub__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
62+
self, other: dt.timedelta | Timedelta | Self
63+
) -> Self: ...
6264
def __mul__(self, other: num) -> Self: ...
6365
@overload # type: ignore[override]
6466
def __truediv__(self, other: num | Sequence[float]) -> Self: ...

0 commit comments

Comments
 (0)