Skip to content

Numeric rules

Numeric rules

Rules operating on numeric columns.

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

RoundRule

Rounds a set of columns to specified decimal places.

Basic usage:

1
2
3
4
5
6
7
# rounds Col_A to 2dps
rule = RoundRule("Col_A", 2)
rule.apply(data)

# rounds Col_B to 0dps and output the results into Col_C, Col_B remains unchanged
rule = RoundRule("Col_B", 0, output_column="Col_C")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

A column with values to round as per the specified scale.

required
scale Union[int, Sequence[int]]

An integer specifying the number of decimal places to round to.

required
output_column Optional[str]

An optional name for a new column with the rounded values. If provided, the existing column is unchanged and the new column is created with the results. 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 a 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, the overwriting of existing columns is ignored.

AbsRule

Converts numbers to absolute values.

Basic usage:

1
2
rule = AbsRule("col_A")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

The name of the column to convert to absolute values.

required
output_column Optional[str]

An optional new column with the absolute values. If provided the existing column is unchanged and a new column is created with the absolute 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 a 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, the overwriting of existing columns is ignored.

ClipRule

Limits the values of a numeric column to a range (the values outside the range are set to the bounds).

Basic usage:

1
2
3
4
5
6
7
# values below 0 become 0 and values above 100 become 100
rule = ClipRule("col_A", lower=0, upper=100)
rule.apply(data)

# values below 0 become 0, the values are written to a new column
rule = ClipRule("col_A", lower=0, output_column="col_B")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

The numeric column with the values to clip.

required
lower Optional[Union[int, float]]

The lower bound. Values below it are replaced with it. Optional.

None
upper Optional[Union[int, float]]

The upper bound. Values above it are replaced with it. Optional. At least one of lower and upper must be specified.

None
output_column Optional[str]

An optional column to hold the result. If provided, the existing column is unchanged, and a new column with this name is created. 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 validation. Default: True

True

Raises:

Type Description
MissingColumnError

raised if the input 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. Missing values (NA) are kept as missing values.