Repository: pola-rs/polars
File: crates/polars-expr/src/expressions/aggregation.rs (Rust)

Code before:
                    AggregatedScalar(agg_s.with_name(keep_name))
                },
                GroupByMethod::Implode => {
                    // if the aggregation is already
                    // in an aggregate flat state for instance by
                    // a mean aggregation, we simply convert to list
                    //
                    // if it is not, we traverse the groups and create
                    // a list per group.
                    let c = match ac.agg_state() {
                        // mean agg:
                        // -> f64 -> list<f64>
                        AggregatedScalar(c) => c
                            .reshape_list(&[
                                ReshapeDimension::Infer,
                                ReshapeDimension::new_dimension(1),
                            ])
                            .unwrap(),
                        // Auto-imploded
                        AggState::NotAggregated(_) | AggState::AggregatedList(_) => {
                            ac._implode_no_agg();
                            return Ok(ac);
                        },
                        _ => {
                            let agg = ac.aggregated();
                            agg.as_list().into_column()
                        },
                    };
                    AggState::AggregatedList(c.with_name(keep_name))
                },
                GroupByMethod::Groups => {
                    let mut column: ListChunked = ac.groups().as_list_chunked();
                    column.rename(keep_name);
                    AggregatedScalar(column.into_column())
                },
                GroupByMethod::Std(ddof) => {
                    let (c, groups) = ac.get_final_aggregation();
                    let agg_c = c.agg_std(&groups, ddof);
                    AggregatedScalar(agg_c.with_name(keep_name))
                },
                GroupByMethod::Var(ddof) => {
                    let (c, groups) = ac.get_final_aggregation();
                    let agg_c = c.agg_var(&groups, ddof);
                    AggregatedScalar(agg_c.with_name(keep_name))
                },
                GroupByMethod::Quantile(_, _) => {
                    // implemented explicitly in AggQuantile struct
                    unimplemented!()
                },
                GroupByMethod::NanMin => {
                    #[cfg(feature = "propagate_nans")]

Review comment (bug):
nit: Do we ever hit this with the wrong name set? I thought `.cast` always maintained the name.

Code after:
                    AggregatedScalar(agg_s.with_name(keep_name))
                },
                GroupByMethod::Implode => {
                    // If the aggregation is already in an aggregate flat state (AggregatedScalar), for instance by
                    // a mean() aggregation, we simply wrap into a list and maintain the AggregatedScalar state
                    //
                    // If it is not, we traverse the groups and create a list per group.
                    let c = match ac.agg_state() {
                        // mean agg:
                        // -> f64 -> list<f64>
                        AggregatedScalar(c) => c
                            .cast(&DataType::List(Box::new(c.dtype().clone())))
                            .unwrap(),
                        // Auto-imploded
                        AggState::NotAggregated(_) | AggState::AggregatedList(_) => {
                            ac._implode_no_agg();
                            return Ok(ac);
                        },
                        _ => {
                            let agg = ac.aggregated();
                            agg.as_list().into_column()
                        },
                    };
                    match ac.agg_state() {
                        // An imploded scalar remains a scalar
                        AggregatedScalar(_) => AggregatedScalar(c.with_name(keep_name)),
                        _ => AggState::AggregatedList(c.with_name(keep_name)),
                    }
                },
                GroupByMethod::Groups => {
                    let mut column: ListChunked = ac.groups().as_list_chunked();
                    column.rename(keep_name);
                    AggregatedScalar(column.into_column())
                },
                GroupByMethod::Std(ddof) => {
                    let (c, groups) = ac.get_final_aggregation();
                    let agg_c = c.agg_std(&groups, ddof);
                    AggregatedScalar(agg_c.with_name(keep_name))
                },
                GroupByMethod::Var(ddof) => {
                    let (c, groups) = ac.get_final_aggregation();
                    let agg_c = c.agg_var(&groups, ddof);
                    AggregatedScalar(agg_c.with_name(keep_name))
                },
                GroupByMethod::Quantile(_, _) => {
                    // implemented explicitly in AggQuantile struct
                    unimplemented!()
                },
                GroupByMethod::NanMin => {
                    #[cfg(feature = "propagate_nans")]
                    {