Skip to content

Commit

Permalink
feat(rust): right-align numeric columns
Browse files Browse the repository at this point in the history
  • Loading branch information
alicja-januszkiewicz committed Mar 10, 2023
1 parent db1c15a commit 6c1f6fb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 73 deletions.
23 changes: 12 additions & 11 deletions polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,18 +553,19 @@ impl Display for DataFrame {
table.set_width(100);
}

// set alignment of cells, if defined
if std::env::var(FMT_TABLE_CELL_ALIGNMENT).is_ok() {
// for (column_index, column) in table.column_iter_mut().enumerate() {
let str_preset = std::env::var(FMT_TABLE_CELL_ALIGNMENT)
.unwrap_or_else(|_| "DEFAULT".to_string());
for column in table.column_iter_mut() {
if str_preset == "RIGHT" {
let str_preset =
std::env::var(FMT_TABLE_CELL_ALIGNMENT).unwrap_or_else(|_| "DEFAULT".to_string());
for (column_index, column) in table.column_iter_mut().enumerate() {
if str_preset == "RIGHT" {
column.set_cell_alignment(CellAlignment::Right);
} else if str_preset == "LEFT" {
column.set_cell_alignment(CellAlignment::Left);
} else if str_preset == "CENTER" {
column.set_cell_alignment(CellAlignment::Center);
} else {
let dtype = fields[column_index].data_type();
if dtype.to_physical().is_numeric() {
column.set_cell_alignment(CellAlignment::Right);
} else if str_preset == "LEFT" {
column.set_cell_alignment(CellAlignment::Left);
} else if str_preset == "CENTER" {
column.set_cell_alignment(CellAlignment::Center);
} else {
column.set_cell_alignment(CellAlignment::Left);
}
Expand Down
124 changes: 62 additions & 62 deletions py-polars/tests/unit/test_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ def test_ascii_tables() -> None:
assert (
str(df) == "shape: (3, 3)\n"
"+-----+-----+-----+\n"
"| a | b | c |\n"
"| a | b | c |\n"
"| --- | --- | --- |\n"
"| i64 | i64 | i64 |\n"
"+=================+\n"
"| 1 | 4 | 7 |\n"
"| 2 | 5 | 8 |\n"
"| 3 | 6 | 9 |\n"
"| 1 | 4 | 7 |\n"
"| 2 | 5 | 8 |\n"
"| 3 | 6 | 9 |\n"
"+-----+-----+-----+"
)

# confirm back to utf8 default after scope-exit
assert (
str(df) == "shape: (3, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"│ --- ┆ --- ┆ --- │\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 4 7 \n"
"│ 2 5 8 \n"
"│ 3 6 9 \n"
"│ 1 4 7\n"
"│ 2 5 8\n"
"│ 3 6 9\n"
"└─────┴─────┴─────┘"
)

Expand All @@ -57,11 +57,11 @@ def test_hide_header_elements() -> None:
assert (
str(df) == "shape: (3, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"╞═════╪═════╪═════╡\n"
"│ 1 4 7 \n"
"│ 2 5 8 \n"
"│ 3 6 9 \n"
"│ 1 4 7\n"
"│ 2 5 8\n"
"│ 3 6 9\n"
"└─────┴─────┴─────┘"
)

Expand All @@ -71,9 +71,9 @@ def test_hide_header_elements() -> None:
"┌─────┬─────┬─────┐\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 4 7 \n"
"│ 2 5 8 \n"
"│ 3 6 9 \n"
"│ 1 4 7\n"
"│ 2 5 8\n"
"│ 3 6 9\n"
"└─────┴─────┴─────┘"
)

Expand All @@ -82,21 +82,21 @@ def test_set_tbl_cols() -> None:
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})

pl.Config.set_tbl_cols(1)
assert str(df).split("\n")[2] == "│ a ┆ ... │"
assert str(df).split("\n")[2] == "│ a ┆ ... │"
pl.Config.set_tbl_cols(2)
assert str(df).split("\n")[2] == "│ a ┆ ... ┆ c │"
assert str(df).split("\n")[2] == "│ a ┆ ... ┆ c │"
pl.Config.set_tbl_cols(3)
assert str(df).split("\n")[2] == "│ a b c │"
assert str(df).split("\n")[2] == "│ a b c │"

df = pl.DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [10, 11, 12]}
)
pl.Config.set_tbl_cols(2)
assert str(df).split("\n")[2] == "│ a ┆ ... ┆ d │"
assert str(df).split("\n")[2] == "│ a ┆ ... ┆ d │"
pl.Config.set_tbl_cols(3)
assert str(df).split("\n")[2] == "│ a b ┆ ... ┆ d │"
assert str(df).split("\n")[2] == "│ a b ┆ ... ┆ d │"
pl.Config.set_tbl_cols(-1)
assert str(df).split("\n")[2] == "│ a b c d │"
assert str(df).split("\n")[2] == "│ a b c d │"


