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

Implement methods like "get", "first", "last" from list namespace in Array namespace #9718

Closed
Marlin-Na opened this issue Jul 5, 2023 · 3 comments · Fixed by #20999
Closed
Labels
enhancement New feature or an improvement of an existing feature

Comments

@Marlin-Na
Copy link

Problem description

It seems that Array type lacks some essential access methods like get in list namespace.

It would also be helpful to implement a to_list method to convert an Array type to List type.

@Marlin-Na Marlin-Na added the enhancement New feature or an improvement of an existing feature label Jul 5, 2023
@ritchie46
Copy link
Member

It would also be helpful to implement a to_list method to convert an Array type to List type.

There is, you can can cast.

@Marlin-Na
Copy link
Author

A follow up request for this is that if it is possible to implement non-strict type cast from array to list.

Currently if I cast to List without explicitly specifying the inner dtype, it will be casted to List(Null). It can be quite surprising to users.

>>> df = pl.DataFrame({"foo": [[1,2], [1,None]]}, {"foo": pl.Array(2, pl.Int32())})
>>> df
shape: (2, 1)
┌───────────────┐
│ foo           │
│ ---           │
│ array[i32, 2] │
╞═══════════════╡
│ [1, 2]        │
│ [1, null]     │
└───────────────┘
>>> df.select(pl.col.foo.cast(pl.List))
shape: (2, 1)
┌──────────────┐
│ foo          │
│ ---          │
│ list[null]   │
╞══════════════╡
│ [null, null] │
│ [null, null] │
└──────────────┘

@robinvd
Copy link

robinvd commented Oct 30, 2023

Im also missing the eval function from lists, or is there a already a way to do this?

example to add 2 to every array element

>> df = pl.DataFrame({"a": pl.Series("a", [[1,2], [3,4]], dtype=pl.Array(width=2, inner=pl.Int64))})
>> df.select(pl.col("a").cast(pl.List(inner=pl.Int64)).list.eval(pl.element() + 2).cast(pl.Array(2, inner=pl.Int64)))
shape: (2, 1)
┌───────────────┐
│ a             │
│ ---           │
│ array[i64, 2] │
╞═══════════════╡
│ [3, 4]        │
│ [5, 6]        │
└───────────────┘

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.

3 participants