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: Properly raise on mean_horizontal with wrong dtypes #19472

Merged
merged 1 commit into from
Oct 27, 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
3 changes: 2 additions & 1 deletion crates/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2851,7 +2851,8 @@ impl DataFrame {
dtype.is_numeric() || matches!(dtype, DataType::Boolean)
})
.cloned()
.collect();
.collect::<Vec<_>>();
polars_ensure!(!columns.is_empty(), InvalidOperation: "'horizontal_mean' expected at least 1 numerical column");
let numeric_df = unsafe { DataFrame::_new_no_checks_impl(self.height(), columns) };

let sum = || numeric_df.sum_horizontal(null_strategy);
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,15 @@ def test_raise_invalid_agg() -> None:
.group_by("index")
.agg(pl.col("foo").filter(pl.col("i_do_not_exist")))
).collect()


def test_err_mean_horizontal_lists() -> None:
df = pl.DataFrame(
{
"experiment_id": [1, 2],
"sensor1": [[1, 2, 3], [7, 8, 9]],
"sensor2": [[4, 5, 6], [10, 11, 12]],
}
)
with pytest.raises(pl.exceptions.InvalidOperationError):
df.with_columns(pl.mean_horizontal("sensor1", "sensor2").alias("avg_sensor"))