Skip to content

Fill rules

Fill rules

Rules to fill missing values (NA) with values from other rows. To fill them with a given value, see the missing values rules.

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

ForwardFillRule

Replaces NAs/missing values with the next non-NA value, optionally sorting and grouping the data.

Example:

1
2
3
4
| A   | B  |
| a   | 1  |
| b   | NA |
| a   | NA |

After a fill forward:

1
2
3
4
| A   | B  |
| a   | 1  |
| b   | 1  |
| a   | 1  |

After a fill forward with group_by=["A"]:

1
2
3
4
| A   | B  |
| a   | 1  |
| b   | NA |
| a   | 1  |

The "a" group has the first non-NA value as 1 and that is used "forward" to fill the 3rd row. The "b" group has no non-NA values, so nothing to fill.

Parameters:

Name Type Description Default
columns Iterable[str]

The list of columns to replaces NAs for. The rest of the columns in the dataframe are not affected.

required
sort_by Optional[Iterable[str]]

The list of columns to sort by before the fill operation. Optional. Given the previous non-NA values are used, sorting can make a difference in the values uses.

None
sort_ascending bool

When sort_by is specified, True means sort ascending, False sort descending.

True
group_by Optional[Iterable[str]]

The list of columns to group by before the fill operation. Optional. The fill values are only used within a group, other adjacent groups are not filled. Useful when you want to copy(fill) data at a certain group level.

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 any columns specified in either columns, sort_by or group_by are missing from the dataframe.

BackFillRule

Replaces NAs/missing values with the next non-NA value, optionally sorting and grouping the data.

Example:

1
2
3
4
| A   | B  |
| a   | NA |
| b   | 2  |
| a   | NA |

After a fill forward:

1
2
3
4
| A   | B  |
| a   | 2  |
| b   | 2  |
| a   | NA |

After a fill forward with group_by=["A"]:

1
2
3
4
| A   | B  |
| a   | NA |
| b   | 2  |
| a   | NA |

The "a" group has no non-NA value, so it is not filled. The "b" group has a non-NA value of 2 but not other NA values, so nothing to fill.

Parameters:

Name Type Description Default
columns Iterable[str]

The list of columns to replaces NAs for. The rest of the columns in the dataframe are not affected.

required
sort_by Optional[Iterable[str]]

The list of columns to sort by before the fill operation. Optional. Given the previous non-NA values are used, sorting can make a difference in the values uses.

None
sort_ascending bool

When sort_by is specified, True means sort ascending, False sort descending.

True
group_by Optional[Iterable[str]]

The list of columns to group by before the fill operation. Optional. The fill values are only used within a group, other adjacent groups are not filled. Useful when you want to copy(fill) data at a certain group level.

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 any columns specified in either columns, sort_by or group_by are missing from the dataframe.