Skip to content

Commit

Permalink
fix(rust): Ensure ASCII ellipsis fits in column width (#21275)
Browse files Browse the repository at this point in the history
  • Loading branch information
ydagosto authored Feb 20, 2025
1 parent 42104e2 commit 2bbe84d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion crates/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,12 @@ impl Display for DataFrame {
str_truncate + ellipsis_len + padding,
std::cmp::max(name_lengths[idx], *elem_len),
);
if mx <= min_col_width {
if (mx <= min_col_width) && !(max_n_rows > 0 && height > max_n_rows) {
// col width is less than min width + table is not truncated
constraints.push(col_width_exact(mx));
} else if mx <= min_col_width {
// col width is less than min width + table is truncated (w/ ellipsis)
constraints.push(col_width_bounds(mx, min_col_width));
} else {
constraints.push(col_width_bounds(min_col_width, mx));
}
Expand Down
34 changes: 34 additions & 0 deletions py-polars/tests/unit/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,37 @@ def test_fmt_decimal_max_scale() -> None:
def test_simple_project_format(lf: pl.LazyFrame, expected: str) -> None:
result = lf.explain()
assert expected in result


@pytest.mark.parametrize(
("df", "expected"),
[
pytest.param(
pl.DataFrame({"A": range(4)}),
"""shape: (4, 1)
+-----+
| A |
+=====+
| 0 |
| 1 |
| ... |
| 3 |
+-----+""",
id="Ellipsis correctly aligned",
),
pytest.param(
pl.DataFrame({"A": range(2)}),
"""shape: (2, 1)
+---+
| A |
+===+
| 0 |
| 1 |
+---+""",
id="No ellipsis needed",
),
],
)
def test_format_ascii_table_truncation(df: pl.DataFrame, expected: str) -> None:
with pl.Config(tbl_rows=3, tbl_hide_column_data_types=True, ascii_tables=True):
assert str(df) == expected

0 comments on commit 2bbe84d

Please sign in to comment.