Skip to content

feat(series): #1098 arithmetic addition #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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

cmp0xff
Copy link
Contributor

@cmp0xff cmp0xff commented Jul 13, 2025

This PR is motivated by #1273 (review).

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]
  • 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 arrays
  • Series

Draft of a plan

  • We can have 4 ~ 5 test scripts for each left operand
  • In each test script, we can have 7 test functions for each binary arithmetic operator
  • In each test function, we test on all right operands. Do not forget the possible four variations, see the example of add.

Checklist

@cmp0xff cmp0xff marked this pull request as ready for review July 13, 2025 16:32
Copy link
Member

@loicdiridollou loicdiridollou left a 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!

@cmp0xff cmp0xff marked this pull request as draft July 14, 2025 16:51
@cmp0xff cmp0xff force-pushed the feature/cmp0xff/1098-__add__-and-__mul__ branch from 3b3f899 to bf79de1 Compare July 15, 2025 09:29
"""Test pd.Series[Any] + Python native scalars"""
i, f, c = 1, 1.0, 1j

# check(assert_type(left + i, pd.Series), pd.Series) # why?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @twoertwein @loicdiridollou , I am making just minimal changes in __add__ now, and trying to understand the behaviour of self: typing.

May I ask why Series[Any] + int now gives Series[int]? From my point of view, what I added is that only Series[int] + int gives Series[int], and Series[Any] as the left operand is irrelevant. Thanks.

Copy link
Member

Choose a reason for hiding this comment

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

I think for Any/unknown, the overload order matters, the int overload is now the first one.

Copy link
Member

Choose a reason for hiding this comment

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

I believe I had a PR or issue where the overload order prevented me when I tried to remove TimestampSeries - unfortuntely, I can't find the PR/issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @twoertwein , thanks for the reply. I have specified self: Series[int], so that for Series[Any] the first overload should not be triggered.

Copy link
Member

Choose a reason for hiding this comment

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

type checkers match the first matching overload, I believe Any/unknown matches anything/the first overload

Copy link
Member

Choose a reason for hiding this comment

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

here is an example where we had issues in the past with the overload order with any/unknown: #571 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think now I understand it better. Is the current MR the direction to go? I just need to add combinations of overloads.

@cmp0xff cmp0xff force-pushed the feature/cmp0xff/1098-__add__-and-__mul__ branch from bf79de1 to 6b592c2 Compare July 15, 2025 15:03
@cmp0xff cmp0xff force-pushed the feature/cmp0xff/1098-__add__-and-__mul__ branch from 02c646d to 8f43efc Compare July 15, 2025 15:25
@cmp0xff cmp0xff changed the title feat(series): #1098 __add__ and __mul__ feat(series): #1098 additions Jul 15, 2025
@cmp0xff cmp0xff changed the title feat(series): #1098 additions feat(series): #1098 arithmetic additions Jul 15, 2025
@cmp0xff cmp0xff changed the title feat(series): #1098 arithmetic additions feat(series): #1098 arithmetic addition Jul 15, 2025
@cmp0xff cmp0xff marked this pull request as ready for review July 15, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Addition of a Series[int] with a complex returns Series[unknown]
3 participants