Skip to content

Commit 791777f

Browse files
authored
Add support for arrays in lower and upper args of DataFrame.clip (#982)
* Supports arrays in DataFrame.clip (#980) * add missing assert_type (#980) * check the content of asserted type
1 parent f8b06e1 commit 791777f

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

pandas-stubs/core/frame.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,8 @@ class DataFrame(NDFrame, OpsMixin):
16321632
) -> DataFrame: ...
16331633
def clip(
16341634
self,
1635-
lower: float | None = ...,
1636-
upper: float | None = ...,
1635+
lower: float | AnyArrayLike | None = ...,
1636+
upper: float | AnyArrayLike | None = ...,
16371637
*,
16381638
axis: Axis | None = ...,
16391639
inplace: _bool = ...,

tests/test_frame.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,23 @@ def test_types_quantile() -> None:
527527
df.quantile(np.array([0.25, 0.75]))
528528

529529

530-
def test_types_clip() -> None:
530+
@pytest.mark.parametrize("lower", [None, 5, pd.Series([3, 4])])
531+
@pytest.mark.parametrize("upper", [None, 15, pd.Series([12, 13])])
532+
@pytest.mark.parametrize("axis", [None, 0, "index"])
533+
def test_types_clip(lower, upper, axis) -> None:
534+
def is_none_or_numeric(val: Any) -> bool:
535+
return val is None or isinstance(val, int | float)
536+
531537
df = pd.DataFrame(data={"col1": [20, 12], "col2": [3, 14]})
532-
df.clip(lower=5, upper=15)
538+
uses_array = not (is_none_or_numeric(lower) and is_none_or_numeric(upper))
539+
if uses_array and axis is None:
540+
with pytest.raises(ValueError):
541+
df.clip(lower=lower, upper=upper, axis=axis)
542+
else:
543+
check(
544+
assert_type(df.clip(lower=lower, upper=upper, axis=axis), pd.DataFrame),
545+
pd.DataFrame,
546+
)
533547

534548

535549
def test_types_abs() -> None:

0 commit comments

Comments
 (0)