Skip to content

Commit

Permalink
fix(rust, python): categorical construction from null values (pola-rs…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashaag authored and c-peters committed Jul 14, 2023
1 parent 2388246 commit 8424cbe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion polars/polars-core/src/series/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl Series {
DataType::Categorical(_) => {
let ca = if let Some(single_av) = av.first() {
match single_av {
AnyValue::Utf8(_) | AnyValue::Utf8Owned(_) => {
AnyValue::Utf8(_) | AnyValue::Utf8Owned(_) | AnyValue::Null => {
any_values_to_utf8(av, strict)?
}
_ => polars_bail!(
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/datatypes/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,12 @@ def test_fast_unique_flag_from_arrow() -> None:

filtered = df.to_arrow().filter([True, False, True, True, False, True, True, True])
assert pl.from_arrow(filtered).select(pl.col("colB").n_unique()).item() == 4


def test_construct_with_null() -> None:
# Example from https://github.com/pola-rs/polars/issues/7188
df = pl.from_dicts([{"A": None}, {"A": "foo"}], schema={"A": pl.Categorical})
assert df.to_series().to_list() == [None, "foo"]

s = pl.Series([{"struct_A": None}], dtype=pl.Struct({"struct_A": pl.Categorical}))
assert s.to_list() == [{"struct_A": None}]

0 comments on commit 8424cbe

Please sign in to comment.