Skip to content

Conditional rules

Conditional rules

Rules to filter rows and calculate columns based on conditions.

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

IfThenElseRule

Calculates the ouput based on a condition (If Cond is true Then use then_value Else use else_value).

Example:

1
2
3
4
5
6
7
8
Given df:
| A   | B  |
| 1   | 2  |
| 5   | 3  |
| 3   | 4  |

rule = IfThenElseRule("df['A'] > df['B']", output_column="C", then_value="A is greater", else_value="B is greater")
rule.apply(df)

Result:

1
2
3
4
| A   | B  | C            |
| 1   | 2  | B is greater |
| 5   | 3  | A is greater |
| 3   | 4  | B is greater |

Parameters:

Name Type Description Default
condition_expression str

An expression as a string. The expression must evaluate to a boolean scalar or a boolean series.

required
then_value Optional[Union[int, float, bool, str]]

The value to use if the condition is true.

None
then_column Optional[str]

Use the value from the then_column if the condition is true. One and only one of then_value and then_column can be used.

None
else_value Optional[Union[int, float, bool, str]]

The value to use if the condition is false.

None
else_column Optional[str]

Use the value from the else_column if the condition is false. One and only one of the else_value and else_column can be used.

None
output_column str

The column name of the result column which will be added to the dataframe.

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
ColumnAlreadyExistsError

raised in strict mode only if a column with the same name already exists in the dataframe.

ExpressionSyntaxError

raised if the column expression has a Python syntax error.

MissingColumnError

raised when then_column or else_column are used but they are missing from the input dataframe.

TypeError

raised if an operation is not supported between the types involved

NameError

raised if an unknown variable is used

KeyError

raised if you try to use an unknown column (i.e. df['ANY_UNKNOWN_COLUMN'])

FilterRule

Exclude rows based on a condition.

Example:

1
2
3
4
5
6
7
8
Given df:
| A   | B  |
| 1   | 2  |
| 5   | 3  |
| 3   | 4  |

rule = FilterRule("df['A'] > df['B']")
rule.apply(df)

Result:

1
2
| A   | B  |
| 5   | 3  |

Same example using discarded_matching_rows=True:

1
2
rule = FilterRule("df['A'] > df['B']", discard_matching_rows=True)
rule.apply(df)

Result:

1
2
3
| A   | B  |
| 1   | 2  |
| 3   | 4  |

Parameters:

Name Type Description Default
condition_expression str

An expression as a string. The expression must evaluate to a boolean scalar or a boolean series.

required
discard_matching_rows bool

By default the rows matching the condition (ie where the condition is True) are kept, the rest of the rows being dropped from the result. Setting this parameter to True essentially inverts the condition, so the rows matching the condition are discarded and the rest of the rows kept. Default: False.

False
named_output_discarded Optional[str]

A named output for the records being discarded if those need to be kept for further processing. Default: None, which doesn't keep track of discarded records.

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
ExpressionSyntaxError

raised if the column expression has a Python syntax error.

TypeError

raised if an operation is not supported between the types involved

NameError

raised if an unknown variable is used

KeyError

raised if you try to use an unknown column (i.e. df['ANY_UNKNOWN_COLUMN'])

FilterValuesRule

Keeps (or removes) the rows where the value of a column is one of a list of values.

Basic usage:

1
2
3
4
5
6
7
# keeps the rows where Country is UK or IE
rule = FilterValuesRule("Country", ["UK", "IE"])
rule.apply(data)

# removes the rows where Status is cancelled or deleted
rule = FilterValuesRule("Status", ["cancelled", "deleted"], exclude=True)
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

The column with the values to check.

required
values Iterable

The list of values. They should have the same type as the column.

required
exclude bool

When False (default), the rows with the values in the list are kept. When True, the rows with the values in the list are removed.

False
named_output_discarded Optional[str]

A named output for the rows which are removed, if those need to be kept for further processing. Default: None, which doesn't keep track of the removed rows.

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
MissingColumnError

raised if the input column doesn't exist in the input dataframe.

Note

The rows with missing values (NA) in the column are removed when exclude is False and kept when exclude is True (a missing value is never one of the values).