Skip to content
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
21 changes: 5 additions & 16 deletions crates/polars-ops/src/chunked_array/strings/pad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,13 @@ fn zfill_fn<'a>(s: Option<&'a str>, len: Option<u64>, buf: &mut String) -> Optio
return Some(s);
}
buf.clear();
let length = length as usize;
if let Some(stripped) = s.strip_prefix('-') {
write!(
buf,
"-{:0length$}{value}",
0,
length = length as usize,
value = stripped
)
.unwrap();
write!(buf, "-{:0length$}{stripped}", 0,).unwrap();
} else if let Some(stripped) = s.strip_prefix('+') {
write!(buf, "+{:0length$}{stripped}", 0,).unwrap();
} else {
write!(
buf,
"{:0length$}{value}",
0,
length = length as usize,
value = s
)
.unwrap();
write!(buf, "{:0length$}{s}", 0,).unwrap();
};
// extend lifetime
// lifetime is bound to 'a
Expand Down
19 changes: 14 additions & 5 deletions py-polars/tests/unit/operations/namespaces/string/test_pad.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def test_str_zfill() -> None:
def test_str_zfill_expr() -> None:
df = pl.DataFrame(
{
"num": ["-10", "-1", "0", "1", "10", None, "1"],
"len": [3, 4, 3, 2, 5, 3, None],
"num": ["-10", "-1", "0", "1", "10", None, "1", "+1"],
"len": [3, 4, 3, 2, 5, 3, None, 3],
}
)
out = df.select(
Expand All @@ -83,9 +83,18 @@ def test_str_zfill_expr() -> None:
)
expected = pl.DataFrame(
{
"all_expr": ["-10", "-001", "000", "01", "00010", None, None],
"str_lit": ["010", "0010", "010", "10", "00010", "010", None],
"len_lit": ["-0010", "-0001", "00000", "00001", "00010", None, "00001"],
"all_expr": ["-10", "-001", "000", "01", "00010", None, None, "+01"],
"str_lit": ["010", "0010", "010", "10", "00010", "010", None, "010"],
"len_lit": [
"-0010",
"-0001",
"00000",
"00001",
"00010",
None,
"00001",
"+0001",
],
}
)
assert_frame_equal(out, expected)
Expand Down
Loading