Skip to content

Commit

Permalink
fix(rust, python): allow fill_null in eager if type now known
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 22, 2023
1 parent 71889a4 commit 4de1c90
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
20 changes: 8 additions & 12 deletions polars/polars-lazy/polars-plan/src/dsl/function_expr/fill_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ pub(super) fn fill_null(s: &[Series], super_type: &DataType) -> PolarsResult<Ser
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, fill_value) = if matches!(super_type, DataType::Unknown) {
let fill_value = fill_value.cast(array.dtype()).map_err(|_|{
let msg = "'fill_null' supertype could not be determined. Set the correct literal value or ensure the type of the expression is known .";
PolarsError::SchemaMisMatch(msg.into())
})?;
(array.clone(), fill_value)
} else {
(array.cast(super_type)?, fill_value.cast(super_type)?)
};

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

if !array.null_count() == 0 {
Ok(array)
} else {
Expand Down
25 changes: 25 additions & 0 deletions py-polars/tests/unit/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,28 @@ def test_overflow_diff() -> None:
assert df.select(pl.col("a").cast(pl.UInt64).diff()).to_dict(False) == {
"a": [None, -10, 20]
}


def test_fill_null_unknown_output_type() -> None:
df = pl.DataFrame(
{
"a": [
None,
2,
3,
4,
5,
]
}
)
assert df.with_columns(
np.exp(pl.col("a")).fill_null(pl.lit(1, pl.Float64))
).to_dict(False) == {
"a": [
1.0,
7.38905609893065,
20.085536923187668,
54.598150033144236,
148.4131591025766,
]
}

0 comments on commit 4de1c90

Please sign in to comment.