Skip to content

Commit

Permalink
fix[rust]: error on unknown supertype in fill null (#5002)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 27, 2022
1 parent 589f364 commit 893215a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions polars/polars-lazy/src/dsl/function_expr/fill_null.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
use super::*;

pub(super) fn fill_null(s: &[Series], super_type: &DataType) -> PolarsResult<Series> {
let array = s[0].cast(super_type)?;
let fill_value = s[1].cast(super_type)?;
let array = &s[0];
let fill_value = &s[1];
if matches!(super_type, DataType::Unknown) {
return Err(PolarsError::SchemaMisMatch(
format!(
"Cannot 'fill_null' a 'Series' of dtype: '{}' with an argument of dtype: '{}'",
array.dtype(),
fill_value.dtype()
)
.into(),
));
};

let array = array.cast(super_type)?;
let fill_value = fill_value.cast(super_type)?;

if !array.null_count() == 0 {
Ok(array)
Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,16 @@ def test_filter_not_of_type_bool() -> None:
pl.ComputeError, match="Filter predicate must be of type Boolean, got"
):
df.filter(pl.col("json_val").str.json_path_match("$.a"))


def test_fill_null_unknown_supertype() -> None:
df = pl.DataFrame(
[
pl.Series("a", [1, None, 3]),
pl.Series("b", ["hello", "at the", "bar"], dtype=pl.Categorical),
pl.Series("c", [1, 1, None]),
]
)

with pytest.raises(pl.SchemaError):
df.fill_null(0)

0 comments on commit 893215a

Please sign in to comment.