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

Add arithmetic for arr namespaces #14541

Closed
mcrumiller opened this issue Feb 16, 2024 · 3 comments · Fixed by #17823
Closed

Add arithmetic for arr namespaces #14541

mcrumiller opened this issue Feb 16, 2024 · 3 comments · Fixed by #17823
Labels
A-dtype-list/array Area: list/array data type accepted Ready for implementation enhancement New feature or an improvement of an existing feature

Comments

@mcrumiller
Copy link
Contributor

Description

Arrays are equal length series and we should be able to add, subtract, multiply, and divide them if they have the same dtype:

import polars as pl

df = pl.DataFrame({
    "a": pl.Series([[1, 2], [3, 4]], dtype=pl.Array(pl.UInt8, 2)),
    "b": pl.Series([[1, 2], [3, 4]], dtype=pl.Array(pl.UInt8, 2)),
})

# any of these fail, seems reasonable they should be implemented
df["a"] + df["b"]
df.select(pl.col("a") + pl.col("b"))
df.select(pl.sum_horizontal("a", "b"))
pyo3_runtime.PanicException: `add` operation not supported for dtype `array[u8, 2]`
@mcrumiller mcrumiller added the enhancement New feature or an improvement of an existing feature label Feb 16, 2024
@stinodego stinodego added the A-dtype-list/array Area: list/array data type label Feb 17, 2024
@ritchie46
Copy link
Member

Yes, we should support this.

@ritchie46 ritchie46 added the accepted Ready for implementation label Jun 4, 2024
@github-project-automation github-project-automation bot moved this to Ready in Backlog Jun 4, 2024
@itamarst
Copy link
Contributor

itamarst commented Jul 23, 2024

These all work now (#16791). However, division is broken because the Python code for division tries to cast to Float64, when it should really be casting in this case to pl.Array(pl.Float64, 2).

Building on the example above:

>>> df["a"] / df["b"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/itamarst/devel/polars/py-polars/polars/series/series.py", line 1082, in __truediv__
    return self.cast(Float64) / other
  File "/home/itamarst/devel/polars/py-polars/polars/series/series.py", line 3992, in cast
    return self._from_pyseries(self._s.cast(dtype, strict, wrap_numerical))
polars.exceptions.InvalidOperationError: cannot cast Array type (inner: 'UInt8', to: 'Float64')

@itamarst
Copy link
Contributor

I will probably fix the division issue as part of #9188, where I have the same problem with List.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dtype-list/array Area: list/array data type accepted Ready for implementation enhancement New feature or an improvement of an existing feature
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants