From ce7a915cb8dda185f55c9f374cd48eeb96de7f5c Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Tue, 3 Sep 2024 15:48:40 +0000 Subject: [PATCH] fix(rust): Ensure result name of pow matches schema in grouped context We must transfer the name from the base column to the result so that the rule that the left operand produces the result name is obeyed. - Closes #18524 --- crates/polars-plan/src/dsl/function_expr/pow.rs | 5 ++++- py-polars/tests/unit/operations/test_group_by.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/polars-plan/src/dsl/function_expr/pow.rs b/crates/polars-plan/src/dsl/function_expr/pow.rs index a12ef242f435..5336220d1ace 100644 --- a/crates/polars-plan/src/dsl/function_expr/pow.rs +++ b/crates/polars-plan/src/dsl/function_expr/pow.rs @@ -37,12 +37,15 @@ where ChunkedArray: IntoSeries, { if (base.len() == 1) && (exponent.len() != 1) { + let name = base.name(); let base = base .get(0) .ok_or_else(|| polars_err!(ComputeError: "base is null"))?; Ok(Some( - unary_elementwise_values(exponent, |exp| Pow::pow(base, exp)).into_series(), + unary_elementwise_values(exponent, |exp| Pow::pow(base, exp)) + .into_series() + .with_name(name.clone()), )) } else { Ok(Some( diff --git a/py-polars/tests/unit/operations/test_group_by.py b/py-polars/tests/unit/operations/test_group_by.py index 3a5436b91299..09864b329c22 100644 --- a/py-polars/tests/unit/operations/test_group_by.py +++ b/py-polars/tests/unit/operations/test_group_by.py @@ -861,7 +861,7 @@ def test_group_by_apply_first_input_is_literal() -> None: pow = df.group_by("g").agg(2 ** pl.col("x")) assert pow.sort("g").to_dict(as_series=False) == { "g": [1, 2], - "x": [[2.0, 4.0], [8.0, 16.0, 32.0]], + "literal": [[2.0, 4.0], [8.0, 16.0, 32.0]], }