diff --git a/crates/polars-core/src/frame/mod.rs b/crates/polars-core/src/frame/mod.rs index 8ce1525b2ed2..6be57d1e7f49 100644 --- a/crates/polars-core/src/frame/mod.rs +++ b/crates/polars-core/src/frame/mod.rs @@ -2851,7 +2851,8 @@ impl DataFrame { dtype.is_numeric() || matches!(dtype, DataType::Boolean) }) .cloned() - .collect(); + .collect::>(); + 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); diff --git a/py-polars/tests/unit/test_errors.py b/py-polars/tests/unit/test_errors.py index c730ee8d30a7..d5338acd2547 100644 --- a/py-polars/tests/unit/test_errors.py +++ b/py-polars/tests/unit/test_errors.py @@ -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"))