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(python): Panic on empty df / null List(Categorical) #16730

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion crates/polars-core/src/series/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ impl Series {
#[cfg(feature = "dtype-categorical")]
dt @ (Categorical(rev_map, ordering) | Enum(rev_map, ordering)) => {
let cats = UInt32Chunked::from_chunks(name, chunks);
let rev_map = rev_map
.clone()
.unwrap_or_else(|| Arc::new(RevMapping::default()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should assert cats is emtpy if we set an empty rev map.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should add it directly in from_cats_and_rev_map_unchecked under cfg(debug_assertions)?
But this seems to be the only place when they're unwrapped, so I added it just here for now.

let mut ca = CategoricalChunked::from_cats_and_rev_map_unchecked(
cats,
rev_map.clone().unwrap(),
rev_map,
matches!(dt, Enum(_, _)),
*ordering,
);
Expand Down
19 changes: 19 additions & 0 deletions py-polars/tests/unit/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,3 +832,22 @@ def test_list_str_sum_exception_12935() -> None:
def test_list_list_sum_exception_12935() -> None:
with pytest.raises(pl.exceptions.InvalidOperationError):
pl.Series([[1], [2]]).sum()


def test_null_list_categorical_16405() -> None:
df = pl.DataFrame(
[(None, "foo")],
schema={
"match": pl.List(pl.Categorical),
"what": pl.Categorical,
},
)

df = df.select(
pl.col("match")
.list.set_intersection(pl.concat_list(pl.col("what")))
.alias("result")
)

expected = pl.DataFrame([None], schema={"result": pl.List(pl.Categorical)})
assert_frame_equal(df, expected)
5 changes: 5 additions & 0 deletions py-polars/tests/unit/test_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@ def test_empty_to_empty(method: str) -> None:
def test_empty_shift_over_16676() -> None:
df = pl.DataFrame({"a": [], "b": []})
assert df.with_columns(pl.col("a").shift(fill_value=0).over("b")).shape == (0, 2)


def test_empty_list_cat_16405() -> None:
df = pl.DataFrame(schema={"cat": pl.List(pl.Categorical)})
df.select(pl.col("cat") == pl.col("cat"))
Loading