Skip to content

String rules

String rules

Rules operating on string 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 StrLowerRule (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.

StrLowerRule

Converts the values in a string column to lower case.

Basic usage:

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

Parameters:

Name Type Description Default
input_column str

A string column to convert to lower case.

required
output_column Optional[str]

An optional new names for the column with the lower case values. If provided, the existing column is unchanged, and a new column is created with the lower case 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.

StrUpperRule

Converts the values in a string columns to upper case.

Basic usage:

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

Parameters:

Name Type Description Default
input_column str

A string column to convert to upper case.

required
output_column Optional[str]

An optional new names for the column with the upper case values. If provided, the existing column is unchanged, and a new column is created with the upper case 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.

StrCapitalizeRule

Converts the values in a string column to capitalized values.

Capitalization will convert the first letter in the string to upper case and the rest of the letters to lower case.

Basic usage:

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

Parameters:

Name Type Description Default
input_column str

A string column with the values to capitalize.

required
output_column Optional[str]

An optional new names for the column with the capitalized values. If provided, the existing column is unchanged, and a new column is created with the capitalized 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.

StrSplitRule

Splits a string into an array of substrings based on a string separator.

Note

The output is an array of substrings which can optionally be limited via the limit parameter to only include the first number of substrings. If you need the output to be a string, perhaps joined on a different separator and optionally sorted then use the StrSplitRejoinRule rule.

Basic usage:

1
2
3
4
# splits the col_A column on ,
# "a,b;c,d" will be split as ["a", "b;c", "d"]
rule = StrSplitRule("col_A", separator=",")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

A string column to split.

required
separator str

A literal value to split the string by.

required
limit Optional[int]

A limit to the number of substrings. If specified, only the first substrings are returned plus an additional remainder. At most, limit + 1 substrings are returned with the last beind the remainder.

None
output_column Optional[str]

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

StrSplitRejoinRule

Splits the values in a string column into an array of substrings based on a string separator then rejoin with a new separator, optionally sorting the substrings.

Note

The output is an array of substrings which can optionally be limited via the limit parameter to only include the first number of substrings.

Basic usage:

1
2
3
4
# splits the col_A column on ,
# "b,d;a,c" will be split and rejoined as "b|c|d;a"
rule = StrSplitRejoinRule("col_A", separator=",", new_separator="|", sort="ascending")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

The column to split and rejoin.

required
separator str

A literal value to split the string by.

required
limit Optional[int]

A limit to the number of substrings. If specified, only the first substrings are returned plus an additional remainder. At most, limit + 1 substrings are returned with the last beind the remainder.

None
new_separator str

A new separator used to rejoin the substrings.

','
sort Optional[Literal[SORT_ASCENDING, SORT_DESCENDING]]

Optionally sorts the substrings before rejoining using the new_separator. It can be set to either ascending or descending, sorting the substrings accordingly. When the value is set to None, there is no sorting.

None
output_column Optional[str]

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

StrStripRule

Strips leading, trailing or both whitespaces or other characters from the values in the input column.

Basic usage:

1
2
rule = StrStripRule("col_A", how="both")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

A input column to strip characters from its values.

required
how Literal[STRIP_LEFT, STRIP_RIGHT, STRIP_BOTH]

How should the stripping be done. One of left, right, both. Left strips leading characters, right trailing characters and both at both ends.

STRIP_BOTH
characters Optional[str]

If set, it contains a list of characters to be stripped. When not specified or when set to None, whitespace is removed.

None
output_column Optional[str]

An optional new column to hold the results. If provided, the existing column is unchanged and a 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.

StrPadRule

Makes strings of a given width (justifies) by padding left or right with a fill character.

Basic usage:

1
2
3
# a value of ABCD will ABCD....
rule = StrPadRule("col_A", width=8, fill_character=".", how="right")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

A string column to be padded.

required
width int

Pad with the fill_character to this width.

required
fill_character str

Character to fill with. Defaults to whitespace.

required
how Literal[PAD_LEFT, PAD_RIGHT]

How should the stripping be done. One of left or right. Left pads at the beggining of the string, right pads at the end. Default: left.

PAD_LEFT
output_column Optional[str]

An optional new column with the padded results. If provided, the existing column is unchanged and a 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.

StrExtractRule

Extract substrings from strings columns using regular expressions.

Basic usage:

1
2
3
4
5
6
7
8
9
# extracts the number between start_ and _end
# ie: for an input value of start_1234_end - will extract 1234 in col_A
rule = StrExtractRule("col_A", regular_expression=r"start_([\d]*)_end")
rule.apply(data)

# extracts with multiple groups, extracting the single digit at the end as well
# for an input value of start_1234_end_9, col_1 will extract 1234, col_2 will extract 9
rule = StrExtractRule("col_A", regular_expression=r"start_([\d]*)_end_([\d])", output_columns=["col_1", "col_2"])
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

A column to extract data from.

required
regular_expression str

The regular expression used to extract data. The regular expression must have 1 or more groups - ie sections between brackets. The groups do the actual extraction of data. If there is a single group, then the column can be modified in place (ie no output_columns are needed) but if there are multiple groups, then output_columns must be specified as each group will be extracted in a new output column.

required
keep_original_value bool

Only used in case there isn't a match and it specifies if NA should be used in the output or the original value. Defaults: True. If the regular expression has multiple groups and therefore multiple output_columns, only the first output column will keep the original value, the rest will be populated with NA.

False
output_columns Optional[Iterable[str]]

A list of new names for the result columns. Optional. If provided, it must have one output_column per regular expression group. For example, given the regular expression "a_([\d])_([\d])" with 2 groups, then the output columns must have 2 columns (one per group) - for example ["out_1", "out_2"]. The existing columns are unchanged, and new columns are created with extracted values. If not provided, the result is updated in place (only possible if the regular expression has a single group).

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 the input_column doesn't exist in the input dataframe.

ColumnAlreadyExistsError

raised in strict mode only if an output_column already exists in the dataframe.

ValueError

raised if output_columns is provided and not the same length as the number of groups in the regular expression.

Note

In non-strict mode, the overwriting of existing columns is ignored.

StrSliceRule

Extracts a substring from each value of a string column, using python's slice semantics: value[start:end].

Basic usage:

1
2
3
4
5
6
7
# the first 3 characters
rule = StrSliceRule("col_A", 0, 3)
rule.apply(data)

# the last 4 characters, written to a new column
rule = StrSliceRule("col_A", -4, output_column="col_B")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

The string column with the values to slice.

required
start int

The position of the first character to keep (0 is the first character). Negative values count from the end of the string (-1 is the last character).

required
end Optional[int]

The position after the last character to keep. Optional. Negative values count from the end of the string. When not specified, the substring goes to the end.

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.

StrLengthRule

Calculates the length (the number of characters) of the values in a string column.

Basic usage:

1
2
3
# the length of the values in col_A, in a new column
rule = StrLengthRule("col_A", output_column="col_A_length")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

The string column.

required
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. The length of a missing value (NA) is NA.

StrContainsRule

Checks if the values in a string column contain, start with or end with a given string.

The result is a boolean column (True/False, NA for missing values). Use it, for example, with a FilterRule to keep only the rows that match.

Basic usage:

1
2
3
4
5
6
7
# Is_Test is True for the rows where Email contains "test"
rule = StrContainsRule("Email", "test", output_column="Is_Test")
rule.apply(data)

# Is_UK is True for the rows where Phone starts with "+44"
rule = StrContainsRule("Phone", "+44", how="startswith", output_column="Is_UK")
rule.apply(data)

Parameters:

Name Type Description Default
input_column str

The string column with the values to check.

required
value str

The string to look for. It's a literal string (not a regular expression) and the check is case sensitive.

required
how str

One of: contains: True if the value contains the string anywhere (default) startswith: True if the value starts with the string endswith: True if the value ends with the string

'contains'
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.