Skip to content
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

test: Add cudf_constructor to conftest #797

Merged
merged 8 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions narwhals/_pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,9 @@ def is_in(self, other: Any) -> PandasLikeSeries:
return self._from_native_series(res)

def arg_true(self) -> PandasLikeSeries:
import numpy as np # ignore-banned-import

ser = self._native_series
res = np.flatnonzero(ser)
return self._from_native_series(
native_series_from_iterable(
res,
name=self.name,
index=range(len(res)),
implementation=self._implementation,
)
)
result = ser.__class__(range(len(ser)), name=ser.name, index=ser.index).loc[ser]
return self._from_native_series(result)
Comment on lines -224 to +226
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.flatnonzero doesn't play well with cudf - but it's actually not too bad to keep it dataframe-native 😎


# Binary comparisons

Expand Down
6 changes: 5 additions & 1 deletion narwhals/_pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ def set_axis(
implementation: Implementation,
backend_version: tuple[int, ...],
) -> T:
if implementation is Implementation.CUDF: # pragma: no cover
obj = obj.copy(deep=False) # type: ignore[attr-defined]
obj.index = index # type: ignore[attr-defined]
return obj
Comment on lines +200 to +203
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cudf.Series doesn't have set_axis wtf

if implementation is Implementation.PANDAS and backend_version < (
1,
): # pragma: no cover
Expand All @@ -210,7 +214,7 @@ def set_axis(
kwargs["copy"] = False
else: # pragma: no cover
pass
return obj.set_axis(index, axis=0, **kwargs) # type: ignore[no-any-return, attr-defined]
return obj.set_axis(index, axis=0, **kwargs) # type: ignore[attr-defined, no-any-return]


def translate_dtype(column: Any) -> DType:
Expand Down
4 changes: 2 additions & 2 deletions narwhals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,8 +1114,8 @@ def arg_true(self) -> Self:

>>> func(df_pd)
a
0 1
1 2
1 1
2 2
>>> func(df_pl)
shape: (2, 1)
β”Œβ”€β”€β”€β”€β”€β”
Expand Down
4 changes: 2 additions & 2 deletions narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ def arg_true(self) -> Self:
We can then pass either pandas or Polars to `func`:

>>> func(s_pd)
0 1
1 2
1 1
2 2
Name: a, dtype: int64
>>> func(s_pl) # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
covdefaults
pandas
polars[timezones]
polars
pre-commit
pyarrow
pytest
Expand Down
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pyarrow as pa
import pytest

from narwhals.dependencies import get_cudf
from narwhals.dependencies import get_dask_dataframe
from narwhals.dependencies import get_modin
from narwhals.typing import IntoDataFrame
Expand All @@ -17,6 +18,8 @@
import modin.pandas # noqa: F401
with contextlib.suppress(ImportError):
import dask.dataframe # noqa: F401
with contextlib.suppress(ImportError):
import cudf # noqa: F401


def pytest_addoption(parser: Any) -> None:
Expand Down Expand Up @@ -56,6 +59,11 @@ def modin_constructor(obj: Any) -> IntoDataFrame: # pragma: no cover
return mpd.DataFrame(pd.DataFrame(obj)).convert_dtypes(dtype_backend="pyarrow") # type: ignore[no-any-return]


def cudf_constructor(obj: Any) -> IntoDataFrame: # pragma: no cover
cudf = get_cudf()
return cudf.DataFrame(obj) # type: ignore[no-any-return]


def polars_eager_constructor(obj: Any) -> IntoDataFrame:
return pl.DataFrame(obj)

Expand Down Expand Up @@ -87,7 +95,8 @@ def pyarrow_table_constructor(obj: Any) -> IntoDataFrame:

if get_modin() is not None: # pragma: no cover
eager_constructors.append(modin_constructor)
# TODO(unassigned): when Dask gets better support, remove the "False and" part
if get_cudf() is not None:
eager_constructors.append(cudf_constructor) # pragma: no cover
if get_dask_dataframe() is not None: # pragma: no cover
lazy_constructors.append(dask_lazy_constructor) # type: ignore # noqa: PGH003

Expand Down
Loading