Skip to content

Commit 4ddae71

Browse files
authored
fix: Properly raise on mean_horizontal with wrong dtypes (#19472)
1 parent 98fcc3f commit 4ddae71

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

crates/polars-core/src/frame/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,8 @@ impl DataFrame {
28512851
dtype.is_numeric() || matches!(dtype, DataType::Boolean)
28522852
})
28532853
.cloned()
2854-
.collect();
2854+
.collect::<Vec<_>>();
2855+
polars_ensure!(!columns.is_empty(), InvalidOperation: "'horizontal_mean' expected at least 1 numerical column");
28552856
let numeric_df = unsafe { DataFrame::_new_no_checks_impl(self.height(), columns) };
28562857

28572858
let sum = || numeric_df.sum_horizontal(null_strategy);

py-polars/tests/unit/test_errors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,3 +708,15 @@ def test_raise_invalid_agg() -> None:
708708
.group_by("index")
709709
.agg(pl.col("foo").filter(pl.col("i_do_not_exist")))
710710
).collect()
711+
712+
713+
def test_err_mean_horizontal_lists() -> None:
714+
df = pl.DataFrame(
715+
{
716+
"experiment_id": [1, 2],
717+
"sensor1": [[1, 2, 3], [7, 8, 9]],
718+
"sensor2": [[4, 5, 6], [10, 11, 12]],
719+
}
720+
)
721+
with pytest.raises(pl.exceptions.InvalidOperationError):
722+
df.with_columns(pl.mean_horizontal("sensor1", "sensor2").alias("avg_sensor"))

0 commit comments

Comments
 (0)