Skip to content

Commit

Permalink
fix aritmetic bug introduced in #3709 (#3741)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jun 19, 2022
1 parent 4c6f9cd commit 6cafa8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion polars/polars-lazy/src/physical_plan/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl PhysicalExpr for BinaryExpr {
// flatten the Series and apply the operators
(AggState::AggregatedList(_), AggState::AggregatedList(_), _) => {
let lhs = ac_l.flat_naive().as_ref().clone();
let rhs = ac_l.flat_naive().as_ref().clone();
let rhs = ac_r.flat_naive().as_ref().clone();

// drop lhs so that we might operate in place
{
Expand Down
25 changes: 25 additions & 0 deletions py-polars/tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,28 @@ def test_groupby_agg_equals_zero_3535() -> None:
"val1": [10, 0, -99],
"val2": [None, 0.0, 10.5],
}


def test_arithmetic_in_aggregation_3739() -> None:
def demean_dot() -> pl.Expr:
x = pl.col("x")
y = pl.col("y")
x1 = x - x.mean()
y1 = y - y.mean()
return (x1 * y1).sum().alias("demean_dot")

assert (
pl.DataFrame(
{
"key": ["a", "a", "a", "a"],
"x": [4, 2, 2, 4],
"y": [2, 0, 2, 0],
}
)
.groupby("key")
.agg(
[
demean_dot(),
]
)
).to_dict(False) == {"key": ["a"], "demean_dot": [0.0]}

0 comments on commit 6cafa8b

Please sign in to comment.