You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "rmnskb (via GitHub)" <gi...@apache.org> on 2025/11/08 00:59:45 UTC

[PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

rmnskb opened a new pull request, #48085:
URL: https://github.com/apache/arrow/pull/48085

   ### Rationale for this change
   Please see #32007, currently, neither arrays nor scalars support Python-native arithmetic operations, such as `array + array`, it has to be done via `pyarrow.compute` API. This PR strives to fix this with custom dunder methods.
   
   ### What changes are included in this PR?
   Implemented dunder methods
   
   ### Are these changes tested?
   Yes
   
   ### Are there any user-facing changes?
   Possibility to use Python operators directly instead of calling the `pyarrow.compute` API.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3593466344

   @AlenkaF @rok thanks for your input on this PR, I've marked it as ready, so I think it's up for a review, please let me know if there is anything else I should include, thanks! :)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3505533407

   @pitrou @AlenkaF @rok
   Sorry for pinging like this, I wanted to understand the scope correctly. So far, I went with the most straightforward implementation and added dunders for all operations that were listed [in the compute docs](https://arrow.apache.org/docs/python/api/compute.html#arithmetic-functions) and had respective dunders available. Do you think this implementation is enough or should I go for other dunders as well, e.g bitwise operations? After reviewing the original discussion, I've seen @jorisvandenbossche suggesting checked versions of the functions, I think I will go with them as well. 
   Regarding the documentation: 
   1. Should I add docstrings to dunder methods? Given that user won't see them in their IDE, for example.
   2. Should the documentation for arrays/scalars be updated to reflect that the native operators are now supported?
   Thanks for your input!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3520596167

   Thank you for working on this @rmnskb and sorry for replying late!
   Pinging us when you have a question is totally fine and in fact necessary ;)
   
   I think the current scope is solid. I am thinking of basic comparisons and the discussion in the issue around `__eq__`. That would also be good to tackle but maybe as a separate PR?
   
   Yes, it would be important to update the documentation (User Guide which is the part that is not API reference related).
   
   One comment after looking at the code would be to add tests that cover various type combinations (array, scalar, Python types, unsupported types ...)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3532197609

   > I'll be happy to work on it, once I'm done with this implementation :)
   
   Thank you!
    
   > Are you talking about [this file](https://github.com/apache/arrow/blob/main/docs/source/python/api/arrays.rst)? Or is there somewhere else, where I can put this information?
   
   I was thinking more about the compute page in our User Guide: https://github.com/apache/arrow/blob/main/docs/source/python/compute.rst
   
   > Are there any particular unsupported types I should include that you have in mind?
   
   No, not really. What comes to mind are strings and/or nested for some arithmetic functions.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3654526351

   Two general quick comments:
   
   - The `&` / `|` operators are currently mapped to the bitwise kernels, but in array-land, I think the most common use case for those is actually for logical (boolean) operations. So as a user, I would expect those to also work on bool type.
   - Do we want to add the round/floor/ceil/trunc rounding dunders? Numpy does not support those, so here I wouldn't have expected them. But maybe it can also be useful (I think it find it a bit strange looking that a stdlib builtin function returns a pyarrow array, but that is probably a question of getting used to it). One difference is that the builtin is documented to return an int, and our kernels don't do that (float stays float)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rok (via GitHub)" <gi...@apache.org>.
rok commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3521683283

   Oh, I managed to forget this and thought `array.equals` dispatches to `compute.equals` already :facepalm:.
   
   It seems like a good opportunity to change this! :)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #48085:
URL: https://github.com/apache/arrow/pull/48085#discussion_r2769319375


##########
python/pyarrow/tests/test_array.py:
##########
@@ -4398,3 +4399,67 @@ def test_non_cpu_array():
         arr.tolist()
     with pytest.raises(NotImplementedError):
         arr.validate(full=True)
+
+
+@pytest.fixture
+def int_arrays():
+    arr1 = pa.array([-1, 2, -3])
+    arr2 = pa.array([2, 4, 5])
+
+    return arr1, arr2
+
+
+@pytest.fixture
+def float_arrays():
+    arr1 = pa.array([-1.1, 2.2, -3.3])
+    arr2 = pa.array([2.2, 4.4, 5.5])
+
+    return arr1, arr2
+
+
+def test_arithmetic_dunders(float_arrays):
+    # GH-32007
+    arr1, arr2 = float_arrays
+
+    assert (arr1 + arr2).equals(pc.add_checked(arr1, arr2))

