Skip to content

Commit

Permalink
fix: Properly raise on mean_horizontal with wrong dtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 27, 2024
1 parent e7a84bc commit ae89c81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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"))

0 comments on commit ae89c81

Please sign in to comment.