-
-
Notifications
You must be signed in to change notification settings - Fork 146
feat(series): #1098 __add__ and __mul__ #1275
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
base: main
Are you sure you want to change the base?
feat(series): #1098 __add__ and __mul__ #1275
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like great progress here!
tests/test_series.py
Outdated
|
||
def test_types_scalar_arithmetic() -> None: | ||
# TODO: assert_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to keep the TODO here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thank you for jumping in. I quickly went through #1093 and recognised that it was important to define a scope within which we want to address the (original) issue.
I think the current MR has addressed most of the original #1098, isn't it?
The TODOs are for a more comprehensive plan, to rectify all possible arithmetic operations, which I think is beyond the scope of the original #1098.
tests/test_series.py
Outdated
|
||
check(assert_type(s - 1.0, pd.Series), pd.Series, np.floating) | ||
check(assert_type(1.0 - s, pd.Series), pd.Series, np.floating) | ||
# check( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to keep the code here it may be worth adding a TODO
and a reason for when we can uncomment it
pandas-stubs/core/series.pyi
Outdated
_T_COMPLEX = TypeVar("_T_COMPLEX", bound=complex) | ||
_T_INT_FLOAT = TypeVar("_T_INT_FLOAT", bound=int | float) | ||
_T_FLOAT_COMPLEX = TypeVar("_T_FLOAT_COMPLEX", bound=float | complex) | ||
_T_INT_FLOAT_COMPLEX = TypeVar("_T_INT_FLOAT_COMPLEX", bound=int | float | complex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aren't _T_FLOAT_COMPLEX
, _T_COMPLEX
, and _T_INT_FLOAT_COMPLEX
from a typing persepctive the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? They have different bound
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function that accepts a complex, is also assumed (from a typing perspective) to accept a float or an int https://peps.python.org/pep-3141/#abstract even though they aren't sub-classes at runtime
pandas-stubs/core/series.pyi
Outdated
self: Series[_T_COMPLEX], | ||
other: ( | ||
_T_INT_FLOAT_COMPLEX | ||
| Sequence[_T_INT_FLOAT_COMPLEX] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sequence[_T_INT_FLOAT_COMPLEX]
can probably be Sequence[complex]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My idea was that Series[float] + Series[complex] -> Series[complex]
and Series[comples] + Series[float] -> Series[complex]
are two different things. It addresses the latter point here.
This PR is motivated by #1273 (review) and partially addresses #1098 and updates
__add__
and__mul__
.The task proved to be tricky, and I have the feeling that pyright and mypy do not support
self
typing very well. I did not make__sub__
and__div__
as well as__truediv__
work.Comprehensive plan (maybe in another PR)
Inspired by #1093, one can draft the following comprehensive plan for all possible combinations of the left arithmetic operand, the binary arithmetic operator and the right arithmetic operand.
Left operand
Series[Any]
andSeries[Unknown]
Series[int]
Series[float]
Series[complex]
Binary arithmetic operators
Include their right-version and functional version. In
add
we give a full example. We follow the official documentation.__add__
,__radd__
,add
,radd
__truediv__
__floordiv__
__pow__
__mod__
__mul__
__sub__
Right operand
Scalar
Sequence
numpy
arraysSeries
Draft of a plan
add
.Checklist
assert_type()
to assert the type of any return value