Skip to content

Commit

Permalink
fix: Incorrect Decimal value for fill_null(strategy="one") (pola-…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemanley authored Jan 22, 2025
1 parent f79ea83 commit 24fccc4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/polars-core/src/chunked_array/ops/fill_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ impl Series {
fill_backward_gather(self)
},
FillNullStrategy::Backward(Some(limit)) => fill_backward_gather_limit(self, limit),
#[cfg(feature = "dtype-decimal")]
FillNullStrategy::One if self.dtype().is_decimal() => {
let ca = self.decimal().unwrap();
let precision = ca.precision();
let scale = ca.scale();
let fill_value = 10i128.pow(scale as u32);
let phys = ca.as_ref().fill_null_with_values(fill_value)?;
Ok(phys.into_decimal_unchecked(precision, scale).into_series())
},
_ => {
let logical_type = self.dtype();
let s = self.to_physical_repr();
Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/datatypes/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,16 @@ def test_shift_over_12957() -> None:
)
assert result["x"].to_list() == [None, D("1.1"), None, D("2.2")]
assert result["y"].to_list() == [None, 1, None, 2]


def test_fill_null() -> None:
s = pl.Series("a", [D("1.2"), None, D("1.4")])

assert s.fill_null(D("0.0")).to_list() == [D("1.2"), D("0.0"), D("1.4")]
assert s.fill_null(strategy="zero").to_list() == [D("1.2"), D("0.0"), D("1.4")]
assert s.fill_null(strategy="max").to_list() == [D("1.2"), D("1.4"), D("1.4")]
assert s.fill_null(strategy="min").to_list() == [D("1.2"), D("1.2"), D("1.4")]
assert s.fill_null(strategy="one").to_list() == [D("1.2"), D("1.0"), D("1.4")]
assert s.fill_null(strategy="forward").to_list() == [D("1.2"), D("1.2"), D("1.4")]
assert s.fill_null(strategy="backward").to_list() == [D("1.2"), D("1.4"), D("1.4")]
assert s.fill_null(strategy="mean").to_list() == [D("1.2"), D("1.3"), D("1.4")]

0 comments on commit 24fccc4

Please sign in to comment.