Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Incorrect Decimal value for fill_null(strategy="one") #20844

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")]
Loading