Skip to content

Join rules

Join rules

Database-style joins of two dataframes, and semi/anti joins which keep or remove the rows of a dataframe with a match in another dataframe.

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

LeftJoinRule

Performs a database-style left join operation on two data frames.

A join involves two data frames left_df right_df with the result performing a database style join or a merge of the two, with the resulting columns coming from both dataframes. For example, if the left dataframe has two columns A, B and the right dataframe has two column A, C, and assuming A is the key column the result will have three columns A, B, C. The rows that have the same value in the key column A will be merged on the same row in the result dataframe.

A left join specifies that all the rows in the left dataframe will be present in the result, irrespective of whether there's a corresponding row with the same values in the key columns in the right dataframe. The right columns will be populated with NaNs/None when there is no corresponding row on the right.

Example:

left dataframe:

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

right dataframe:

1
2
3
| A  | C  |
| 1  | c  |
| 3  | d  |

result (key columns=["A"]):

1
2
3
| A  | B  | C  |
| 1  | a  | c  |
| 2  | b  | NA |

Parameters:

Name Type Description Default
named_input_left Optional[str]

Which dataframe to use as the input on the left side of the join. When set to None, the input is taken from the main output of the previous rule. Set it to a string value, the name of an output dataframe of a previous rule.

required
named_input_right Optional[str]

Which dataframe to use as the input on the right side of the join. When set to None, the input is taken from the main output of the previous rule. Set it to a string value, the name of an output dataframe of a previous rule.

required
key_columns_left Iterable[str]

A list or tuple of column names to join on (columns in the left data frame)

required
key_columns_right Optional[Iterable[str]]

A list or tuple of column names to join on (columns in the right data frame). If not set or set to None, the key_columns_left is used on the right dataframe too.

None
suffixes Iterable[Optional[str]]

A list or tuple of two values which will be set as suffixes for the columns in the result data frame for those columns that have the same name (and are not key columns).

(None, '_r')
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 (keys) are missing from any of the two input data frames.

InnerJoinRule

Performs a database-style inner join operation on two data frames.

A join involves two data frames left_df right_df with the result performing a database style join or a merge of the two, with the resulting columns coming from both dataframes. For example, if the left dataframe has two columns A, B and the right dataframe has two column A, C, and assuming A is the key column the result will have three columns A, B, C. The rows that have the same value in the key column A will be merged on the same row in the result dataframe.

An inner join specifies that only those rows that have key values in both left and right will be copied over and merged into the result data frame. Any rows without corresponding values on the other side (be it left or right) will be dropped from the result.

Example:

left dataframe:

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

right dataframe:

1
2
3
| A  | C  |
| 1  | c  |
| 3  | d  |

result (key columns=["A"]):

1
2
| A  | B  | C  |
| 1  | a  | c  |

Parameters:

Name Type Description Default
named_input_left Optional[str]

Which dataframe to use as the input on the left side of the join. When set to None, the input is taken from the main output of the previous rule. Set it to a string value, the name of an output dataframe of a previous rule.

required
named_input_right Optional[str]

Which dataframe to use as the input on the right side of the join. When set to None, the input is taken from the main output of the previous rule. Set it to a string value, the name of an output dataframe of a previous rule.

required
key_columns_left Iterable[str]

A list or tuple of column names to join on (columns in the left data frame)

required
key_columns_right Optional[Iterable[str]]

A list or tuple of column names to join on (columns in the right data frame). If not set or set to None, the key_columns_left is used on the right dataframe too.

None
suffixes Iterable[Optional[str]]

A list or tuple of two values which will be set as suffixes for the columns in the result data frame for those columns that have the same name (and are not key columns).

(None, '_r')
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 (keys) are missing from any of the two input data frames.

OuterJoinRule

Performs a database-style left join operation on two data frames.

A join involves two data frames left_df right_df with the result performing a database style join or a merge of the two, with the resulting columns coming from both dataframes. For example, if the left dataframe has two columns A, B and the right dataframe has two column A, C, and assuming A is the key column the result will have three columns A, B, C. The rows that have the same value in the key column A will be merged on the same row in the result dataframe.

An outer join specifies that all the rows in the both left and right dataframes will be present in the result, irrespective of whether there's a corresponding row with the same values in the key columns in the other dataframe. The missing side will have its columns populated with NA when the rows are missing.

Example:

left dataframe:

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

right dataframe:

1
2
3
| A  | C  |
| 1  | c  |
| 3  | d  |

result (key columns=["A"]):

1
2
3
4
| A  | B  | C  |
| 1  | a  | c  |
| 2  | b  | NA |
| 3  | NA | d  |

Parameters:

Name Type Description Default
named_input_left Optional[str]

Which dataframe to use as the input on the left side of the join. When set to None, the input is taken from the main output of the previous rule. Set it to a string value, the name of an output dataframe of a previous rule.

required
named_input_right Optional[str]

Which dataframe to use as the input on the right side of the join. When set to None, the input is taken from the main output of the previous rule. Set it to a string value, the name of an output dataframe of a previous rule.

