diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index 53f46756140d..e6cd4f0881d3 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -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 = ( @@ -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()] diff --git a/py-polars/tests/unit/test_df.py b/py-polars/tests/unit/test_df.py index ea79cc227123..06a9f9d77194 100644 --- a/py-polars/tests/unit/test_df.py +++ b/py-polars/tests/unit/test_df.py @@ -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... 11, 22, 33 + """ + ) + assert result == expected + def test_item() -> None: df = pl.DataFrame({"a": [1]})