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

rows_by_key works with pl.Array #18813

Closed
gab23r opened this issue Sep 18, 2024 · 1 comment
Closed

rows_by_key works with pl.Array #18813

gab23r opened this issue Sep 18, 2024 · 1 comment
Labels
enhancement New feature or an improvement of an existing feature

Comments

@gab23r
Copy link
Contributor

gab23r commented Sep 18, 2024

Description

I have this dataframe:

import polars as pl
df = (
    pl.DataFrame([
        pl.Series(name = "a", values = [[0,0], [0,1]], dtype=pl.Array(pl.Int64, 2)),
        pl.Series(name = "b", values = ["a", "b"])
    ])
)
# shape: (2, 2)
# ┌───────────────┬─────┐
# │ a             ┆ b   │
# │ ---           ┆ --- │
# │ array[i64, 2] ┆ str │
# ╞═══════════════╪═════╡
# │ [0, 0]        ┆ a   │
# │ [0, 1]        ┆ b   │
# └───────────────┴─────┘

And I want to transform this dataframe into this dictionnary:
{(0,0): "a", (0, 1): "b"}

The way to do it would be to used df.rows_by_key("a") but it fails with TypeError: unhashable type: 'list'
This is because pl.Array becomes list python object and so cannot be key of a dictionary. But I would be great is pl.Array get transform in tuple.

I think it make a lot of sense overall and this way we can translate any polars dataframe to any builtin python type:

  • pl.List -> list
  • pl.Struct -> dict
  • pl.Array -> tuple
@gab23r gab23r added the enhancement New feature or an improvement of an existing feature label Sep 18, 2024
@gab23r
Copy link
Contributor Author

gab23r commented Nov 6, 2024

I just realized that rows_by_key doesn't work with pl.Array but it takes a list of columns as a key so this workaround works fine and is even better:
python

df.select(c.a.arr.to_struct().struct.unnest(), "b").rows_by_key(("field_0", "field_1"), unique=True)
# {(0, 0): 'a', (0, 1): 'b'}

@gab23r gab23r closed this as completed Nov 6, 2024
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

No branches or pull requests

1 participant