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 1 commit
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
7 changes: 6 additions & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,12 @@ 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]
@overload # type: ignore[override]
def __add__(
self, other: TimedeltaSeries | np.timedelta64 | TimestampSeries
) -> TimestampSeries: ...
@overload
def __add__(self, other: Timestamp) -> TimestampSeries: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can't add a Timestamp to a TimestampSeries. Make the return type Never here.

Copy link
Contributor Author

@ramvikrams ramvikrams Nov 23, 2022

Choose a reason for hiding this comment

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

We have issue in line number 266 of tests/time_funcs.py if we change this, it's pyright issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

On that line, change # TODO both: ignore[operator] to # pyright: ignore

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

Expand Down
22 changes: 12 additions & 10 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import (
TYPE_CHECKING,
Any,
NoReturn,
Optional,
Union,
)
Expand All @@ -15,7 +14,10 @@
import pandas as pd
from pandas.core.indexes.numeric import IntegerIndex
import pytz
from typing_extensions import assert_type
from typing_extensions import (
Never,
assert_type,
)

from pandas._libs import NaTType
from pandas._libs.tslibs import BaseOffset
Expand Down Expand Up @@ -1043,11 +1045,11 @@ def timedelta64_and_arithmatic_operator() -> None:
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
assert_type((s3 / td), pd.Series[float])
check(assert_type((s1 - td), TimestampSeries), TimestampSeries, pd.Timestamp)
Copy link
Collaborator

Choose a reason for hiding this comment

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

check(assert_type((s1 - td), "TimestampSeries"), pd.Series, pd.Timestamp)

All the tests should look like this. The type TimestampSeries isn't known at runtime, so you have to surround it with quotes.

you will have to do something similar for TimedeltaSeries and pd.Series[float].

Look elsewhere in the code to see examples

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we also change thte left 3 tests because they cannot pass pytets

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point. Move those 3 tests to the end and do

if TYPE_CHECKING_INVALID_USAGE:
     # test 1 here
     # test 2 here
     # test 3 here

you import TYPE_CHECKING_INVALID_USAGE from tests

check(assert_type((s1 + td), TimestampSeries), TimestampSeries, pd.Timestamp)
assert_type((s1 * td), Never) # pyright: ignore
assert_type((s1 / td), Never) # pyright: ignore
check(assert_type((s3 - td), TimedeltaSeries), TimedeltaSeries, pd.Timedelta)
check(assert_type((s3 + td), TimedeltaSeries), TimedeltaSeries, pd.Timedelta)
assert_type((s3 * td), Never) # pyright: ignore
check(assert_type((s3 / td), pd.Series[float]), pd.Series[float], float)