diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index b32f023ff131..9f0c9b39b8d5 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -7051,7 +7051,7 @@ def lazy(self) -> LazyFrame: ... } ... ) >>> df.lazy() # doctest: +ELLIPSIS - + """ return wrap_ldf(self._df.lazy()) diff --git a/py-polars/polars/lazyframe/frame.py b/py-polars/polars/lazyframe/frame.py index 0a48672c1f49..c7185abc94d6 100644 --- a/py-polars/polars/lazyframe/frame.py +++ b/py-polars/polars/lazyframe/frame.py @@ -675,7 +675,15 @@ def __str__(self) -> str: def __repr__(self) -> str: # don't expose internal/private classpath - return f"" + width = self.width + cols_str = "{} col{}".format(width, "" if width == 1 else "s") + schema_max_2 = ( + item for i, item in enumerate(self.schema.items()) if i in (0, width - 1) + ) + schema_str = (", " if width == 2 else " … ").join( + (f'"{k}": {v}' for k, v in schema_max_2) + ) + return f"<{self.__class__.__name__} [{cols_str}, {{{schema_str}}}] at 0x{id(self):X}>" def _repr_html_(self) -> str: try: @@ -1010,7 +1018,7 @@ def inspect(self, fmt: str = "{}") -> Self: ... .inspect() # print the node before the filter ... .filter(pl.col("bar") == pl.col("foo")) ... ) # doctest: +ELLIPSIS - + """ @@ -1782,7 +1790,7 @@ def lazy(self) -> Self: ... } ... ) >>> lf.lazy() # doctest: +ELLIPSIS - + """ return self @@ -1857,7 +1865,7 @@ def clone(self) -> Self: ... } ... ) >>> lf.clone() # doctest: +ELLIPSIS - + """ return self._from_pyldf(self._ldf.clone()) diff --git a/py-polars/tests/unit/test_lazy.py b/py-polars/tests/unit/test_lazy.py index 6065248671f5..d654eb9e9a99 100644 --- a/py-polars/tests/unit/test_lazy.py +++ b/py-polars/tests/unit/test_lazy.py @@ -55,6 +55,20 @@ def test_lazy() -> None: assert profiling_info[1].columns == ["node", "start", "end"] +@pytest.mark.parametrize( + ("data", "repr_"), + [ + ({}, "0 cols, {}"), + ({"a": [1]}, '1 col, {"a": Int64}'), + ({"a": [1], "b": ["B"]}, '2 cols, {"a": Int64, "b": Utf8}'), + ({"a": [1], "b": ["B"], "c": [0.0]}, '3 cols, {"a": Int64 … "c": Float64}'), + ], +) +def test_repr(data: dict[str, list[Any]], repr_: str) -> None: + ldf = pl.LazyFrame(data) + assert repr(ldf).startswith(f" None: ldf = pl.LazyFrame({"name": ["Jane", "John"], "age": [20, 30]}) assert "name" in ldf