Skip to content

Commit

Permalink
fix: Fix number of rows printed in DataFrame/Series repr (edge case…
Browse files Browse the repository at this point in the history
…s) (#14548)
  • Loading branch information
stinodego committed Feb 26, 2024
1 parent c00a2bf commit 6b7eb52
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 46 deletions.
62 changes: 29 additions & 33 deletions crates/polars-core/src/fmt.rs
Expand Up @@ -130,19 +130,18 @@ macro_rules! format_array {
};
Ok(())
};
if (limit == 0 && $a.len() > 0) || ($a.len() > limit + 1) {
if limit > 0 {
for i in 0..std::cmp::max((limit / 2), 1) {
let v = $a.get_any_value(i).unwrap();
write_fn(v, $f)?;
}
if $a.len() > limit {
let half = limit / 2;
let rest = limit % 2;

for i in 0..(half + rest) {
let v = $a.get_any_value(i).unwrap();
write_fn(v, $f)?;
}
write!($f, "\t\n")?;
if limit > 1 {
for i in ($a.len() - (limit + 1) / 2)..$a.len() {
let v = $a.get_any_value(i).unwrap();
write_fn(v, $f)?;
}
for i in ($a.len() - half)..$a.len() {
let v = $a.get_any_value(i).unwrap();
write_fn(v, $f)?;
}
} else {
for i in 0..$a.len() {
Expand Down Expand Up @@ -524,8 +523,8 @@ impl Display for DataFrame {
.as_deref()
.unwrap_or("")
.parse()
// Note: see "https://github.com/pola-rs/polars/pull/13699" for
// the rationale behind choosing 10 as the default value ;)
// Note: see https://github.com/pola-rs/polars/pull/13699 for
// the rationale behind choosing 10 as the default value
.map_or(10, |n: i64| if n < 0 { height } else { n as usize });

let (n_first, n_last) = if self.width() > max_n_cols {
Expand Down Expand Up @@ -588,11 +587,15 @@ impl Display for DataFrame {
let mut max_elem_lengths: Vec<usize> = vec![0; n_tbl_cols];

if max_n_rows > 0 {
if height > max_n_rows + 1 {
// Truncate the table if we have more rows than the configured maximum
// number of rows plus the single row which would contain "…".
if height > max_n_rows {
// Truncate the table if we have more rows than the
// configured maximum number of rows
let mut rows = Vec::with_capacity(std::cmp::max(max_n_rows, 2));
for i in 0..std::cmp::max(max_n_rows / 2, 1) {

let half = max_n_rows / 2;
let rest = max_n_rows % 2;

for i in 0..(half + rest) {
let row = self
.columns
.iter()
Expand All @@ -606,23 +609,16 @@ impl Display for DataFrame {
}
let dots = rows[0].iter().map(|_| "…".to_string()).collect();
rows.push(dots);
if max_n_rows > 1 {
for i in (height - (max_n_rows + 1) / 2)..height {
let row = self
.columns
.iter()
.map(|s| s.str_value(i).unwrap())
.collect();
for i in (height - half)..height {
let row = self
.columns
.iter()
.map(|s| s.str_value(i).unwrap())
.collect();

let row_strings = prepare_row(
row,
n_first,
n_last,
str_truncate,
&mut max_elem_lengths,
);
rows.push(row_strings);
}
let row_strings =
prepare_row(row, n_first, n_last, str_truncate, &mut max_elem_lengths);
rows.push(row_strings);
}
table.add_rows(rows);
} else {
Expand Down
9 changes: 5 additions & 4 deletions py-polars/polars/dataframe/frame.py
Expand Up @@ -4455,10 +4455,11 @@ def describe(
Customize which percentiles are displayed, applying linear interpolation:
>>> df.describe(
... percentiles=[0.1, 0.3, 0.5, 0.7, 0.9],
... interpolation="linear",
... )
>>> with pl.Config(tbl_rows=12):
... df.describe(
... percentiles=[0.1, 0.3, 0.5, 0.7, 0.9],
... interpolation="linear",
... )
shape: (11, 7)
┌────────────┬──────────┬──────────┬──────────┬──────┬────────────┬──────────┐
│ statistic ┆ float ┆ int ┆ bool ┆ str ┆ date ┆ time │
Expand Down
9 changes: 5 additions & 4 deletions py-polars/polars/lazyframe/frame.py
Expand Up @@ -1001,10 +1001,11 @@ def describe(
Customize which percentiles are displayed, applying linear interpolation:
>>> lf.describe(
... percentiles=[0.1, 0.3, 0.5, 0.7, 0.9],
... interpolation="linear",
... )
>>> with pl.Config(tbl_rows=12):
... lf.describe(
... percentiles=[0.1, 0.3, 0.5, 0.7, 0.9],
... interpolation="linear",
... )
shape: (11, 7)
┌────────────┬──────────┬──────────┬──────────┬──────┬────────────┬──────────┐
│ statistic ┆ float ┆ int ┆ bool ┆ str ┆ date ┆ time │
Expand Down
8 changes: 4 additions & 4 deletions py-polars/tests/unit/test_config.py
Expand Up @@ -172,7 +172,7 @@ def test_set_tbl_rows() -> None:
"╞═════╪═════╪═════╡\n"
"│ 1 ┆ 5 ┆ 9 │\n"
"│ 2 ┆ 6 ┆ 10 │\n"
"│ 3711\n"
"│ \n"
"│ 4 ┆ 8 ┆ 12 │\n"
"└─────┴─────┴─────┘"
)
Expand All @@ -181,8 +181,8 @@ def test_set_tbl_rows() -> None:
"Series: 'ser' [i64]\n"
"[\n"
"\t1\n"
"\t2\n"
"\t\n"
"\t4\n"
"\t5\n"
"]"
)
Expand All @@ -207,7 +207,7 @@ def test_set_tbl_rows() -> None:
"[\n"
"\t1\n"
"\t2\n"
"\t3\n"
"\t\n"
"\t4\n"
"\t5\n"
"]"
Expand All @@ -230,8 +230,8 @@ def test_set_tbl_rows() -> None:
"│ i64 ┆ i64 ┆ i64 │\n"
"╞═════╪═════╪═════╡\n"
"│ 1 ┆ 6 ┆ 11 │\n"
"│ 2 ┆ 7 ┆ 12 │\n"
"│ … ┆ … ┆ … │\n"
"│ 4 ┆ 9 ┆ 14 │\n"
"│ 5 ┆ 10 ┆ 15 │\n"
"└─────┴─────┴─────┘"
)
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_format.py
Expand Up @@ -68,8 +68,8 @@ def _environ() -> Iterator[None]:
9
10
11
12
87
88
89
90
Expand Down

0 comments on commit 6b7eb52

Please sign in to comment.