Skip to content

Commit

Permalink
fix bug in flattened apply (groupby)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 15, 2021
1 parent 4923eb5 commit 0d7555a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions polars/polars-lazy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ test = [
"regex",
"polars-core/plain_fmt",
"diff",
"abs",
]

[dependencies]
Expand Down
4 changes: 3 additions & 1 deletion polars/polars-lazy/src/physical_plan/expressions/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ impl PhysicalExpr for ApplyExpr {
}
ApplyOptions::ApplyFlat => {
let s = self.function.call_udf(&mut [ac.flat().into_owned()])?;
ac.with_update_groups(UpdateGroups::WithGroupsLen);
if ac.is_aggregated() {
ac.with_update_groups(UpdateGroups::WithGroupsLen);
}
ac.with_series(s, false);
Ok(ac)
}
Expand Down
20 changes: 20 additions & 0 deletions polars/polars-lazy/src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2418,3 +2418,23 @@ fn empty_df() -> Result<()> {

Ok(())
}

#[test]
fn test_apply_flatten() -> Result<()> {
let df = df![
"A"=> [1.1435, 2.223456, 3.44732, -1.5234, -2.1238, -3.2923],
"B"=> ["a", "b", "a", "b", "a", "b"]
]?;

let out = df
.lazy()
.stable_groupby([col("B")])
.agg([col("A").abs().sum()])
.collect()?;

let out = out.column("A_sum")?;
assert_eq!(out.get(0), AnyValue::Float64(6.71462));
assert_eq!(out.get(1), AnyValue::Float64(7.039156));

Ok(())
}

0 comments on commit 0d7555a

Please sign in to comment.