Skip to content

Commit

Permalink
fix: Error on some invalid clip inputs (#14416)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Feb 11, 2024
1 parent 7df4e87 commit 2e3e793
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
21 changes: 9 additions & 12 deletions crates/polars-ops/src/series/ops/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ pub fn clip(s: &Series, min: &Series, max: &Series) -> PolarsResult<Series> {
);

let original_type = s.dtype();
// cast min & max to the dtype of s first.
let (min, max) = (min.cast(s.dtype())?, max.cast(s.dtype())?);
let (min, max) = (min.strict_cast(s.dtype())?, max.strict_cast(s.dtype())?);

let (s, min, max) = (
s.to_physical_repr(),
Expand All @@ -27,9 +26,9 @@ pub fn clip(s: &Series, min: &Series, max: &Series) -> PolarsResult<Series> {
let min: &ChunkedArray<$T> = min.as_ref().as_ref().as_ref();
let max: &ChunkedArray<$T> = max.as_ref().as_ref().as_ref();
let out = clip_helper(ca, min, max).into_series();
if original_type.is_logical(){
if original_type.is_logical() {
out.cast(original_type)
}else{
} else {
Ok(out)
}
})
Expand All @@ -46,8 +45,7 @@ pub fn clip_max(s: &Series, max: &Series) -> PolarsResult<Series> {
);

let original_type = s.dtype();
// cast max to the dtype of s first.
let max = max.cast(s.dtype())?;
let max = max.strict_cast(s.dtype())?;

let (s, max) = (s.to_physical_repr(), max.to_physical_repr());

Expand All @@ -57,9 +55,9 @@ pub fn clip_max(s: &Series, max: &Series) -> PolarsResult<Series> {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
let max: &ChunkedArray<$T> = max.as_ref().as_ref().as_ref();
let out = clip_min_max_helper(ca, max, clamp_max).into_series();
if original_type.is_logical(){
if original_type.is_logical() {
out.cast(original_type)
}else{
} else {
Ok(out)
}
})
Expand All @@ -76,8 +74,7 @@ pub fn clip_min(s: &Series, min: &Series) -> PolarsResult<Series> {
);

let original_type = s.dtype();
// cast min to the dtype of s first.
let min = min.cast(s.dtype())?;
let min = min.strict_cast(s.dtype())?;

let (s, min) = (s.to_physical_repr(), min.to_physical_repr());

Expand All @@ -87,9 +84,9 @@ pub fn clip_min(s: &Series, min: &Series) -> PolarsResult<Series> {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
let min: &ChunkedArray<$T> = min.as_ref().as_ref().as_ref();
let out = clip_min_max_helper(ca, min, clamp_min).into_series();
if original_type.is_logical(){
if original_type.is_logical() {
out.cast(original_type)
}else{
} else {
Ok(out)
}
})
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/operations/test_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ def test_clip_string_input() -> None:
assert_frame_equal(result, expected)


def test_clip_bound_invalid_for_original_dtype() -> None:
s = pl.Series([1, 2, 3, 4], dtype=pl.UInt32)
with pytest.raises(pl.ComputeError, match="conversion from `i32` to `u32` failed"):
s.clip(-1, 5)


def test_clip_min_max_deprecated() -> None:
s = pl.Series([-1, 0, 1])

Expand Down

0 comments on commit 2e3e793

Please sign in to comment.