Skip to content

Commit 168d410

Browse files
committed
fix: Method was inconsistent with Python and pandas when string contained leading '+'
1 parent 35f6e9f commit 168d410

File tree

2 files changed

+20
-8
lines changed
  • crates/polars-ops/src/chunked_array/strings
  • py-polars/tests/unit/operations/namespaces/string

2 files changed

+20
-8
lines changed

crates/polars-ops/src/chunked_array/strings/pad.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ fn zfill_fn<'a>(s: Option<&'a str>, len: Option<u64>, buf: &mut String) -> Optio
5959
return Some(s);
6060
}
6161
buf.clear();
62-
if let Some(stripped) = s.strip_prefix('-') {
62+
let sign_opt = s.chars().next();
63+
if let Some(sign) = sign_opt
64+
&& ['-', '+'].contains(&sign)
65+
{
6366
write!(
6467
buf,
65-
"-{:0length$}{value}",
68+
"{sign}{:0length$}{value}",
6669
0,
6770
length = length as usize,
68-
value = stripped
71+
value = &s[1..]
6972
)
7073
.unwrap();
7174
} else {

py-polars/tests/unit/operations/namespaces/string/test_pad.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ def test_str_zfill() -> None:
7272
def test_str_zfill_expr() -> None:
7373
df = pl.DataFrame(
7474
{
75-
"num": ["-10", "-1", "0", "1", "10", None, "1"],
76-
"len": [3, 4, 3, 2, 5, 3, None],
75+
"num": ["-10", "-1", "0", "1", "10", None, "1", "+1"],
76+
"len": [3, 4, 3, 2, 5, 3, None, 3],
7777
}
7878
)
7979
out = df.select(
@@ -83,9 +83,18 @@ def test_str_zfill_expr() -> None:
8383
)
8484
expected = pl.DataFrame(
8585
{
86-
"all_expr": ["-10", "-001", "000", "01", "00010", None, None],
87-
"str_lit": ["010", "0010", "010", "10", "00010", "010", None],
88-
"len_lit": ["-0010", "-0001", "00000", "00001", "00010", None, "00001"],
86+
"all_expr": ["-10", "-001", "000", "01", "00010", None, None, "+01"],
87+
"str_lit": ["010", "0010", "010", "10", "00010", "010", None, "010"],
88+
"len_lit": [
89+
"-0010",
90+
"-0001",
91+
"00000",
92+
"00001",
93+
"00010",
94+
None,
95+
"00001",
96+
"+0001",
97+
],
8998
}
9099
)
91100
assert_frame_equal(out, expected)

0 commit comments

Comments
 (0)