Skip to content

Commit

Permalink
fix bug with nulls at start/end
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp committed May 24, 2024
1 parent 6b14d7e commit e76170a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 5 additions & 3 deletions crates/polars-core/src/chunked_array/ops/fill_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ where
.collect_trusted();

// Compute bitmask.
let num_start_nulls = ca.first_non_null().unwrap_or(0);
let num_start_nulls = ca.first_non_null().unwrap_or(ca.len());
let mut bm = MutableBitmap::with_capacity(ca.len());
bm.extend_constant(num_start_nulls, false);
bm.extend_constant(ca.len() - num_start_nulls, true);
Expand Down Expand Up @@ -330,8 +330,10 @@ where
.collect_reversed();

// Compute bitmask.
let last_idx = ca.len().saturating_sub(1);
let num_end_nulls = last_idx - ca.last_non_null().unwrap_or(last_idx);
let num_end_nulls = ca
.last_non_null()
.map(|i| ca.len() - 1 - i)
.unwrap_or(ca.len());
let mut bm = MutableBitmap::with_capacity(ca.len());
bm.extend_constant(ca.len() - num_end_nulls, true);
bm.extend_constant(num_end_nulls, false);
Expand Down
18 changes: 16 additions & 2 deletions py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1866,10 +1866,24 @@ def test_fill_nan() -> None:
assert df.fill_nan(2.0).dtypes == [pl.Float64, pl.Datetime]


def test_forward_fill() -> None:
df = pl.DataFrame({"a": [1.0, None, 3.0]})
fill = df.select(pl.col("a").forward_fill())["a"]
assert_series_equal(fill, pl.Series("a", [1, 1, 3]).cast(pl.Float64))

df = pl.DataFrame({"a": [None, 1, None]})
fill = df.select(pl.col("a").forward_fill())["a"]
assert_series_equal(fill, pl.Series("a", [None, 1, 1]).cast(pl.Int64))


def test_backward_fill() -> None:
df = pl.DataFrame({"a": [1.0, None, 3.0]})
col_a_backward_fill = df.select([pl.col("a").backward_fill()])["a"]
assert_series_equal(col_a_backward_fill, pl.Series("a", [1, 3, 3]).cast(pl.Float64))
fill = df.select(pl.col("a").backward_fill())["a"]
assert_series_equal(fill, pl.Series("a", [1, 3, 3]).cast(pl.Float64))

df = pl.DataFrame({"a": [None, 1, None]})
fill = df.select(pl.col("a").backward_fill())["a"]
assert_series_equal(fill, pl.Series("a", [1, 1, None]).cast(pl.Int64))


def test_shrink_to_fit() -> None:
Expand Down

0 comments on commit e76170a

Please sign in to comment.