Basic rules
Basic rules¶
Rules to dedupe, project, rename, replace values, sort, limit the number of rows and explode, and to group rules into blocks.
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 DedupeRule (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.
DedupeRule ¶
De-duplicates by dropping duplicates using a set of columns to determine the duplicates.
It has logic to keep the first, last or none of the duplicate in a set of duplicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Iterable[str]
|
A subset of columns in the data frame which are used to determine the set of duplicates. Any rows that have the same values in these columns are considered to be duplicates. |
required |
keep
|
Literal[KEEP_FIRST, KEEP_LAST, KEEP_NONE]
|
What to keep in the de-duplication process. One of: first: keeps the first row in the duplicate set last: keeps the last row in the duplicate set none: drops all the duplicates |
KEEP_FIRST
|
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 |
|---|---|
MissingColumnError
|
raised when a column specified to deduplicate on doesn't exist in the input data frame. |
Note
MissingColumnError is raised in both strict and non-strict modes. This is because the rule cannot operate reliably without a correct set of columns.
ProjectRule ¶
Reshapes the data frame to keep, eliminate or re-order the set of columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Iterable[str]
|
The list of columns to keep or eliminate from the data frame. The order of column names will be reflected in the result data frame, so this rule can be used to re-order columns. |
required |
exclude
|
bool
|
When set to True, the columns in the columns arg will be excluded from the data frame. Boolean. Default: False In strict mode, if any column specified in the columns arg doesn't exist in the input data frame, a MissingColumnError exception is raised. In non strict mode, the missing columns are ignored. |
False
|
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 |
|---|---|
MissingColumnError
|
raised in strict mode only, if any columns are missing from the input data frame. |
RenameRule ¶
Renames a set of columns in the data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
Mapping[str, str]
|
A dictionary of old names (keys) and new names (values) to be used for the rename operation The order of column names will be reflected in the result data frame, so this rule can be used to re-order columns. |
required |
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 |
|---|---|
MissingColumnError
|
raised in strict mode only, if any columns (keys) are missing from the input data frame. |
ReplaceRule ¶
Replaces some some values (or regular expressions) with another set of values (or regular expressions).
Basic usage:
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
A column with the input values. |
required |
values
|
Iterable[Union[int, float, str]]
|
A sequence of values to replace. Regular expressions can be used to match values more widely, in which case, the regex parameter must be set to True. Values can be any supported types but they should match the type of the columns. |
required |
new_values
|
Iterable[Union[int, float, str]]
|
A sequence of the same length as values. Each value within new_values will replace the corresponding value in values (at the same index). New values can be any supported types but they should match the type of the columns. |
required |
regex
|
bool
|
True if all the values and new_values are to be interpreted as regular expressions. Default: False. regex=True is only applicable to string columns. |
False
|
output_column
|
Optional[str]
|
An optional column to hold the result with the new values. Optional. If provided, if must have the same length as the columns sequence. The existing columns are unchanged, and new columns are created with the upper case values. If not provided, the result is updated in place. |
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 |
|---|---|
MissingColumnError
|
raised if the input_column column doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
Note
In non-strict mode, overwriting existing columns is ignored.
SortRule ¶
Sort the input dataframe by the given columns, either ascending or descending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sort_by
|
Iterable[str]
|
Either a single column speified as a string or a list or tuple of columns to sort by |
required |
ascending
|
Union[bool, Iterable[bool]]
|
Whether to sort ascending or descending. Boolean. Default: True |
True
|
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 |
|---|---|
MissingColumnError
|
raised when a column in the sort_by doesn't exist in the input dataframe. |
Note
When multiple columns are specified, the first column decides the sort order. For any rows that have the same value in the first column, the second column is used to decide the sort order within that group and so on.
LimitRule ¶
Keeps only the first n rows of the dataframe.
Basic usage:
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number of rows to keep. If the dataframe has fewer rows, all the rows are kept. |
required |
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 validation. Default: True |
True
|
ExplodeValuesRule ¶
Explode a list of values into multiple rows with each value on a separate row
Example:
1 2 3 4 5 6 | |
ExplodeValuesRule("A").apply(df)
Result:
1 2 3 4 5 6 7 8 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
A column with values to round as per the specified scale. |
required |
column_type
|
Optional[str]
|
An optional string with the type of the resulting exploded column. When not specified, the column_type is backend implementation specific. |
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 |
|---|---|
MissingColumnError
|
raised if the input column doesn't exist in the input dataframe. |
RulesBlock ¶
Groups rules into encapsulated blocks or units of rules that achieve one thing. Blocks are reusable and encapsulated to reduce complexity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
Iterable[BaseRule]
|
An iterable of rules which are part of this block. The first rule in the block will take its input from the named_input of the RulesBlock (if any, if not from the main output of the previous rule). The last rule in the block will publish the output as the named_output of the RulesBlock (if any, or the main output of the block). Any named outputs in the block are not exposed to the rules outside of the block (proper encapsulation). |
required |
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
|