required
key_columns_left Iterable[str]

A list or tuple of column names to join on (columns in the left data frame)

required
key_columns_right Optional[Iterable[str]]

A list or tuple of column names to join on (columns in the right data frame). If not set or set to None, the key_columns_left is used on the right dataframe too.

None
suffixes Iterable[Optional[str]]

A list or tuple of two values which will be set as suffixes for the columns in the result data frame for those columns that have the same name (and are not key columns).

(None, '_r')
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 (keys) are missing from any of the two input data frames.

RightJoinRule

Performs a database-style left join operation on two data frames.

A join involves two data frames left_df right_df with the result performing a database style join or a merge of the two, with the resulting columns coming from both dataframes. For example, if the left dataframe has two columns A, B and the right dataframe has two column A, C, and assuming A is the key column the result will have three columns A, B, C. The rows that have the same value in the key column A will be merged on the same row in the result dataframe.

A right join specifies that all the rows in the right dataframe will be present in the result, irrespective of whether there's a corresponding row with the same values in the key columns in the left dataframe. The left columns will be populated with NA when there is no corresponding row on the left.

Example:

left dataframe:

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

right dataframe:

1
2
3
| A  | C  |
| 1  | c  |
| 3  | d  |

result (key columns=["A"]):

1
2
3
| A  | B  | C  |
| 1  | a  | c  |
| 3  | NA | d  |
Note

A right join is equivalent to a left join with the dataframes inverted, ie: left_df right_df is equivalent to right_df left_df although the order of the rows will be different.

Parameters:

Name Type Description Default
named_input_left Optional[str]

Which dataframe to use as the input on the left side of the join. When set to None, the input is taken from the main output of the previous rule. Set it to a string value, the name of an output dataframe of a previous rule.

required
named_input_right Optional[str]

Which dataframe to use as the input on the right side of the join. When set to None, the input is taken from the main output of the previous rule. Set it to a string value, the name of an output dataframe of a previous rule.

required
key_columns_left Iterable[str]

A list or tuple of column names to join on (columns in the left data frame)

required
key_columns_right Optional[Iterable[str]]

A list or tuple of column names to join on (columns in the right data frame). If not set or set to None, the key_columns_left is used on the right dataframe too.

None
suffixes Iterable[Optional[str]]

A list or tuple of two values which will be set as suffixes for the columns in the result data frame for those columns that have the same name (and are not key columns).

(None, '_r')
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 (keys) are missing from any of the two input data frames.

SemiJoinRule

Keeps the rows of the left dataframe which have a match in the right dataframe.

Unlike an inner join, the result has only the columns of the left dataframe and each left row appears at most once, even when it matches multiple rows in the right dataframe. The order of the left rows is kept.

Example:

left dataframe:

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

right dataframe:

1
2
3
4
| A  | C  |
| 1  | x  |
| 1  | y  |
| 3  | z  |

result (key columns=["A"]):

1
2
3
| A  | B  |
| 1  | a  |
| 3  | c  |

Parameters:

Name Type Description Default
named_input_left Optional[str]

Which dataframe to use as the left input (the rows to keep or remove). When set to None, the input is taken from the main output of the previous rule.

required
named_input_right Optional[str]

Which dataframe to use as the right input (the rows to match against). When set to None, the input is taken from the main output of the previous rule.

required
key_columns_left Iterable[str]

A list or tuple of column names to match on (columns in the left data frame)

required
key_columns_right Optional[Iterable[str]]

A list or tuple of column names to match on (columns in the right data frame). If not set or set to None, the key_columns_left is used on the right dataframe too.

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 any columns (keys) are missing from any of the two input data frames.

Note

Like in SQL, missing values (NA) in the key columns don't match anything.

AntiJoinRule

Keeps the rows of the left dataframe which don't have a match in the right dataframe.

This is useful, for example, to find the new records which have not been loaded yet. The result has only the columns of the left dataframe and the order of the left rows is kept.

Example:

left dataframe:

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

right dataframe:

1
2
3
| A  | C  |
| 1  | x  |
| 3  | z  |

result (key columns=["A"]):

1
2
| A  | B  |
| 2  | b  |

Parameters:

Name Type Description Default
named_input_left Optional[str]

Which dataframe to use as the left input (the rows to keep or remove). When set to None, the input is taken from the main output of the previous rule.

required
named_input_right Optional[str]

Which dataframe to use as the right input (the rows to match against). When set to None, the input is taken from the main output of the previous rule.

required
key_columns_left Iterable[str]

A list or tuple of column names to match on (columns in the left data frame)

required
key_columns_right Optional[Iterable[str]]

A list or tuple of column names to match on (columns in the right data frame). If not set or set to None, the key_columns_left is used on the right dataframe too.

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 any columns (keys) are missing from any of the two input data frames.

Note

Like in SQL, missing values (NA) in the key columns don't match anything, so the left rows with missing values in the key columns are kept.