New column rules
New column rules¶
Rules to add new 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 AddNewColumnRule (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.
AddNewColumnRule ¶
Adds a new column and sets it to the value of an evaluated expression.
Example:
1 2 3 4 5 | |
AddNewColumnRule("Sum", "df['A'] + df['B']").apply(df)
Result:
1 2 3 4 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_column
|
str
|
The name of the new column to be added. |
required |
column_expression
|
str
|
An expression that gets evaluated and produces the value for the new column. The syntax: df["EXISTING_COL"] can be used in the expression to refer to other columns in the dataframe. |
required |
column_type
|
Optional[str]
|
An optional type to convert the result to. If not specified, the type is determined from the output of the expression, which can sometimes differ based on the backend. If the input dataframe is empty, this type ensures the column will be of the specified type, rather than default to string type. |
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 |
|---|---|
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. |
UnsupportedTypeError
|
raised if the column_type parameter is specified and not supported. |
TypeError
|
raised if an operation is not supported between the types involved. Also raised when the column type is specified but the conversion to that type fails. With the dask backend, which evaluates lazily, the conversion error is raised when the result is computed. |
NameError
|
raised if an unknown variable is used |
KeyError
|
raised if you try to use an unknown column (i.e. df['ANY_UNKNOWN_COLUMN']) |
Note
The implementation will try to use dataframe operations for performance, but when those are not supported it will fallback to row level operations.
Note
NA are treated slightly differently between dataframe level operations and row level. At dataframe level operations, NAs in operations will make the result be NA. In row level operations, NAs will generally raise a TypeError. To avoid such behavior, fill the NAs before performing operations.
AddRowNumbersRule ¶
Adds a new column with row numbers.
Example:
1 2 3 4 5 | |
AddRowNumbersRule("Row_Number").apply(df)
Result:
1 2 3 4 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_column
|
str
|
The name of the new column to be added. |
required |
start
|
int
|
The value to start the numbers from. Defaults to 0. |
0
|
step
|
int
|
The increment to be used between row numbers. Defaults to 1. |
1
|
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. |