Skip to content

Commit

Permalink
feat(python): Update LazyFrame.__repr__
Browse files Browse the repository at this point in the history
  • Loading branch information
datapythonista authored Jun 20, 2023
1 parent 12c4d9a commit db13403
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7051,7 +7051,7 @@ def lazy(self) -> LazyFrame:
... }
... )
>>> df.lazy() # doctest: +ELLIPSIS
<polars.LazyFrame object at ...>
<LazyFrame [3 cols, {"a": Int64 … "c": Boolean}] at ...>
"""
return wrap_ldf(self._df.lazy())
Expand Down
16 changes: 12 additions & 4 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,15 @@ def __str__(self) -> str:

def __repr__(self) -> str:
# don't expose internal/private classpath
return f"<polars.{self.__class__.__name__} object at 0x{id(self):X}>"
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:
Expand Down Expand Up @@ -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
<polars.LazyFrame object at ...>
<LazyFrame [1 col, {"bar": Int64}] at ...>
"""

Expand Down Expand Up @@ -1782,7 +1790,7 @@ def lazy(self) -> Self:
... }
... )
>>> lf.lazy() # doctest: +ELLIPSIS
<polars.LazyFrame object at ...>
<LazyFrame [3 cols, {"a": Int64 … "c": Boolean}] at ...>
"""
return self
Expand Down Expand Up @@ -1857,7 +1865,7 @@ def clone(self) -> Self:
... }
... )
>>> lf.clone() # doctest: +ELLIPSIS
<polars.LazyFrame object at ...>
<LazyFrame [3 cols, {"a": Int64 … "c": Boolean}] at ...>
"""
return self._from_pyldf(self._ldf.clone())
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/unit/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<LazyFrame [{repr_}] at ")


def test_lazyframe_membership_operator() -> None:
ldf = pl.LazyFrame({"name": ["Jane", "John"], "age": [20, 30]})
assert "name" in ldf
Expand Down

0 comments on commit db13403

Please sign in to comment.