Skip to content

Commit a66ea6b

Browse files
mutricylLaurent Mutricy
and
Laurent Mutricy
authored
748 series operator timedelta (#924)
* adding tests for #748 * updating __rmul__, rmul and mul --------- Co-authored-by: Laurent Mutricy <[email protected]>
1 parent a251e5a commit a66ea6b

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

pandas-stubs/core/series.pyi

+27-4
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,11 @@ class Series(IndexOpsMixin[S1], NDFrame):
15731573
def __rdivmod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
15741574
def __rfloordiv__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
15751575
def __rmod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
1576+
@overload
1577+
def __rmul__(
1578+
self, other: timedelta | Timedelta | TimedeltaSeries | np.timedelta64
1579+
) -> TimedeltaSeries: ...
1580+
@overload
15761581
def __rmul__(self, other: num | _ListLike | Series) -> Series: ...
15771582
def __rnatmul__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
15781583
def __rpow__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
@@ -1794,13 +1799,22 @@ class Series(IndexOpsMixin[S1], NDFrame):
17941799
fill_value: float | None = ...,
17951800
axis: AxisIndex | None = ...,
17961801
) -> Series[S1]: ...
1802+
@overload
17971803
def mul(
17981804
self,
1799-
other: num | _ListLike | Series[S1],
1805+
other: timedelta | Timedelta | TimedeltaSeries | np.timedelta64,
18001806
level: Level | None = ...,
18011807
fill_value: float | None = ...,
18021808
axis: AxisIndex | None = ...,
1803-
) -> Series[S1]: ...
1809+
) -> TimedeltaSeries: ...
1810+
@overload
1811+
def mul(
1812+
self,
1813+
other: num | _ListLike | Series,
1814+
level: Level | None = ...,
1815+
fill_value: float | None = ...,
1816+
axis: AxisIndex | None = ...,
1817+
) -> Series: ...
18041818
def multiply(
18051819
self,
18061820
other: num | _ListLike | Series[S1],
@@ -1869,13 +1883,22 @@ class Series(IndexOpsMixin[S1], NDFrame):
18691883
fill_value: float | None = ...,
18701884
axis: AxisIndex = ...,
18711885
) -> Series[S1]: ...
1886+
@overload
18721887
def rmul(
18731888
self,
1874-
other: Series[S1] | Scalar,
1889+
other: timedelta | Timedelta | TimedeltaSeries | np.timedelta64,
18751890
level: Level | None = ...,
18761891
fill_value: float | None = ...,
18771892
axis: AxisIndex = ...,
1878-
) -> Series[S1]: ...
1893+
) -> TimedeltaSeries: ...
1894+
@overload
1895+
def rmul(
1896+
self,
1897+
other: num | _ListLike | Series,
1898+
level: Level | None = ...,
1899+
fill_value: float | None = ...,
1900+
axis: AxisIndex = ...,
1901+
) -> Series: ...
18791902
@overload
18801903
def rolling(
18811904
self,

tests/test_series.py

+25
Original file line numberDiff line numberDiff line change
@@ -3214,3 +3214,28 @@ def test_diff_never3() -> None:
32143214
if TYPE_CHECKING_INVALID_USAGE:
32153215
# str -> TypeError: unsupported operand type(s) for -: 'str' and 'str'
32163216
assert_never(pd.Series(["a", "b"]).diff())
3217+
3218+
3219+
def test_operator_constistency() -> None:
3220+
# created for #748
3221+
s = pd.Series([1, 2, 3])
3222+
check(
3223+
assert_type(s * np.timedelta64(1, "s"), "TimedeltaSeries"),
3224+
pd.Series,
3225+
pd.Timedelta,
3226+
)
3227+
check(
3228+
assert_type(np.timedelta64(1, "s") * s, "TimedeltaSeries"),
3229+
pd.Series,
3230+
pd.Timedelta,
3231+
)
3232+
check(
3233+
assert_type(s.mul(np.timedelta64(1, "s")), "TimedeltaSeries"),
3234+
pd.Series,
3235+
pd.Timedelta,
3236+
)
3237+
check(
3238+
assert_type(s.rmul(np.timedelta64(1, "s")), "TimedeltaSeries"),
3239+
pd.Series,
3240+
pd.Timedelta,
3241+
)

0 commit comments

Comments
 (0)