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(rust, python): adjust for null values in str.replace fast path #10132

Merged
merged 1 commit into from
Jul 28, 2023
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
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"],
}
Loading