def test_set_tbl_rows() -> None:
Expand All @@ -107,7 +107,7 @@ def test_set_tbl_rows() -> None:
assert (
str(df) == "shape: (4, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"│ --- ┆ --- ┆ --- │\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
Expand All @@ -120,11 +120,11 @@ def test_set_tbl_rows() -> None:
assert (
str(df) == "shape: (4, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"│ --- ┆ --- ┆ --- │\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 5 9 \n"
"│ 1 5 9\n"
"│ ... ┆ ... ┆ ... │\n"
"└─────┴─────┴─────┘"
)
Expand All @@ -136,13 +136,13 @@ def test_set_tbl_rows() -> None:
assert (
str(df) == "shape: (4, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"│ --- ┆ --- ┆ --- │\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 5 9 \n"
"│ 1 5 9\n"
"│ ... ┆ ... ┆ ... │\n"
"│ 4 8 12 \n"
"│ 4 8 12\n"
"└─────┴─────┴─────┘"
)
assert (
Expand All @@ -159,14 +159,14 @@ def test_set_tbl_rows() -> None:
assert (
str(df) == "shape: (4, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"│ --- ┆ --- ┆ --- │\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 5 9 \n"
"│ 1 5 9\n"
"│ ... ┆ ... ┆ ... │\n"
"│ 3 7 11 \n"
"│ 4 8 12 \n"
"│ 3 7 11\n"
"│ 4 8 12\n"
"└─────┴─────┴─────┘"
)
assert (
Expand All @@ -184,14 +184,14 @@ def test_set_tbl_rows() -> None:
assert (
str(df) == "shape: (4, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"│ --- ┆ --- ┆ --- │\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 5 9 \n"
"│ 2 6 10 \n"
"│ 3 7 11 \n"
"│ 4 8 12 \n"
"│ 1 5 9\n"
"│ 2 6 10\n"
"│ 3 7 11\n"
"│ 4 8 12\n"
"└─────┴─────┴─────┘"
)
assert (
Expand All @@ -218,14 +218,14 @@ def test_set_tbl_rows() -> None:
assert (
str(df) == "shape: (5, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"│ --- ┆ --- ┆ --- │\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 6 11 \n"
"│ 1 6 11\n"
"│ ... ┆ ... ┆ ... │\n"
"│ 4 9 14 \n"
"│ 5 ┆ 10 ┆ 15 \n"
"│ 4 9 14\n"
"│ 5 10 ┆ 15\n"
"└─────┴─────┴─────┘"
)

Expand All @@ -246,14 +246,14 @@ def test_set_tbl_rows() -> None:
assert (
str(df) == "shape: (5, 3)\n"
"┌─────┬─────┬─────┐\n"
"│ a b c \n"
"│ a b c\n"
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 6 11 \n"
"│ 2 7 12 \n"
"│ 3 8 13 \n"
"│ 4 9 14 \n"
"│ 5 ┆ 10 ┆ 15 \n"
"│ 1 6 11\n"
"│ 2 7 12\n"
"│ 3 8 13\n"
"│ 4 9 14\n"
"│ 5 10 ┆ 15\n"
"└─────┴─────┴─────┘"
)

Expand All @@ -273,9 +273,9 @@ def test_set_tbl_formats() -> None:
"| --- | --- | --- |\n"
"| i64 | f64 | str |\n"
"|-----|-----|-----|\n"
"| 1 | 6.0 | a |\n"
"| 2 | 7.0 | b |\n"
"| 3 | 8.0 | c |"
"| 1 | 6.0 | a |\n"
"| 2 | 7.0 | b |\n"
"| 3 | 8.0 | c |"
)

pl.Config().set_tbl_formatting("ASCII_BORDERS_ONLY_CONDENSED")
Expand All @@ -287,9 +287,9 @@ def test_set_tbl_formats() -> None:
"| foo bar ham |\n"
"| i64 f64 str |\n"
"+=================+\n"
"| 1 6.0 a |\n"
"| 2 7.0 b |\n"
"| 3 8.0 c |\n"
"| 1 6.0 a |\n"
"| 2 7.0 b |\n"
"| 3 8.0 c |\n"
"+-----------------+"
)

Expand All @@ -299,9 +299,9 @@ def test_set_tbl_formats() -> None:
assert str(df) == (
"shape: (3, 3)\n"
" foo bar ham \n"
" 1 6.0 a \n"
" 2 7.0 b \n"
" 3 8.0 c "
" 1 6.0 a \n"
" 2 7.0 b \n"
" 3 8.0 c "
)

# after scope, expect previous style
Expand All @@ -312,9 +312,9 @@ def test_set_tbl_formats() -> None:
"| --- --- --- |\n"
"| i64 f64 str |\n"
"+=================+\n"
"| 1 6.0 a |\n"
"| 2 7.0 b |\n"
"| 3 8.0 c |\n"
"| 1 6.0 a |\n"
"| 2 7.0 b |\n"
"| 3 8.0 c |\n"
"+-----------------+"
)

Expand Down Expand Up @@ -350,9 +350,9 @@ def test_shape_below_table_and_inlined_dtype() -> None:
"╭─────────┬─────────┬─────────╮\n"
"│ a (i64) ┆ b (i64) ┆ c (i64) │\n"
"╞═════════╪═════════╪═════════╡\n"
"│ 135\n"
"│ 135\n"
"├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤\n"
"│ 246\n"
"│ 246\n"
"╰─────────┴─────────┴─────────╯\n"
"shape: (2, 3)"
)
Expand All @@ -363,9 +363,9 @@ def test_shape_below_table_and_inlined_dtype() -> None:
"╭─────────┬─────────┬─────────╮\n"
"│ a (i64) ┆ b (i64) ┆ c (i64) │\n"
"╞═════════╪═════════╪═════════╡\n"
"│ 135\n"
"│ 135\n"
"├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤\n"
"│ 246\n"
"│ 246\n"
"╰─────────┴─────────┴─────────╯"
)
(
Expand Down

0 comments on commit 6c1f6fb

Please sign in to comment.