Skip to content

Commit

Permalink
test: Add cudf_constructor to conftest (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Aug 16, 2024
1 parent a8220c8 commit dec35e5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
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)

# 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
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

0 comments on commit dec35e5

Please sign in to comment.