Skip to content
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

fix: Make Array arithmetic ops fully elementwise #19682

Merged
merged 2 commits into from
Nov 7, 2024

Conversation

coastalwhite
Copy link
Collaborator

Fixes #19679.

Along side two other issues.

Issue 1:

import polars as pl

a = pl.Series('a', [[[1, 2, 3], [4, 5, 6]]], pl.Array(pl.Int64, (2, 3)))

print(a + pl.Series([1]))
print(pl.Series([1]) + a)
shape: (1,)
Series: 'a' [array[i64, (2, 3)]]
[
    [[2, 3, 4], [5, 6, 7]]
]
Traceback (most recent call last):
  File "/home/johndoe/Projects/polars/array_elementwise_bug.py", line 6, in <module>
    print(pl.Series([1]) + a)
          ~~~~~~~~~~~~~~~^~~
  File "/home/johndoe/Projects/polars/py-polars/polars/series/series.py", line 1045, in __add__
    return self._arithmetic(other, "add", "add_<>")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johndoe/Projects/polars/py-polars/polars/series/series.py", line 994, in _arithmetic
    return self._from_pyseries(getattr(self._s, op_s)(other._s))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
polars.exceptions.InvalidOperationError: add operation not supported for dtypes `i64` and `array[i64, (2, 3)]`

Issue 2:

import polars as pl

a = pl.Series('a', [[[1, 2, 3], [4, 5, 6]]], pl.Array(pl.Int64, (2, 3)))
print(a + a)
shape: (3,)
Series: 'a' [array[i64, 2]]
[
    [2, 4]
    [6, 8]
    [10, 12]
]

Fixes pola-rs#19679.

Along side two other issues.

Issue 1:

```python
import polars as pl

a = pl.Series('a', [[[1, 2, 3], [4, 5, 6]]], pl.Array(pl.Int64, (2, 3)))

print(a + pl.Series([1]))
print(pl.Series([1]) + a)
```

```console
shape: (1,)
Series: 'a' [array[i64, (2, 3)]]
[
    [[2, 3, 4], [5, 6, 7]]
]
Traceback (most recent call last):
  File "/home/johndoe/Projects/polars/array_elementwise_bug.py", line 6, in <module>
    print(pl.Series([1]) + a)
          ~~~~~~~~~~~~~~~^~~
  File "/home/johndoe/Projects/polars/py-polars/polars/series/series.py", line 1045, in __add__
    return self._arithmetic(other, "add", "add_<>")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/johndoe/Projects/polars/py-polars/polars/series/series.py", line 994, in _arithmetic
    return self._from_pyseries(getattr(self._s, op_s)(other._s))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
polars.exceptions.InvalidOperationError: add operation not supported for dtypes `i64` and `array[i64, (2, 3)]`
```

Issue 2:

```python
import polars as pl

a = pl.Series('a', [[[1, 2, 3], [4, 5, 6]]], pl.Array(pl.Int64, (2, 3)))
print(a + a)
```

```console
shape: (3,)
Series: 'a' [array[i64, 2]]
[
    [2, 4]
    [6, 8]
    [10, 12]
]
```
Copy link

codecov bot commented Nov 7, 2024

Codecov Report

Attention: Patch coverage is 77.64706% with 19 lines in your changes missing coverage. Please review.

Project coverage is 79.71%. Comparing base (4b03406) to head (48f044a).
Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
...ates/polars-core/src/series/arithmetic/borrowed.rs 75.64% 19 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #19682      +/-   ##
==========================================
- Coverage   79.73%   79.71%   -0.03%     
==========================================
  Files        1541     1542       +1     
  Lines      212227   212207      -20     
  Branches     2449     2449              
==========================================
- Hits       169226   169167      -59     
- Misses      42446    42485      +39     
  Partials      555      555              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ritchie46 ritchie46 merged commit ee92ff8 into pola-rs:main Nov 7, 2024
25 checks passed
tylerriccio33 pushed a commit to tylerriccio33/polars that referenced this pull request Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array arithmetic is not elementwise for zero-width arrays
2 participants