Skip to content

Commit

Permalink
return_type -> eager_backend
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi committed Jan 11, 2025
1 parent 3174be4 commit e2459e3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
20 changes: 11 additions & 9 deletions narwhals/_duckdb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,21 @@ def __getitem__(self, item: str) -> DuckDBInterchangeSeries:
)

@overload
def collect(self, return_type: Literal["pyarrow"] = "pyarrow") -> ArrowDataFrame: ...
def collect(
self, eager_backend: Literal["pyarrow"] = "pyarrow"
) -> ArrowDataFrame: ...

@overload
def collect(self, return_type: Literal["pandas"]) -> PandasLikeDataFrame: ...
def collect(self, eager_backend: Literal["pandas"]) -> PandasLikeDataFrame: ...

@overload
def collect(self, return_type: Literal["polars"]) -> PolarsDataFrame: ...
def collect(self, eager_backend: Literal["polars"]) -> PolarsDataFrame: ...

def collect(
self,
return_type: Literal["pyarrow", "pandas", "polars"] = "pyarrow",
eager_backend: Literal["pyarrow", "pandas", "polars"] = "pyarrow",
) -> ArrowDataFrame | PandasLikeDataFrame | PolarsDataFrame:
if return_type == "pyarrow":
if eager_backend == "pyarrow":
try:
import pyarrow as pa # ignore-banned-import
except ModuleNotFoundError as exc: # pragma: no cover
Expand All @@ -110,7 +112,7 @@ def collect(
version=self._version,
)

elif return_type == "pandas":
elif eager_backend == "pandas":
import pandas as pd # ignore-banned-import

from narwhals._pandas_like.dataframe import PandasLikeDataFrame
Expand All @@ -123,7 +125,7 @@ def collect(
version=self._version,
)

elif return_type == "polars":
elif eager_backend == "polars":
import polars as pl # ignore-banned-import

from narwhals._polars.dataframe import PolarsDataFrame
Expand All @@ -137,8 +139,8 @@ def collect(

else:
msg = (
"Only the following `return_type`'s are supported: pyarrow, pandas and "
f"polars. Found '{return_type}'."
"Only the following `eager_backend`'s are supported: pyarrow, pandas and "
f"polars. Found '{eager_backend}'."
)
raise ValueError(msg)

Expand Down
2 changes: 1 addition & 1 deletion narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3633,7 +3633,7 @@ def collect(
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>"}`.
`duckdb_kwargs={"eager_backend": "<eager_backend>"}`.
Returns:
DataFrame
Expand Down
2 changes: 1 addition & 1 deletion narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def collect(
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>"}`.
`duckdb_kwargs={"eager_backend": "<eager_backend>"}`.
Returns:
DataFrame
Expand Down
12 changes: 6 additions & 6 deletions tests/frame/collect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_collect_kwargs(constructor: Constructor) -> None:
.collect(
polars_kwargs={"no_optimization": True},
dask_kwargs={"optimize_graph": False},
duckdb_kwargs={"return_type": "pyarrow"},
duckdb_kwargs={"eager_backend": "pyarrow"},
)
)

Expand All @@ -32,23 +32,23 @@ def test_collect_kwargs(constructor: Constructor) -> None:


@pytest.mark.parametrize(
("return_type", "expected_cls"),
("eager_backend", "expected_cls"),
[
("pyarrow", pa.Table),
("polars", pl.DataFrame),
("pandas", pd.DataFrame),
],
)
def test_collect_duckdb(
return_type: Literal["pyarrow", "polars", "pandas"], expected_cls: type
eager_backend: Literal["pyarrow", "polars", "pandas"], expected_cls: type
) -> None:
duckdb = pytest.importorskip("duckdb")

data = {"a": [1, 2], "b": [3, 4]}
df_pl = pl.DataFrame(data) # noqa: F841
df = nw.from_native(duckdb.sql("select * from df_pl"))

result = df.lazy().collect(duckdb_kwargs={"return_type": return_type}).to_native()
result = df.lazy().collect(duckdb_kwargs={"eager_backend": eager_backend}).to_native()
assert isinstance(result, expected_cls)


Expand All @@ -62,8 +62,8 @@ def test_collect_duckdb_raise() -> None:
with pytest.raises(
ValueError,
match=(
"Only the following `return_type`'s are supported: pyarrow, pandas and "
"Only the following `eager_backend`'s are supported: pyarrow, pandas and "
"polars. Found 'foo'."
),
):
df.lazy().collect(duckdb_kwargs={"return_type": "foo"})
df.lazy().collect(duckdb_kwargs={"eager_backend": "foo"})

0 comments on commit e2459e3

Please sign in to comment.