Skip to content

Commit 8efc161

Browse files
authored
feat: support to_list for cudf, note limitations (#1335)
* feat: support to_list for cudf, note limitations * feat: support to_list for cudf, note limitations
1 parent 7696b6e commit 8efc161

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

narwhals/_pandas_like/dataframe.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def rows(
280280
) -> list[tuple[Any, ...]] | list[dict[str, Any]]:
281281
if not named:
282282
# cuDF does not support itertuples. But it does support to_dict!
283-
if self._implementation is Implementation.CUDF: # pragma: no cover
283+
if self._implementation is Implementation.CUDF:
284284
# Extract the row values from the named rows
285285
return [tuple(row.values()) for row in self.rows(named=True)]
286286

@@ -532,7 +532,7 @@ def join(
532532
)
533533

534534
if how == "anti":
535-
if self._implementation is Implementation.CUDF: # pragma: no cover
535+
if self._implementation is Implementation.CUDF:
536536
return self._from_native_frame(
537537
self._native_frame.merge(
538538
other._native_frame,
@@ -730,7 +730,7 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any:
730730
def to_pandas(self) -> Any:
731731
if self._implementation is Implementation.PANDAS:
732732
return self._native_frame
733-
if self._implementation is Implementation.MODIN: # pragma: no cover
733+
if self._implementation is Implementation.MODIN:
734734
return self._native_frame._to_pandas()
735735
return self._native_frame.to_pandas() # pragma: no cover
736736

@@ -797,7 +797,7 @@ def gather_every(self: Self, n: int, offset: int = 0) -> Self:
797797
return self._from_native_frame(self._native_frame.iloc[offset::n])
798798

799799
def to_arrow(self: Self) -> Any:
800-
if self._implementation is Implementation.CUDF: # pragma: no cover
800+
if self._implementation is Implementation.CUDF:
801801
return self._native_frame.to_arrow(preserve_index=False)
802802

803803
import pyarrow as pa # ignore-banned-import()

narwhals/_pandas_like/series.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ def to_frame(self) -> PandasLikeDataFrame:
223223
)
224224

225225
def to_list(self) -> Any:
226+
if self._implementation is Implementation.CUDF:
227+
return self._native_series.to_arrow().to_pylist()
226228
return self._native_series.to_list()
227229

228230
def is_between(
@@ -544,9 +546,9 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any:
544546
def to_pandas(self) -> Any:
545547
if self._implementation is Implementation.PANDAS:
546548
return self._native_series
547-
elif self._implementation is Implementation.CUDF: # pragma: no cover
549+
elif self._implementation is Implementation.CUDF:
548550
return self._native_series.to_pandas()
549-
elif self._implementation is Implementation.MODIN: # pragma: no cover
551+
elif self._implementation is Implementation.MODIN:
550552
return self._native_series._to_pandas()
551553
msg = f"Unknown implementation: {self._implementation}" # pragma: no cover
552554
raise AssertionError(msg)
@@ -671,7 +673,7 @@ def clip(
671673
)
672674

673675
def to_arrow(self: Self) -> Any:
674-
if self._implementation is Implementation.CUDF: # pragma: no cover
676+
if self._implementation is Implementation.CUDF:
675677
return self._native_series.to_arrow()
676678

677679
import pyarrow as pa # ignore-banned-import()

narwhals/series.py

+6
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ def to_list(self) -> list[Any]:
468468
"""
469469
Convert to list.
470470
471+
Notes:
472+
This function converts to Python scalars. It's typically
473+
more efficient to keep your data in the format native to
474+
your original dataframe, so we recommend only calling this
475+
when you absolutely need to.
476+
471477
Examples:
472478
>>> import pandas as pd
473479
>>> import polars as pl

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ omit = [
138138
exclude_also = [
139139
"> POLARS_VERSION",
140140
"if sys.version_info() <",
141-
"if implementation is Implementation.MODIN",
142-
"if implementation is Implementation.CUDF",
141+
"if (:?self._)?implementation is Implementation.MODIN",
142+
"if (:?self._)?implementation is Implementation.CUDF",
143143
'request.applymarker\(pytest.mark.xfail\)'
144144
]
145145

0 commit comments

Comments
 (0)