Missing values rules
Missing values rules¶
Rules to replace missing values (NA/nulls) with a given value, drop the rows with missing values and take the first non-missing value from a list of columns. To fill missing values with values from other rows, see the fill rules.
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 FillNullRule (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.
FillNullRule ¶
Replaces the missing values (NA/nulls) in a set of columns with a given value.
Basic usage:
1 2 3 4 5 6 7 | |
Example:
1 2 3 4 | |
FillNullRule(["A", "B"], 0).apply(df)
Result:
1 2 3 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Iterable[str]
|
The columns in which to replace the missing values. The rest of the columns in the dataframe are not affected. |
required |
value
|
Union[int, float, str, bool]
|
The value to replace the missing values with. It should be of a type compatible with the columns (e.g. a number for numeric columns, a string for string 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 validation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if any of the columns are missing from the input dataframe. |
See also
ForwardFillRule and BackFillRule fill the missing values with values from other rows.
DropNullRowsRule ¶
Removes the rows with missing values (NA/nulls).
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Optional[Iterable[str]]
|
The columns to check for missing values. Optional. When not specified, all the columns are checked. |
None
|
how
|
Literal['any', 'all']
|
One of: any: a row is removed if any of the columns has a missing value (default) all: a row is removed only if all the columns have missing values |
'any'
|
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
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if any of the columns are missing from the input dataframe. |
CoalesceRule ¶
Creates a column with the first non-missing value from a list of columns.
Basic usage:
1 2 3 | |
Example:
1 2 3 4 5 6 | |
CoalesceRule(["Mobile", "Home", "Work"], "Phone").apply(df)
Result:
1 2 3 4 5 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Iterable[str]
|
The columns to take the values from, in order of preference. The columns should have the same (or compatible) types. |
required |
output_column
|
str
|
The name of the new column with the result. |
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
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if any of the columns are missing from the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
Note
In non-strict mode, an existing output_column is overwritten.