Skip to content

Commit

Permalink
bug[rust]: Fix float display (#4840)
Browse files Browse the repository at this point in the history
  • Loading branch information
owrior committed Sep 13, 2022
1 parent e2edab3 commit 3dcb2ad
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 82 deletions.
71 changes: 1 addition & 70 deletions polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@ fn fmt_float<T: Num + NumCast>(f: &mut Formatter<'_>, width: usize, v: T) -> fmt
let mut len = s.len() - 1;

while s.ends_with('0') {
len -= 1;
s = &s[..len];
len -= 1;
}
if s.ends_with('.') {
write!(f, "{}0", s)
Expand Down Expand Up @@ -837,73 +837,4 @@ ChunkedArray: 'name' [str]
format!("{:?}", ca)
);
}

#[test]
fn test_fmt_series() {
let s = Series::new("foo", &["Somelongstringto eeat wit me oundaf"]);
assert_eq!(
r#"shape: (1,)
Series: 'foo' [str]
[
"Somelongstring...
]"#,
format!("{:?}", s)
);

let s = Series::new("foo", &["😀😁😂😃😄😅😆😇😈😉😊😋😌😎😏😐😑😒😓"]);
assert_eq!(
r#"shape: (1,)
Series: 'foo' [str]
[
"😀😁😂😃😄😅😆😇😈😉😊😋😌😎...
]"#,
format!("{:?}", s)
);

let s = Series::new("foo", &["yzäöüäöüäöüäö"]);
assert_eq!(
r#"shape: (1,)
Series: 'foo' [str]
[
"yzäöüäöüäöüäö"
]"#,
format!("{:?}", s)
);

let s = Series::new("foo", (0..100).collect::<Vec<_>>());

dbg!(&s);
assert_eq!(
r#"shape: (100,)
Series: 'foo' [i32]
[
0
1
2
3
4
5
6
7
8
9
10
11
...
88
89
90
91
92
93
94
95
96
97
98
99
]"#,
format!("{:?}", s)
);
}
}
24 changes: 12 additions & 12 deletions py-polars/polars/internals/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,17 @@ def log10(self) -> Expr:
>>> df = pl.DataFrame({"values": [1.0, 2.0, 4.0]})
>>> df.select(pl.col("values").log10())
shape: (3, 1)
┌────────┐
│ values │
│ --- │
│ f64 │
╞════════╡
│ 0.0 │
├╌╌╌╌╌╌╌╌┤
│ 0.301
├╌╌╌╌╌╌╌╌┤
│ 0.602
└────────┘
┌────────
│ values
│ ---
│ f64
╞════════
│ 0.0
├╌╌╌╌╌╌╌╌
│ 0.30103
├╌╌╌╌╌╌╌╌
│ 0.60206
└────────
"""
return self.log(10.0)
Expand All @@ -421,7 +421,7 @@ def exp(self) -> Expr:
├╌╌╌╌╌╌╌╌╌╌┤
│ 7.389056 │
├╌╌╌╌╌╌╌╌╌╌┤
│ 54.5981
│ 54.59815
└──────────┘
"""
Expand Down
99 changes: 99 additions & 0 deletions py-polars/tests/unit/test_fmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from typing import Any, List

import pytest

import polars as pl


@pytest.mark.parametrize(
"expected, values",
[
pytest.param(
"""shape: (1,)
Series: 'foo' [str]
[
"Somelongstring...
]
""", # noqa: W191, E101
["Somelongstringto eeat wit me oundaf"],
id="Long string",
),
pytest.param(
"""shape: (1,)
Series: 'foo' [str]
[
"😀😁😂😃😄😅😆😇😈😉😊😋😌😎...
]
""", # noqa: W191, E101
["😀😁😂😃😄😅😆😇😈😉😊😋😌😎😏😐😑😒😓"],
id="Emojis",
),
pytest.param(
"""shape: (1,)
Series: 'foo' [str]
[
"yzäöüäöüäöüäö"
]
""", # noqa: W191, E101
["yzäöüäöüäöüäö"],
id="Characters with accents",
),
pytest.param(
"""shape: (100,)
Series: 'foo' [i64]
[
0
1
2
3
4
5
6
7
8
9
10
11
...
88
89
90
91
92
93
94
95
96
97
98
99
]
""", # noqa: W191, E101
[*range(100)],
id="Long series",
),
],
)
def test_fmt_series(
capfd: pytest.CaptureFixture[str], expected: str, values: List[Any]
) -> None:
s = pl.Series(name="foo", values=values)
print(s)
out, err = capfd.readouterr()
assert out == expected


def test_fmt_float(capfd: pytest.CaptureFixture[str]) -> None:
s = pl.Series(name="foo", values=[7.966e-05, 7.9e-05, 8.4666e-05, 8.00007966])
print(s)
out, err = capfd.readouterr()
expected = """shape: (4,)
Series: 'foo' [f64]
[
0.00008
0.000079
0.000085
8.00008
]
""" # noqa: W191, E101
assert out == expected

0 comments on commit 3dcb2ad

Please sign in to comment.