-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC, feat: LazyFrame.collect
kwargs
#1734
Open
FBruzzesi
wants to merge
11
commits into
main
Choose a base branch
from
feat/collect-kwargs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d449521
feat: LazyFrame.collect kwargs
FBruzzesi 1046a9e
phrasing
FBruzzesi 8a4ea66
Merge branch 'main' into feat/collect-kwargs
FBruzzesi 6cd4b0b
forgot to save one file...
FBruzzesi e9e573c
tests
FBruzzesi 53671ce
Merge branch 'main' into feat/collect-kwargs
FBruzzesi aedbff2
duckdb_kwargs
FBruzzesi 23e814d
Merge branch 'main' into feat/collect-kwargs
FBruzzesi 3174be4
Merge branch 'main' into feat/collect-kwargs
FBruzzesi e2459e3
return_type -> eager_backend
FBruzzesi b65586f
merge main, do not use nw.all
FBruzzesi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3610,16 +3610,40 @@ def __getitem__(self, item: str | slice) -> NoReturn: | |
msg = "Slicing is not supported on LazyFrame" | ||
raise TypeError(msg) | ||
|
||
def collect(self) -> DataFrame[Any]: | ||
def collect( | ||
self: Self, | ||
*, | ||
polars_kwargs: dict[str, Any] | None = None, | ||
dask_kwargs: dict[str, Any] | None = None, | ||
duckdb_kwargs: dict[str, str] | None = None, | ||
Comment on lines
+3616
to
+3618
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could all be |
||
) -> DataFrame[Any]: | ||
r"""Materialize this LazyFrame into a DataFrame. | ||
|
||
As each underlying lazyframe has different arguments to set when materializing | ||
the lazyframe into a dataframe, we allow to pass them separately into its own | ||
keyword argument. | ||
|
||
Arguments: | ||
polars_kwargs: [polars.LazyFrame.collect](https://docs.pola.rs/api/python/dev/reference/lazyframe/api/polars.LazyFrame.collect.html) | ||
arguments. Used only if the `LazyFrame` is backed by a `polars.LazyFrame`. | ||
If not provided, it uses the polars default values. | ||
dask_kwargs: [dask.dataframe.DataFrame.compute](https://docs.dask.org/en/stable/generated/dask.dataframe.DataFrame.compute.html) | ||
arguments. Used only if the `LazyFrame` is backed by a `dask.dataframe.DataFrame`. | ||
If not provided, it uses the dask default values. | ||
duckdb_kwargs: Allows to specify in which eager backend to materialize a | ||
DuckDBPyRelation backed LazyFrame. It is possible to choose among | ||
`pyarrow`, `pandas` or `polars` by declaring | ||
`duckdb_kwargs={"return_type": "<eager_backend>"}`. | ||
|
||
Returns: | ||
DataFrame | ||
|
||
Examples: | ||
>>> import narwhals as nw | ||
>>> import polars as pl | ||
>>> import dask.dataframe as dd | ||
>>> import narwhals as nw | ||
>>> from narwhals.typing import IntoDataFrame, IntoFrame | ||
>>> | ||
>>> data = { | ||
... "a": ["a", "b", "a", "b", "b", "c"], | ||
... "b": [1, 2, 3, 4, 5, 6], | ||
|
@@ -3628,28 +3652,14 @@ def collect(self) -> DataFrame[Any]: | |
>>> lf_pl = pl.LazyFrame(data) | ||
>>> lf_dask = dd.from_dict(data, npartitions=2) | ||
|
||
>>> lf = nw.from_native(lf_pl) | ||
>>> lf # doctest:+ELLIPSIS | ||
>>> nw.from_native(lf_pl) # doctest:+ELLIPSIS | ||
βββββββββββββββββββββββββββββββ | ||
| Narwhals LazyFrame | | ||
|-----------------------------| | ||
|<LazyFrame at ... | ||
βββββββββββββββββββββββββββββββ | ||
>>> df = lf.group_by("a").agg(nw.all().sum()).collect() | ||
>>> df.to_native().sort("a") | ||
shape: (3, 3) | ||
βββββββ¬ββββββ¬ββββββ | ||
β a β b β c β | ||
β --- β --- β --- β | ||
β str β i64 β i64 β | ||
βββββββͺββββββͺββββββ‘ | ||
β a β 4 β 10 β | ||
β b β 11 β 10 β | ||
β c β 6 β 1 β | ||
βββββββ΄ββββββ΄ββββββ | ||
|
||
>>> lf = nw.from_native(lf_dask) | ||
>>> lf | ||
>>> nw.from_native(lf_dask) | ||
βββββββββββββββββββββββββββββββββββββ | ||
| Narwhals LazyFrame | | ||
|-----------------------------------| | ||
|
@@ -3662,15 +3672,90 @@ def collect(self) -> DataFrame[Any]: | |
|Dask Name: frompandas, 1 expression| | ||
|Expr=df | | ||
βββββββββββββββββββββββββββββββββββββ | ||
>>> df = lf.group_by("a").agg(nw.col("b", "c").sum()).collect() | ||
>>> df.to_native() | ||
|
||
Let's define a dataframe-agnostic that does some grouping computation and | ||
finally collects to a DataFrame: | ||
|
||
>>> def agnostic_group_by_and_collect(lf_native: IntoFrame) -> IntoDataFrame: | ||
... lf = nw.from_native(lf_native) | ||
... return ( | ||
... lf.group_by("a") | ||
... .agg(nw.col("b", "c").sum()) | ||
... .sort("a") | ||
... .collect() | ||
... .to_native() | ||
... ) | ||
|
||
We can then pass any supported library such as Polars or Dask | ||
to `agnostic_group_by_and_collect`: | ||
|
||
>>> agnostic_group_by_and_collect(lf_pl) | ||
shape: (3, 3) | ||
βββββββ¬ββββββ¬ββββββ | ||
β a β b β c β | ||
β --- β --- β --- β | ||
β str β i64 β i64 β | ||
βββββββͺββββββͺββββββ‘ | ||
β a β 4 β 10 β | ||
β b β 11 β 10 β | ||
β c β 6 β 1 β | ||
βββββββ΄ββββββ΄ββββββ | ||
|
||
>>> agnostic_group_by_and_collect(lf_dask) | ||
a b c | ||
0 a 4 10 | ||
1 b 11 10 | ||
2 c 6 1 | ||
|
||
Now, let's suppose that we want to run lazily, yet without | ||
query optimization (e.g. for debugging purpose). As this is achieved | ||
differently in polars and dask, to keep a unified workflow we can specify | ||
the native kwargs for each backend: | ||
|
||
>>> def agnostic_collect_no_opt(lf_native: IntoFrame) -> IntoDataFrame: | ||
... lf = nw.from_native(lf_native) | ||
... return ( | ||
... lf.group_by("a") | ||
... .agg(nw.col("b", "c").sum()) | ||
... .sort("a") | ||
... .collect( | ||
... polars_kwargs={"no_optimization": True}, | ||
... dask_kwargs={"optimize_graph": False}, | ||
... ) | ||
... .to_native() | ||
... ) | ||
|
||
>>> agnostic_collect_no_opt(lf_pl) | ||
shape: (3, 3) | ||
βββββββ¬ββββββ¬ββββββ | ||
β a β b β c β | ||
β --- β --- β --- β | ||
β str β i64 β i64 β | ||
βββββββͺββββββͺββββββ‘ | ||
β a β 4 β 10 β | ||
β b β 11 β 10 β | ||
β c β 6 β 1 β | ||
βββββββ΄ββββββ΄ββββββ | ||
|
||
>>> agnostic_collect_no_opt(lf_dask) | ||
a b c | ||
0 a 4 10 | ||
1 b 11 10 | ||
2 c 6 1 | ||
""" | ||
from narwhals.utils import Implementation | ||
|
||
if self.implementation is Implementation.POLARS and polars_kwargs is not None: | ||
kwargs = polars_kwargs | ||
elif self.implementation is Implementation.DASK and dask_kwargs is not None: | ||
kwargs = dask_kwargs | ||
elif self.implementation is Implementation.DUCKDB and duckdb_kwargs is not None: | ||
kwargs = duckdb_kwargs | ||
else: | ||
kwargs = {} | ||
|
||
return self._dataframe( | ||
self._compliant_frame.collect(), | ||
self._compliant_frame.collect(**kwargs), | ||
level="full", | ||
) | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated but.. should we change in
PolarsDataFrame
: