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(rust): allow get access to list of categoricals #14015

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions crates/polars-ops/src/chunked_array/list/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,12 @@ pub trait ListNameSpaceImpl: AsList {
.downcast_iter()
.map(|arr| sublist_get(arr, idx))
.collect::<Vec<_>>();
Series::try_from((ca.name(), chunks))
.unwrap()
.cast(&ca.inner_dtype())
// Safety: every element in list has dtype equal to its inner type
unsafe {
Series::try_from((ca.name(), chunks))
.unwrap()
.cast_unchecked(&ca.inner_dtype())
}
}

#[cfg(feature = "list_gather")]
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/namespaces/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ def test_list_arr_get() -> None:
) == {"lists": [None, None, 4]}


def test_list_categorical_get() -> None:
df = pl.DataFrame(
{
"actions": pl.Series(
[["a", "b"], ["c"], [None], None], dtype=pl.List(pl.Categorical)
),
}
)
expected = pl.Series("actions", ["a", "c", None, None], dtype=pl.Categorical)
assert_series_equal(df["actions"].list.get(0), expected, categorical_as_str=True)


def test_contains() -> None:
a = pl.Series("a", [[1, 2, 3], [2, 5], [6, 7, 8, 9]])
out = a.list.contains(2)
Expand Down
Loading