Skip to content

Commit

Permalink
fix(rust, python): adjust for null values in str.replace fast path (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Jul 28, 2023
1 parent a207241 commit f27c9b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions polars/polars-ops/src/chunked_array/strings/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,14 @@ pub(super) fn replace_lit_n_char(

// set the end of this string region
// safety: invariant of Utf8Array tells us that there is a next offset.
if let Some(next) = offsets_iter.next() {
end = *next as usize - 1;

// must loop to skip null values, as they have the same offsets
for next in offsets_iter.by_ref() {
let new_end = *next as usize - 1;
if new_end != end {
end = new_end;
break;
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/unit/namespaces/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,17 @@ def test_titlecase() -> None:
"And\tA\t Tab",
]
}


def test_string_replace_with_nulls_10124() -> None:
df = pl.DataFrame({"col1": ["S", "S", "S", None, "S", "S", "S", "S"]})

assert df.select(
pl.col("col1"),
pl.col("col1").str.replace("S", "O", n=1).alias("n_1"),
pl.col("col1").str.replace("S", "O", n=3).alias("n_3"),
).to_dict(False) == {
"col1": ["S", "S", "S", None, "S", "S", "S", "S"],
"n_1": ["O", "O", "O", None, "O", "O", "O", "O"],
"n_3": ["O", "O", "O", None, "O", "O", "O", "O"],
}

0 comments on commit f27c9b7

Please sign in to comment.