Skip to content

Aggregation rules

Aggregation rules

SQL-like group by and aggregation.

Importing the rules

The rules are implemented by each backend. Import them from the package of the backend you use, e.g. from etlrules.backends.pandas import AggregateRule (or etlrules.backends.polars, etlrules.backends.dask, etlrules.backends.pyarrow). The arguments and the behaviour are the same in all the backends, unless stated otherwise.

AggregateRule

Performs a SQL-like groupby and aggregation.

It takes a list of columns to group by and the result will have one row for each unique combination of values in the group_by columns.
The rest of the columns (not in the group_by) can be aggregated using either pre-defined aggregations or using custom python expressions.

Parameters:

Name Type Description Default
group_by Iterable[str]

A list of columns to group the result by

required
aggregations Optional[Mapping[str, str]]

A mapping {column_name: aggregation_function} which specifies how to aggregate columns which are not in the group_by list.
The following list of aggregation functions are supported:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
min: minimum of the values in the group
max: minimum of the values in the group
mean: The mathematical mean value in the group
count: How many values are in the group, including NA
countNoNA: How many values are in the group, excluding NA
sum: The sum of the values in the group
first: The first value in the group
last: The last value in the group
list: Produces a python list with all the values in the group, excluding NA
csv: Produces a comma separated string of values, exluding NA
None
aggregation_expressions Optional[Mapping[str, str]]

A mapping {column_name: aggregation_expression} which specifies how to aggregate columns which are not in the group_by list.
The aggregation expression is a string representing a valid Python expression which gets evaluated.
The input will be in a variable values. isnull can be used to filter out NA.

Example:

1
2
3
{"C": "';'.join(str(v) for v in values if not isnull(v))"}

The above aggregates the column C by producing a ; separated string of values in the group, excluding NA.

The dask backend doesn't support aggregation_expressions.

None
aggregation_types Optional[Mapping[str, str]]

An optional mapping of {column_name: column_type} which converts the respective output column to the given type. The supported types are: int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64, string, boolean, datetime and timedelta.

None
named_input Optional[str]

Which dataframe to use as the input. Optional.
When not set, the input is taken from the main output.
Set it to a string value, the name of an output dataframe of a previous rule.

None
named_output Optional[str]

Give the output of this rule a name so it can be used by another rule as a named input. Optional.
When not set, the result of this rule will be available as the main output.
When set to a name (string), the result will be available as that named output.

None
name Optional[str]

Give the rule a name. Optional.
Named rules are more descriptive as to what they're trying to do/the intent.

None
description Optional[str]

Describe in detail what the rules does, how it does it. Optional.
Together with the name, the description acts as the documentation of the rule.

None
strict bool

When set to True, the rule does a stricter valiation. Default: True

True

Raises:

Type Description
ColumnAlreadyExistsError

raised if a column appears in multiple places in group_by/aggregations/aggregation_expressions.

ExpressionSyntaxError

raised if any aggregation expression (if any are passed in) has a Python syntax error.

MissingColumnError

raised in strict mode only if a column specified in aggregations or aggregation_expressions is missing from the input dataframe. If aggregation_types are specified, it is raised in strict mode if a column in the aggregation_types is missing from the input dataframe.

UnsupportedTypeError

raised if a type specified in aggregation_types is not supported.

ValueError

raised if a column in aggregations is trying to be aggregated using an unknown aggregate function

TypeError

raised if an operation is not supported between the types involved

NameError

raised if an unknown variable is used

Note

Other Python exceptions can be raised when custom aggregation expressions are used, depending on what the expression is doing.

Note

Any columns not in the group_by list and not present in either aggregations or aggregation_expressions will be dropped from the result.