Skip to content

Commit

Permalink
chore(python): Remove trailing spaces in glimpse output (#6037)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghuls committed Jan 4, 2023
1 parent fde4e9c commit 9f01023
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
11 changes: 6 additions & 5 deletions py-polars/polars/internals/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2589,7 +2589,7 @@ def glimpse(self: DF) -> str:
... "f": [date(2020, 1, 1), date(2021, 1, 2), date(2022, 1, 1)],
... }
... )
>>> df.glimpse() # doctest: +IGNORE_RESULT
>>> print(df.glimpse())
Rows: 3
Columns: 6
$ a <Float64> 1.0, 2.8, 3.0
Expand Down Expand Up @@ -2623,18 +2623,19 @@ def _parse_column(col_name: str) -> tuple[str, str, str]:
# limit the amount of data printed such that total width is fixed
max_col_values = 100 - max_col_name - max_col_dtype

output = StringIO()
# print header
output = f"Rows: {self.height}\nColumns: {self.width}\n"
output.write(f"Rows: {self.height}\nColumns: {self.width}\n")

# print individual columns: one row per column
for col_name, dtype_str, val_str in data:
output += (
output.write(
f"$ {col_name:<{max_col_name}}"
f" {dtype_str:>{max_col_dtype}}"
f" {val_str:<{max_col_values}}\n"
f" {val_str:<{min(len(val_str), max_col_values)}}\n"
)

return output
return output.getvalue()

def describe(self: DF) -> DF:
"""
Expand Down
27 changes: 14 additions & 13 deletions py-polars/tests/unit/test_df.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import sys
import textwrap
import typing
from datetime import date, datetime, timedelta
from decimal import Decimal
Expand Down Expand Up @@ -2497,19 +2498,19 @@ def test_glimpse() -> None:
)
result = df.glimpse()

# Strip trailing whitespace for the purposes of this test
result_lines = [line.strip() for line in result.strip().split("\n")]
expected_lines = [
"Rows: 3",
"Columns: 6",
"$ a <Float64> 1.0, 2.8, 3.0",
"$ b <Int64> 4, 5, None",
"$ c <Boolean> True, False, True",
"$ d <Utf8> None, b, c",
"$ e <Utf8> usd, eur, None",
"$ f <Date> 2020-01-01, 2021-01-02, 2022-01-01",
]
assert result_lines == expected_lines
expected = textwrap.dedent(
"""\
Rows: 3
Columns: 6
$ a <Float64> 1.0, 2.8, 3.0
$ b <Int64> 4, 5, None
$ c <Boolean> True, False, True
$ d <Utf8> None, b, c
$ e <Utf8> usd, eur, None
$ f <Date> 2020-01-01, 2021-01-02, 2022-01-01
"""
)
assert result == expected


def test_item() -> None:
Expand Down

0 comments on commit 9f01023

Please sign in to comment.