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

Allow arithmetic operations for list and array type #9188

Closed
lucazanna opened this issue Jun 2, 2023 · 7 comments · Fixed by #17823
Closed

Allow arithmetic operations for list and array type #9188

lucazanna opened this issue Jun 2, 2023 · 7 comments · Fixed by #17823
Labels
enhancement New feature or an improvement of an existing feature

Comments

@lucazanna
Copy link

Problem description

I wish Polars could allows arithmetic operations for list and array types with an inner numeric data type.

Taking an example from this GitHub question: https://stackoverflow.com/questions/76389832/polars-how-to-add-two-series-that-contain-lists-as-elements

I would be nice to be able to do:

a = pl.Series("a",[[1,2],[2,3]])
b = pl.Series("b",[[4,5],[6,7]])
c = a+b

Currently, it panicks: PanicException: add operation not supported for dtype list[i64]

@lucazanna lucazanna added the enhancement New feature or an improvement of an existing feature label Jun 2, 2023
@cmdlineluser
Copy link
Contributor

While experimenting with the example I found it interesting you can add structs:

a.list.to_struct() + b.list.to_struct()

# shape: (2,)
# Series: 'a' [struct[2]]
# [
# 	{5,7}
# 	{8,10}
# ]

Although it only works with 2d, and requires turning back into a list.

@lucazanna
Copy link
Author

thanks for sharing @cmdlineluser !

I would not have guessed it would work with structs.

@itamarst
Copy link
Contributor

Going to try to implement this.

@cmdlineluser
Copy link
Contributor

It was added for Array #16791

import polars as pl
import numpy as np

a = pl.Series("a",np.array([[1,2],[2,3]]))
b = pl.Series("b",np.array([[4,5],[6,7]]))

a + b

# shape: (2,)
# Series: 'a' [array[i64, 2]]
# [
# 	[5, 7]
# 	[8, 10]
# ]

@itamarst
Copy link
Contributor

Thank you, that'll make it easier for me to implement given the example.

@itamarst
Copy link
Contributor

itamarst commented Jul 23, 2024

OK, it appears to be working at least in the happy path.

Remaining work:

  • Thorough test suite, handling any bugs it catches.
  • Test for division of arrays, since I fixed that as part of this.
  • Make sure there's a followup issue for broadcasting scalars. Simple arithmetic operations on the "list" type columns  #8006 is follow-up for "differently structured lists" which may or may not overlap with scalar math implementation, I will play around and see what I learn. Initial experiments suggest that scalars might be a different issue.

@bike-picket
Copy link

@itamarst is it feasible to add pow support?

(pl.Series("a",[[1,2],[2,3]]) ** pl.Series("a",[[1,2],[2,3]])).to_list()

# Polars 1.8.2 gives the error:
# polars.exceptions.InvalidOperationError: `pow` operation not supported for dtype `list[i64]` as base

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants