Skip to content

Commit

Permalink
cast null to categorical (#4254)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Aug 4, 2022
1 parent c4bacff commit 35af3d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions polars/polars-lazy/src/physical_plan/expressions/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl CastExpr {
// in a ternary or binary operation, we then do type coercion to matching supertype.
// here we create a null array for the types we cannot cast to from a booleanarray

// todo! check if the expression is really null
if input.bool().is_ok() && input.null_count() == input.len() {
match &self.data_type {
DataType::List(inner) => {
Expand Down Expand Up @@ -56,6 +57,12 @@ impl CastExpr {
.unwrap()
.into_series());
}
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => {
return Ok(
CategoricalChunked::full_null(input.name(), input.len()).into_series()
)
}
_ => {}
}
}
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,9 @@ def test_categorical_error_on_local_cmp() -> None:
),
):
df_cat.filter(pl.col("a_cat") == pl.col("b_cat"))


def test_cast_null_to_categorical() -> None:
assert pl.DataFrame().with_columns(
[pl.lit(None).cast(pl.Categorical).alias("nullable_enum")]
).dtypes == [pl.Categorical]

0 comments on commit 35af3d9

Please sign in to comment.