Skip to content

Validation rules

Validation rules

Rules which check the data and stop the plan with a ValidationError when the check fails (e.g. missing values or duplicate keys before loading the data into a database). The data is not changed: the output of a validation rule is its input.

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 AssertNotNullRule (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.

AssertNotNullRule

Checks that a set of columns don't have missing values (NA/nulls) and raises ValidationError if they do.

The data is not changed: the output of the rule is the input dataframe. Use it to stop a plan early when the data doesn't match the expectations (e.g. before loading it into a database).

Basic usage:

1
2
3
# raises ValidationError if any of the columns Id or Name have missing values
rule = AssertNotNullRule(["Id", "Name"])
rule.apply(data)

Parameters:

Name Type Description Default
columns Optional[Iterable[str]]

The columns to check. Optional. When not specified, all the columns are checked.

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 validation. Default: True

True

Raises:

Type Description
ValidationError

raised if any of the columns have missing values. The message has the number of missing values in each column.

MissingColumnError

raised if any of the columns are missing from the input dataframe.

Note

With the dask backend, the check computes the input dataframe.

AssertUniqueRule

Checks that the values of a column (or the combinations of values of a set of columns) are unique and raises ValidationError if they're not.

The data is not changed: the output of the rule is the input dataframe. Use it, for example, to check that a key is unique before loading the data into a database.

Basic usage:

1
2
3
4
5
6
7
# raises ValidationError if the values in the Id column are not unique
rule = AssertUniqueRule(["Id"])
rule.apply(data)

# raises ValidationError if there are rows with the same Date and Country
rule = AssertUniqueRule(["Date", "Country"])
rule.apply(data)

Parameters:

Name Type Description Default
columns Optional[Iterable[str]]

The columns which together must be unique. Optional. When not specified, all the columns are used (ie the rows must be unique).

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 validation. Default: True

True

Raises:

Type Description
ValidationError

raised if there are duplicates. The message has the number of duplicate rows.

MissingColumnError

raised if any of the columns are missing from the input dataframe.

Note

Missing values (NA) are compared like any other value: two rows with missing values in the same columns (and the same values in the other columns) are duplicates.

Note

With the dask backend, the check computes the input dataframe.