Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AggFormula in io.deephaven.api.agg.Aggregation, fixes #1699 #1813

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pyintegration/deephaven2/agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ def first(cols: List[str]) -> Aggregation:
return Aggregation(j_aggregation=_JAggregation.AggFirst(*cols))


def formula(formula: str, formula_param: str, cols: List[str]) -> Aggregation:
""" Create a user defined formula aggregation.

Args:
formula (str): the user defined formula to apply to each group
formula_param (str): the parameter name within the formula
cols (List[str]): the columns to aggregate on, can be renaming expressions, i.e. "new_col = col"

Returns:
an aggregation
"""

return Aggregation(j_aggregation=_JAggregation.AggFormula(formula, formula_param, *cols))


def last(cols: List[str]) -> Aggregation:
""" Create Last aggregation.

Expand Down
6 changes: 3 additions & 3 deletions pyintegration/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from deephaven2 import DHError, read_csv, empty_table, SortDirection
from deephaven2.agg import sum_, weighted_avg, avg, pct, group, count_, first, last, max_, median, min_, std, abs_sum, \
var
var, formula
from deephaven2.table import Table
from tests.testbase import BaseTestCase

Expand Down Expand Up @@ -258,7 +258,8 @@ def test_agg_by(self):
aggs = [sum_(cols=["SumC=c"]),
avg(cols=["AvgB = b", "AvgD = d"]),
pct(percentile=0.5, cols=["PctC = c"]),
weighted_avg(wcol="d", cols=["WavGD = d"])]
weighted_avg(wcol="d", cols=["WavGD = d"]),
formula(formula="min(each)", formula_param="each", cols=["MinA=a", "MinD=d"])]

result_table = self.test_table.agg_by(aggs=aggs, by=["a"])
self.assertEqual(result_table.size, num_distinct_a)
Expand All @@ -283,7 +284,6 @@ def test_agg_by_2(self):
weighted_avg("var", ["weights"])]

result_table = test_table.agg_by(aggs, ["dumb"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not part of this review, but I think the variable name dumb is "improper". I'd advise choosing completely "neutral" names commonly used (foo, bar, baz), or choose something "meaningful" such as key, or my_group. I'd suggest an audit of the client for other potentially "improper" names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do.

# TODO: AggFormula - this is terrible

self.assertGreaterEqual(result_table.size, 1)

Expand Down
5 changes: 5 additions & 0 deletions table-api/src/main/java/io/deephaven/api/agg/Aggregation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.deephaven.api.agg.NormalAggregations.Builder;
import io.deephaven.api.agg.spec.AggSpec;
import io.deephaven.api.agg.spec.AggSpecFormula;

import java.io.Serializable;
import java.util.Collection;
Expand Down Expand Up @@ -63,6 +64,10 @@ static Aggregation AggFirst(String... pairs) {
return of(AggSpec.first(), pairs);
}

static Aggregation AggFormula(String formula, String formulaParam, String... pairs) {
return of(AggSpec.formula(formula, formulaParam), pairs);
}

static Aggregation AggGroup(String... pairs) {
return of(AggSpec.group(), pairs);
}
Expand Down