Skip to content

added np.timedelta64 for series arithmatic methods #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ from pandas.core.window.rolling import (
Rolling,
Window,
)
from typing_extensions import TypeAlias
from typing_extensions import (
Never,
TypeAlias,
)
import xarray as xr

from pandas._libs.missing import NAType
Expand Down Expand Up @@ -1211,7 +1214,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
def __add__(self, other: Timestamp) -> TimestampSeries: ...
@overload
def __add__(
self, other: num | _str | Timedelta | _ListLike | Series[S1]
self, other: num | _str | Timedelta | _ListLike | Series[S1] | np.timedelta64
) -> Series: ...
# ignore needed for mypy as we want different results based on the arguments
@overload
Expand Down Expand Up @@ -1243,7 +1246,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
def __le__(self, other: S1 | _ListLike | Series[S1]) -> Series[_bool]: ...
def __lt__(self, other: S1 | _ListLike | Series[S1]) -> Series[_bool]: ...
@overload
def __mul__(self, other: Timedelta | TimedeltaSeries) -> TimedeltaSeries: ...
def __mul__(
self, other: Timedelta | TimedeltaSeries | np.timedelta64
) -> TimedeltaSeries: ...
@overload
def __mul__(self, other: num | _ListLike | Series) -> Series: ...
def __mod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
Expand Down Expand Up @@ -1304,17 +1309,19 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
) -> TimedeltaSeries: ...
@overload
def __sub__(
self: Series[Timestamp], other: Timedelta | TimedeltaSeries | TimedeltaIndex
self: Series[Timestamp],
other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
) -> TimestampSeries: ...
@overload
def __sub__(
self: Series[Timedelta], other: Timedelta | TimedeltaSeries | TimedeltaIndex
self: Series[Timedelta],
other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
) -> TimedeltaSeries: ...
@overload
def __sub__(self, other: num | _ListLike | Series) -> Series: ...
@overload
def __truediv__(
self, other: Timedelta | TimedeltaSeries | TimedeltaIndex
self, other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64
) -> Series[float]: ...
@overload
def __truediv__(self, other: num | _ListLike | Series[S1]) -> Series: ...
Expand Down Expand Up @@ -1739,6 +1746,9 @@ class TimestampSeries(Series[Timestamp]):
# ignore needed because of mypy
@property
def dt(self) -> TimestampProperties: ... # type: ignore[override]
def __add__(self, other: TimedeltaSeries | np.timedelta64 | TimestampSeries | Timestamp) -> TimestampSeries: ... # type: ignore[override]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove TimestampSeries | Timestamp here as you can't add two timestamps

def __mul__(self, other: TimestampSeries | np.timedelta64 | TimedeltaSeries) -> Never: ... # type: ignore[override]
def __truediv__(self, other: TimestampSeries | np.timedelta64 | TimedeltaSeries) -> Never: ... # type: ignore[override]

class TimedeltaSeries(Series[Timedelta]):
# ignores needed because of mypy
Expand All @@ -1747,12 +1757,18 @@ class TimedeltaSeries(Series[Timedelta]):
@overload
def __add__(self, other: Timestamp | DatetimeIndex) -> TimestampSeries: ...
@overload
def __add__(self, other: Timedelta) -> TimedeltaSeries: ...
def __add__(self, other: Timedelta | np.timedelta64) -> TimedeltaSeries: ...
def __radd__(self, pther: Timestamp | TimestampSeries) -> TimestampSeries: ... # type: ignore[override]
def __mul__(self, other: num) -> TimedeltaSeries: ... # type: ignore[override]
@overload # type: ignore[override]
def __mul__(
self, other: TimestampSeries | np.timedelta64 | Timedelta | TimedeltaSeries
) -> Never: ...
@overload
def __mul__(self, other: num) -> TimedeltaSeries: ...
def __sub__( # type: ignore[override]
self, other: Timedelta | TimedeltaSeries | TimedeltaIndex
self, other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64
) -> TimedeltaSeries: ...
def __truediv__(self, other: TimedeltaSeries | np.timedelta64 | TimedeltaIndex) -> Series[float]: ... # type: ignore[override]
@property
def dt(self) -> TimedeltaProperties: ... # type: ignore[override]

Expand Down
16 changes: 16 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import (
TYPE_CHECKING,
Any,
NoReturn,
Optional,
Union,
)
Expand Down Expand Up @@ -1035,3 +1036,18 @@ def test_timedelta_range() -> None:
def test_dateoffset_freqstr() -> None:
offset = DateOffset(minutes=10)
check(assert_type(offset.freqstr, str), str)


def timedelta64_and_arithmatic_operator() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name of any test function must begin with test_. If you make that change, you will see that the tests will fail. They were never getting tested.

You will have to change the tests as well. See example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir even after adding the test_ the poe mypy test doesn't fail on my system

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do poe test to run all tests or poe pytest to just run pytest which is where the failures will occur

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sir there were errors, I'll fix it then

s1 = pd.Series(data=pd.date_range("1/1/2020", "2/1/2020"))
s2 = pd.Series(data=pd.date_range("1/1/2021", "2/1/2021"))
s3 = s2 - s1
td = np.timedelta64(1, "M")
assert_type((s1 - td), TimestampSeries)
assert_type((s1 + td), TimestampSeries)
assert_type((s1 * td), NoReturn) # pyright: ignore
assert_type((s1 / td), NoReturn) # pyright: ignore
assert_type((s3 - td), TimedeltaSeries)
assert_type((s3 + td), TimedeltaSeries)
assert_type((s3 * td), NoReturn) # pyright: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do the following:

  1. for the 4 cases where you are expecting TimestampSeries or TimedeltaSeries, use check(assert_type( operation, expected_type), expected_type, expected_subtype) where operation is the operation you are testing, expected_type is the type expected, and expected_subtype is pd.Timestamp or pd.Timedelta as appropriate
  2. For the 3 cases where you have NoReturn, can you change that to Never , which you will need to import from typing_extensions
  3. The # pyright: ignore is correct - pyright is doing the right thing here, but mypy is not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir regarding point 2 and 3 when I changed it to Never pyrigth shows error, mypy works ok and if we add the #type: ignore both work fine so should I add it.
Because in the pyright error it says "assert_type" mismatch: expected "Never" but received "Unknown"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you will still need the # pyright: ignore where you had it before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done sir

assert_type((s3 / td), pd.Series[float])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use check on this one too