Skip to content

Commit

Permalink
fix(python): Truncate long column name in glimpse (#8281)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj authored Apr 16, 2023
1 parent abed1ee commit 4bd37ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3544,6 +3544,7 @@ def glimpse(self, *, return_as_string: bool = False) -> str | None:
# always print at most this number of values, mainly used to ensure
# we do not cast long arrays to strings which would be very slow
max_num_values = min(10, self.height)
max_col_name_trunc = 50

def _parse_column(col_name: str, dtype: PolarsDataType) -> tuple[str, str, str]:
dtype_str = (
Expand All @@ -3553,6 +3554,8 @@ def _parse_column(col_name: str, dtype: PolarsDataType) -> tuple[str, str, str]:
)
val = self[:max_num_values][col_name].to_list()
val_str = ", ".join(map(str, val))
if len(col_name) > max_col_name_trunc:
col_name = col_name[: (max_col_name_trunc - 3)] + "..."
return col_name, dtype_str, val_str

data = [_parse_column(s, dtype) for s, dtype in self.schema.items()]
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -3407,6 +3407,18 @@ def test_glimpse(capsys: Any) -> None:
# remove the last newline on the capsys
assert capsys.readouterr().out[:-1] == expected

colc = "a" * 96
df = pl.DataFrame({colc: [11, 22, 33]})
result = df.glimpse(return_as_string=True)
expected = textwrap.dedent(
"""\
Rows: 3
Columns: 1
$ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... <i64> 11, 22, 33
"""
)
assert result == expected


def test_item() -> None:
df = pl.DataFrame({"a": [1]})
Expand Down

0 comments on commit 4bd37ed

Please sign in to comment.