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
4 changes: 3 additions & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,8 @@ class TimestampSeries(Series[Timestamp]):
# ignore needed because of mypy
@property
def dt(self) -> TimestampProperties: ... # type: ignore[override]
@overload
def __add__(self, other: Timedelta | np.timedelta64) -> TimedeltaSeries: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

result here of adding TimestampSeries plus a timedelta is TimestampSeries

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 here there is no TimestampSeries

Copy link
Collaborator

Choose a reason for hiding this comment

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

Look at line 1742


class TimedeltaSeries(Series[Timedelta]):
# ignores needed because of mypy
Expand All @@ -1753,7 +1755,7 @@ class TimedeltaSeries(Series[Timedelta]):
@overload
def __add__(self, other: Timedelta | np.timedelta64) -> TimedeltaSeries: ...
def __radd__(self, pther: Timestamp | TimestampSeries) -> TimestampSeries: ... # type: ignore[override]
def __mul__(self, other: num | np.timedelta64) -> TimedeltaSeries: ... # type: ignore[override]
def __mul__(self, other: num) -> TimedeltaSeries: ... # type: ignore[override]
def __sub__( # type: ignore[override]
self, other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64
) -> TimedeltaSeries: ...
Expand Down
28 changes: 8 additions & 20 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,23 +1042,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")
check(assert_type((s1 - td), TimestampSeries), np.timedelta64)
check(assert_type((s1 + td), pd.Series[Any]), np.timedelta64)
check(assert_type((s1 * td), TimedeltaSeries), np.timedelta64)
check(assert_type((s1 / td), pd.Series[float]), np.timedelta64)
check(
assert_type((s3 - td), TimedeltaSeries),
pd.Series,
)
check(
assert_type((s3 + td), TimedeltaSeries),
pd.Series,
)
check(
assert_type((s3 * td), TimedeltaSeries),
pd.Series,
)
check(
assert_type((s3 / td), pd.Series[float]),
pd.Series,
)
check(assert_type((s1 - td), TimestampSeries), pd.Series, pd.Timestamp)
check(assert_type((s1 + td), TimestampSeries), pd.Series, pd.Timestamp)
check(assert_type((s1 * td), TimedeltaSeries), pd.Series, 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.

multipying a TimestampSeries times a Timedelta is not allowed, so have to fix that.

In TimestampSeries, put in a method for __mul__() that takes the two timedelta arguments, and returns Never from typing_extensions

Change the test to assert the type, but do not do the check part.

check(assert_type((s1 / td), TimestampSeries), pd.Series, 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.

dividing a TimestampSEries by a Timedelta is not allowed, so have to fix that

In TimestampSeries, put in a method for __truediv__() that takes the two timedelta arguments, and returns Never from typing_extensions

Change the test to assert the type, but do not do the check part.

check(assert_type((s3 - td), TimedeltaSeries), pd.Series, pd.Timedelta)
check(assert_type((s3 + td), TimedeltaSeries), pd.Series, pd.Timedelta)
check(assert_type((s3 * td), TimedeltaSeries), pd.Series, pd.Timedelta)
Copy link
Collaborator

Choose a reason for hiding this comment

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

multiplying a TimedeltaSeries by a timedelta fails, so have to fix that

In TimedeltaSeries, put in a method for __mul__() that takes the two timedelta arguments, and returns Never from typing_extensions

Change the test to assert the type, but do not do the check part.

Copy link
Contributor Author

@ramvikrams ramvikrams Nov 21, 2022

Choose a reason for hiding this comment

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

Sir in TimedeltaSeries should a __trudiv__() be added because it is showing errors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and sir __mul__() it is still showing errors after adding it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sir in TimedeltaSeries should a __trudiv__() be added because it is showing errors

Yes, you should add that. It is valid to divide TimedeltaSeries by Timedelta or np.timedelta64 to get a Series[float], and to divde by a float to get TimedeltaSeries

Copy link
Collaborator

Choose a reason for hiding this comment

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

and sir __mul__() it is still showing errors after adding it.

Your job to fix them!

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 just not able to fix this error: Name "__mul__" already defined on line 1761 [no-redef] could not find about it over internet also

Copy link
Collaborator

Choose a reason for hiding this comment

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

You probably have __mul__() defined in there twice, with different arguments. You need to put a @overload before each def __mul__() statement

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 still mypy hows the same error

Copy link
Collaborator

Choose a reason for hiding this comment

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

Without seeing your code, I can't see what you are doing wrong. You can push and it will fail and I will tell you what you did wrong.

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

check(assert_type((s3 / td), TimedeltaSeries), pd.Series, np.float)