Skip to content

Commit

Permalink
fix(rust, python): don't clear rev_map when categorical series is cle… (
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored and c-peters committed Jul 14, 2023
1 parent f3abd78 commit f8d9f5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion polars/polars-core/src/series/ops/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ impl Series {
ListChunked::full_null_with_dtype(name, size, inner_dtype).into_series()
}
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => CategoricalChunked::full_null(name, size).into_series(),
DataType::Categorical(rev_map) => {
let mut ca = CategoricalChunked::full_null(name, size);
// ensure we keep the rev-map of a cleared series
if let Some(rev_map) = rev_map {
unsafe { ca.set_rev_map(rev_map.clone(), false) }
}
ca.into_series()
}
#[cfg(feature = "dtype-date")]
DataType::Date => Int32Chunked::full_null(name, size)
.into_date()
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/unit/datatypes/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,13 @@ def test_construct_with_null() -> None:

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


def test_categorical_concat_string_cached() -> None:
with pl.StringCache():
df1 = pl.DataFrame({"x": ["A"]}).with_columns(pl.col("x").cast(pl.Categorical))
df2 = pl.DataFrame({"x": ["B"]}).with_columns(pl.col("x").cast(pl.Categorical))

out = pl.concat([df1, df2])
assert out.dtypes == [pl.Categorical]
assert out["x"].to_list() == ["A", "B"]

0 comments on commit f8d9f5e

Please sign in to comment.