Review Comment:
   If we really want to assert the "checked" aspect, then we should also include a case where overflow occurs.



##########
python/pyarrow/tests/test_scalars.py:
##########
@@ -995,3 +995,55 @@ def test_map_scalar_with_empty_values():
     s = pa.scalar(v, type=map_type)
 
     assert s.as_py(maps_as_pydicts="strict") == v
+
+
+@pytest.fixture

Review Comment:
   Same comments here.



##########
python/pyarrow/array.pxi:
##########
@@ -229,6 +235,25 @@ def array(object obj, type=None, mask=None, size=None, from_pandas=None,
     >>> arr = pa.array(range(1024), type=pa.dictionary(pa.int8(), pa.int64()))
     >>> arr.type.index_type
     DataType(int16)
+
+    >>> arr1 = pa.array([1, 2, 3], type=pa.int8())
+    >>> arr2 = pa.array([4, 5, 6], type=pa.int8())
+    >>> arr1 + arr2
+    <pyarrow.lib.Int8Array object at ...>
+    [
+      5,
+      7,
+      9
+    ]
+
+    >>> val = pa.scalar(42)
+    >>> val - arr1

Review Comment:
   Can I simply call `arr1 - 42` or would that not work?



##########
python/pyarrow/tests/test_array.py:
##########
@@ -4398,3 +4399,67 @@ def test_non_cpu_array():
         arr.tolist()
     with pytest.raises(NotImplementedError):
         arr.validate(full=True)
+
+
+@pytest.fixture

Review Comment:
   I don't think these fixtures are useful and their names seem too generic to me. Testing on a single example per test should be sufficient IMHO.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3845926788

   Thank you for the update @rmnskb!
   The documentation CI failures do not look related. Sphinx job seems to start failing with pandas 3.0 and needs a separate fix (will open an issue if it is not opened yet). The Debian job is connected to R documentation, I need to investigate that also (as a separate issue).
   
   I think the latest change addresses the last comments, @pitrou or @raulcd - mind giving another look at the changes?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3629172810

   Hey, @AlenkaF, good points! Thanks for the review. I've added another test and updated the docs as per your suggestion.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rok (via GitHub)" <gi...@apache.org>.
rok commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3518053328

   Sorry for late reply @rmnskb. I think all dunders that semantically map to our existing kernels are fair game! I'm not sure what these would be, but the set you have now looks decently sized.
   
   > Should I add docstrings to dunder methods? Given that user won't see them in their IDE, for example.
   
   We might be able to add docstrings via type annotations. Or perhaps it's possible to copy them at runtime from the respective kernels? (unsure that is possible). In any case we probably don't want to duplicate docstrings in general.
   
   > Should the documentation for arrays/scalars be updated to reflect that the native operators are now supported?
   
   Yes, that would be good to have!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3529762464

   > I think the current scope is solid. I am thinking of basic comparisons and the discussion in the issue around `__eq__`. That would also be good to tackle but maybe as a separate PR?
   
   I'll be happy to work on it, once I'm done with this implementation :)
   
   > Yes, it would be important to update the documentation (User Guide which is the part that is not API reference related).
   
   Are you talking about [this file](https://github.com/apache/arrow/blob/main/docs/source/python/api/arrays.rst)? Or is there somewhere else, where I can put this information?
   
   > One comment after looking at the code would be to add tests that cover various type combinations (array, scalar, Python types, unsupported types ...)
   
   Yes, I agree. Are there any particular unsupported types I should include that you have in mind? 
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3533133153

   > > Are you talking about [this file](https://github.com/apache/arrow/blob/main/docs/source/python/api/arrays.rst)? Or is there somewhere else, where I can put this information?
   > 
   > I was thinking more about the compute page in our User Guide: https://github.com/apache/arrow/blob/main/docs/source/python/compute.rst
   
   <img width="664" height="878" alt="Operators Documentation" src="https://github.com/user-attachments/assets/d1758ea3-755c-4f5e-b691-42cf0adf6240" />
   
   I mentioned the newly implemented operators in the documentation. Please let me know if it makes sense. I'm not sure whether we should list all the implemented methods, on the other hand, I don't want to leave users guessing what exactly can they use.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3746658841

   > The `&` / `|` operators are currently mapped to the bitwise kernels, but in array-land, I think the most common use case for those is actually for logical (boolean) operations. So as a user, I would expect those to also work on bool type.
   
   That is true, I also though about it, but it seems to me that being consistent with other dunders also makes sense. Should we probably document this? So the users know what to expect.
    
   > Do we want to add the round/floor/ceil/trunc rounding dunders? Numpy does not support those, so here I wouldn't have expected them. But maybe it can also be useful (I think it find it a bit strange looking that a stdlib builtin function returns a pyarrow array, but that is probably a question of getting used to it). One difference is that the builtin is documented to return an int, and our kernels don't do that (float stays float)
   
   Fair point. I think documenting the expected behavior might be a good idea given that Python is quite forgiving when it comes to data types. What do you think? The other option is to explicitly convert the output of this dunders to integers to stay consistent with the builtin docs.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007: [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-4140877240

   Thanks @rmnskb!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3834109450

   We can split this PR in two and move round/floor/etc., ... in a follow-up one if there will be requests for them.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rok (via GitHub)" <gi...@apache.org>.
rok commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3540789453

   > > One comment after looking at the code would be to add tests that cover various type combinations (array, scalar, Python types, unsupported types ...)
   > 
   > Yes, I agree. Are there any particular unsupported types I should include that you have in mind? 
   
   An integer based extension type maybe.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3521454230

   > I think we already have `__eq__`: https://github.com/apache/arrow/pull/7737/files.
   
   Yes, true. I was under the impression that we might want to change the use of `equals()` method with the [equal compute function](https://arrow.apache.org/docs/python/generated/pyarrow.compute.equal.html)? From the issue:
   
   > I think if we add those, we should also change the behaviour of `__eq__`, although that is something that will require a long deprecation cycle.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007: [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF merged PR #48085:
URL: https://github.com/apache/arrow/pull/48085


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on code in PR #48085:
URL: https://github.com/apache/arrow/pull/48085#discussion_r2778058522


##########
python/pyarrow/tests/test_array.py:
##########
@@ -4398,3 +4399,67 @@ def test_non_cpu_array():
         arr.tolist()
     with pytest.raises(NotImplementedError):
         arr.validate(full=True)
+
+
+@pytest.fixture

Review Comment:
   Fair enough, removed those



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3654303370

   @pitrou @raulcd looking for another review before merging, if anybody has time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3529746970

   > We might be able to add docstrings via type annotations. Or perhaps it's possible to copy them at runtime from the respective kernels? (unsure that is possible). In any case we probably don't want to duplicate docstrings in general.
   
   I've looked more into this, and based on [these](https://stackoverflow.com/a/68901244) [two](https://softwareengineering.stackexchange.com/a/386758) discussions, we'd have to copy them directly from the underlying functions at runtime via importing the `pyarrow.compute` module, which AFAIK we don't have access to, since we call them with `_pc().call_function()`.
   I've added the docstrings to respective classes which notes that the API now support calling the Python-native operators directly, as well as provides some examples of that. Please let me know if that's sufficient. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rok (via GitHub)" <gi...@apache.org>.
rok commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3540789502

   > > One comment after looking at the code would be to add tests that cover various type combinations (array, scalar, Python types, unsupported types ...)
   > 
   > Yes, I agree. Are there any particular unsupported types I should include that you have in mind? 
   
   An integer based extension type maybe.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on code in PR #48085:
URL: https://github.com/apache/arrow/pull/48085#discussion_r2600281175


##########
docs/source/python/compute.rst:
##########
@@ -528,3 +528,56 @@ For example, the "numpy_gcd" function that we've been using as an example above
 function to use in a projection.  A "cumulative sum" function would not be a valid function
 since the result of each input row depends on the rows that came before.  A "drop nulls"
 function would also be invalid because it doesn't emit a value for some rows.
+
+
+Standard Python Operators
+=========================
+
+PyArrow supports standard Python operators for element-wise operations for arrays and scalars.
+Currently, the support is limited to some of the standard arithmetic compute functions, namely
+``pc.add, pc.subtract, pc.divide, pc.multiply, pc.abs, pc.negate, pc.power``;
+
+bit-wise functions: ``pc.bit_wise_and, bit_wise_or, bit_wise_xor, shift_left, shift_right``;
+
+and rounding functions: ``pc.round, pc.trunc, pc.floor, pc.ceil``.

Review Comment:
   Fair enough, I've updated the list as you suggested :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3748122467

   Do we want to gold-plate this? We can keep the bitwise operators, remove the round/floor/etc. dunders, and perhaps improve things later if people request it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on code in PR #48085:
URL: https://github.com/apache/arrow/pull/48085#discussion_r2778060695


##########
python/pyarrow/tests/test_array.py:
##########
@@ -4398,3 +4399,67 @@ def test_non_cpu_array():
         arr.tolist()
     with pytest.raises(NotImplementedError):
         arr.validate(full=True)
+
+
+@pytest.fixture
+def int_arrays():
+    arr1 = pa.array([-1, 2, -3])
+    arr2 = pa.array([2, 4, 5])
+
+    return arr1, arr2
+
+
+@pytest.fixture
+def float_arrays():
+    arr1 = pa.array([-1.1, 2.2, -3.3])
+    arr2 = pa.array([2.2, 4.4, 5.5])
+
+    return arr1, arr2
+
+
+def test_arithmetic_dunders(float_arrays):
+    # GH-32007
+    arr1, arr2 = float_arrays
+
+    assert (arr1 + arr2).equals(pc.add_checked(arr1, arr2))

Review Comment:
   The overall intention of this was to test that the dunder methods have the same output given the same input (kernels as well). Albeit, your suggestion is valid, so added extra tests to cover whether dunder methods indeed overflow.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on code in PR #48085:
URL: https://github.com/apache/arrow/pull/48085#discussion_r2778062398


##########
python/pyarrow/array.pxi:
##########
@@ -229,6 +235,25 @@ def array(object obj, type=None, mask=None, size=None, from_pandas=None,
     >>> arr = pa.array(range(1024), type=pa.dictionary(pa.int8(), pa.int64()))
     >>> arr.type.index_type
     DataType(int16)
+
+    >>> arr1 = pa.array([1, 2, 3], type=pa.int8())
+    >>> arr2 = pa.array([4, 5, 6], type=pa.int8())
+    >>> arr1 + arr2
+    <pyarrow.lib.Int8Array object at ...>
+    [
+      5,
+      7,
+      9
+    ]
+
+    >>> val = pa.scalar(42)
+    >>> val - arr1

Review Comment:
   It would work, yes. I wanted to show in the docstrings that the users can also use explicit scalars, that's why I went with this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rmnskb (via GitHub)" <gi...@apache.org>.
rmnskb commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3518285148

   > Sorry for late reply @rmnskb. I think all dunders that semantically map to our existing kernels are fair game! I'm not sure what these would be, but the set you have now looks decently sized.
   
   Ok, I was also thinking about covering as many dunder methods as possible, but did not want to go out of scope for this issue.
   
   > We might be able to add docstrings via type annotations. Or perhaps it's possible to copy them at runtime from the respective kernels? (unsure that is possible). In any case we probably don't want to duplicate docstrings in general.
   
   Copying the docstrings sounds like a good idea, I will look further into that.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007 [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "rok (via GitHub)" <gi...@apache.org>.
rok commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-3521237569

   > I think the current scope is solid. I am thinking of basic comparisons and the discussion in the issue around `__eq__`. That would also be good to tackle but maybe as a separate PR?
   
   I think we already have `__eq__`: https://github.com/apache/arrow/pull/7737/files.
   
   > One comment after looking at the code would be to add tests that cover various type combinations (array, scalar, Python types, unsupported types ...)
   
   That's a good point, `__eq__` currently raises if the types of compared don't match. Good test coverage would also help find edge cases.
   
   ```python
       def __eq__(self, other):
           try:
               return self.equals(other)
           except TypeError:
               # This also handles comparing with None
               # as Array.equals(None) raises a TypeError.
               return NotImplemented
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-32007: [Python] Support arithmetic on arrays and scalars [arrow]

Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #48085:
URL: https://github.com/apache/arrow/pull/48085#issuecomment-4142910992

   After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit a19f13b9431482d5776efc65ad0fac72f012127f.
   
   There were no benchmark performance regressions. 🎉
   
   The [full Conbench report](https://github.com/apache/arrow/runs/68893458113) has more details. It also includes information about 364 possible false positives for unstable benchmarks that are known to sometimes produce them.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org