Skip to content

Type conversion rules

Type conversion rules

Rules to convert the types of 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 TypeConversionRule (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.

TypeConversionRule

Converts the type of a given set of columns to other types.

Basic usage:

1
2
3
# converts column A to int64 and B to string
rule = TypeConversionRule({"A": "int64", "B": "string"})
rule.apply(data)

Parameters:

Name Type Description Default
mapper Mapping[str, str]

A dict with columns names as keys and the new types as values. The supported types are: int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64, string and boolean. To convert strings to datetime values, use StrToDateTimeRule.

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
MissingColumnError

raised when a column specified in the mapper doesn't exist in the input data frame.

UnsupportedTypeError

raised when an unknown type is speified in the values of the mapper.

ValueError

raised in strict mode if a value cannot be converted to the desired type. In non strict mode, the exception is not raised and the value is converted to NA.