Skip to content
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
18 changes: 16 additions & 2 deletions narwhals/_pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,22 @@ 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))
impl = self._implementation
kwargs: dict[str, Any] = {"axis": 0} if impl.is_modin() else {}
result = self.native

if not impl.is_pandas():
# 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
if self._is_native(lower):
result = result.where(result >= lower, lower)
lower = None
if self._is_native(upper):
result = result.where(result <= upper, upper)
upper = None

return self._with_native(result.clip(lower, upper, **kwargs))

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