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.
|
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. Example:
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. |
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. |
None
|
||
name
|
Optional[str]
|
Give the rule a name. Optional. |
None
|
||
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. |
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.