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

Fix Dask-cuDF clip APIs #17509

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix dask-cudf clip
  • Loading branch information
rjzamora committed Dec 4, 2024
commit 7eacf50b8aaa841d5a638252c8a8620213ce16d7
10 changes: 10 additions & 0 deletions python/dask_cudf/dask_cudf/_expr/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ def read_text(*args, **kwargs):

return legacy_read_text(*args, **kwargs)

def clip(self, lower=None, upper=None, axis=1):
if axis not in (None, 1):
raise NotImplementedError("axis not yet supported in clip.")
return new_collection(self.expr.clip(lower, upper, 1))


class Series(DXSeries, CudfFrameBase):
def groupby(self, by, **kwargs):
Expand All @@ -182,6 +187,11 @@ def struct(self):

return StructMethods(self)

def clip(self, lower=None, upper=None, axis=1):
if axis not in (None, 1):
raise NotImplementedError("axis not yet supported in clip.")
return new_collection(self.expr.clip(lower, upper, 1))


class Index(DXIndex, CudfFrameBase):
pass # Same as pandas (for now)
Expand Down
26 changes: 26 additions & 0 deletions python/dask_cudf/dask_cudf/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,3 +1019,29 @@ def test_rename_axis_after_join():
result = ddf1.join(ddf2, how="outer")
expected = df1.join(df2, how="outer")
dd.assert_eq(result, expected, check_index=False)


def test_clip_dataframe():
df = cudf.DataFrame(
{
"id": ["a", "b", "c", "d"],
"score": [-1, 1, 4, 6],
}
)
expect = df.clip(lower=["b", 1], upper=["d", 5], axis=1)
got = dd.from_pandas(df, npartitions=2).clip(
lower=["b", 1], upper=["d", 5], axis=1
)
dd.assert_eq(expect, got)


def test_clip_series():
ser = cudf.Series([-0.5, 0.5, 4.5, 5.5])
expect = ser.clip(lower=0, upper=5).round().astype(int)
got = (
dd.from_pandas(ser, npartitions=2)
.clip(lower=0, upper=5)
.round()
.astype(int)
)
dd.assert_eq(expect, got)