Skip to content

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
# replaces the missing values in columns A and B with 0
rule = FillNullRule(["A", "B"], 0)
rule.apply(data)

# replaces the missing values in column C with "unknown"
rule = FillNullRule(["C"], "unknown")
rule.apply(data)

Example:

1
2
3
4
Given df:
| A    | B    |
| 1    | NA   |
| NA   | 2    |

FillNullRule(["A", "B"], 0).apply(df)

Result:

1
2
3
| A   | B  |
| 1   | 0  |
| 0   | 2  |

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
# removes the rows with a missing value in any of the columns
rule = DropNullRowsRule()
rule.apply(data)

# removes the rows with a missing value in column A or in column B
rule = DropNullRowsRule(["A", "B"])
rule.apply(data)

# removes the rows where both columns A and B have missing values
rule = DropNullRowsRule(["A", "B"], how="all")
rule.apply(data)

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
# Phone is the mobile number, or the home number if the mobile is missing, or the work number
rule = CoalesceRule(["Mobile", "Home", "Work"], "Phone")
rule.apply(data)

Example:

1
2
3
4
5
6
Given df:
| Mobile | Home | Work |
| 111    | 222  | NA   |
| NA     | 444  | 555  |
| NA     | NA   | 666  |
| NA     | NA   | NA   |

CoalesceRule(["Mobile", "Home", "Work"], "Phone").apply(df)

Result:

1
2
3
4
5
| Mobile | Home | Work | Phone |
| 111    | 222  | NA   | 111   |
| NA     | 444  | 555  | 444   |
| NA     | NA   | 666  | 666   |
| NA     | NA   | NA   | NA    |

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.