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: Error on some invalid clip inputs #14416

Merged
merged 2 commits into from
Feb 11, 2024
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
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
Loading