Skip to content

Commit

Permalink
feat(rust, python): allow all null cast (#5933)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 29, 2022
1 parent 6db2311 commit 04b96e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion polars/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,12 @@ impl Series {

/// Cast `[Series]` to another `[DataType]`
pub fn cast(&self, dtype: &DataType) -> PolarsResult<Self> {
self.0.cast(dtype)
let len = self.len();
if self.null_count() == len {
Ok(Series::full_null(self.name(), len, dtype))
} else {
self.0.cast(dtype)
}
}

/// Compute the sum of all values in this Series.
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,10 @@ def test_lazy_rename() -> None:
assert (
df.lazy().rename({"y": "x", "x": "y"}).select(["x", "y"]).collect()
).to_dict(False) == {"x": [2], "y": [1]}


def test_all_null_cast_5826() -> None:
df = pl.DataFrame(data=[pl.Series("a", [None], dtype=pl.Utf8)])
out = df.with_column(pl.col("a").cast(pl.Boolean))
assert out.dtypes == [pl.Boolean]
assert out.item() is None

0 comments on commit 04b96e6

Please sign in to comment.