Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 19 additions & 2 deletions narwhals/_pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ def clip(
lower_bound: Self | NumericLiteral | TemporalLiteral | None,
upper_bound: Self | NumericLiteral | TemporalLiteral | None,
) -> Self:
native = self.native
Copy link
Member

Choose a reason for hiding this comment

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

Needs to be done with the other suggestion, but out of range 😒

Suggested change
native = self.native

_, lower = (
align_and_extract_native(self, lower_bound)
if lower_bound is not None
Expand All @@ -824,8 +825,24 @@ def clip(
if upper_bound is not None
else (None, None)
)
kwargs = {"axis": 0} if self._implementation is Implementation.MODIN else {}
return self._with_native(self.native.clip(lower, upper, **kwargs))
if self._implementation.is_pandas():
result = native.clip(lower, upper)
else:
native_cls = type(native)
if isinstance(lower, native_cls) or isinstance(upper, native_cls):
# Workaround for both cudf and modin when clipping with a series
# * cudf: https://github.com/rapidsai/cudf/issues/17682
# * modin: https://github.com/modin-project/modin/issues/7415
result = native
if lower is not None:
result = result.where(result >= lower, lower)
if upper is not None:
result = result.where(result <= upper, upper)
else:
kwargs = {"axis": 0} if self._implementation.is_modin() else {}
result = self.native.clip(lower, upper, **kwargs)

return self._with_native(result)

def to_arrow(self) -> pa.Array[Any]:
if self._implementation is Implementation.CUDF:
Expand Down
20 changes: 2 additions & 18 deletions tests/expr_and_series/clip_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ def test_clip_expr(
assert_equal_data(result, {"result": expected})


def test_clip_expr_expressified(
request: pytest.FixtureRequest, constructor: Constructor
) -> None:
if "modin_pyarrow" in str(constructor):
request.applymarker(pytest.mark.xfail)
if "cudf" in str(constructor):
# https://github.com/rapidsai/cudf/issues/17682
request.applymarker(pytest.mark.xfail)

def test_clip_expr_expressified(constructor: Constructor) -> None:
data = {"a": [1, 2, 3, -4, 5], "lb": [3, 2, 1, 1, 1], "ub": [4, 4, 2, 2, 2]}
df = nw.from_native(constructor(data))
result = df.select(nw.col("a").clip("lb", nw.col("ub") + 1))
Expand Down Expand Up @@ -63,15 +55,7 @@ def test_clip_series(
assert_equal_data(result, {"result": expected})


def test_clip_series_expressified(
request: pytest.FixtureRequest, constructor_eager: ConstructorEager
) -> None:
if "modin_pyarrow" in str(constructor_eager):
request.applymarker(pytest.mark.xfail)
if "cudf" in str(constructor_eager):
# https://github.com/rapidsai/cudf/issues/17682
request.applymarker(pytest.mark.xfail)

def test_clip_series_expressified(constructor_eager: ConstructorEager) -> None:
data = {"a": [1, 2, 3, -4, 5], "lb": [3, 2, 1, 1, 1], "ub": [4, 4, 2, 2, 2]}
df = nw.from_native(constructor_eager(data), eager_only=True)
result = df["a"].clip(df["lb"], df["ub"] + 1).to_frame()
Expand Down
Loading