Skip to content

Commit

Permalink
fix fill_value with python datetimes and expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 13, 2021
1 parent a40061b commit e6260f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
14 changes: 12 additions & 2 deletions py-polars/polars/lazy/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,17 @@ def fill_null(self, fill_value: Union[str, int, float, "Expr"]) -> "Expr":
* "one"
* "zero"
"""
if fill_value in ["backward", "forward", "min", "max", "mean", "zero", "one"]:
# we first must check if it is not an expr, as expr does not implement __bool__
# and thus leads to a value error in the second comparisson.
if not isinstance(fill_value, pl.Expr) and fill_value in [
"backward",
"forward",
"min",
"max",
"mean",
"zero",
"one",
]:
return wrap_expr(self._pyexpr.fill_null_with_strategy(fill_value))

fill_value = expr_to_lit_or_expr(fill_value, str_to_lit=True)
Expand Down Expand Up @@ -2335,7 +2345,7 @@ def expr_to_lit_or_expr(
"""
if isinstance(expr, str) and not str_to_lit:
return col(expr)
elif isinstance(expr, (int, float, str, pl.Series)) or expr is None:
elif isinstance(expr, (int, float, str, pl.Series, datetime)) or expr is None:
return lit(expr)
elif isinstance(expr, list):
return [expr_to_lit_or_expr(e, str_to_lit=str_to_lit) for e in expr] # type: ignore[return-value]
Expand Down
15 changes: 15 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from datetime import datetime

import polars as pl


def test_fill_null():
dt = datetime.strptime("2021-01-01", "%Y-%m-%d")
s = pl.Series("A", [dt, None])

for fill_val in (dt, pl.lit(dt)):
out = s.fill_null(fill_val)

assert out.null_count() == 0
assert out.dt[0] == dt
assert out.dt[1] == dt

0 comments on commit e6260f2

Please sign in to comment.