Skip to content

Commit

Permalink
Add lazy() method to LazyFrame (#4077)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj committed Jul 19, 2022
1 parent efd1ee3 commit 2b52779
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions py-polars/polars/internals/lazy_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,18 @@ def fetch(
)
return self._dataframe_class._from_pydf(ldf.fetch(n_rows))

def lazy(self: LDF) -> LDF:
"""
Returns lazy representation, i.e. itself.
Useful for writing code that expects either a :class:`DataFrame` or :class:`LazyFrame`.
Returns
-------
LazyFrame
"""
return self

@property
def columns(self) -> list[str]:
"""
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,3 +1352,13 @@ def test_explode_inner_lists_3985() -> None:
.agg(pl.col("categories"))
.with_column(pl.col("categories").arr.eval(pl.element().explode()))
).collect().to_dict(False) == {"id": [1], "categories": [["a", "b", "a", "c"]]}


def test_lazy_method() -> None:
"""
We want to support `.lazy()` on a Lazy DataFrame as to allow more generic user code.
"""
df = pl.DataFrame({"a": [1, 1, 2, 2, 3, 3], "b": [1, 2, 3, 4, 5, 6]})
lazy_df = df.lazy()

assert lazy_df.lazy() == lazy_df

0 comments on commit 2b52779

Please sign in to comment.