tabled.join_tables#

Join multiple tables (pandas DataFrames) down to a target subset of columns.

Given a mapping of tables and the set of columns you want in the result, this module figures out which pairs of tables to join, in what order, and which overlapping fields to drop at each step, so the final result has exactly the target columns.

Main entry points:

  • Join: a join operation paired with optional fields to remove.

  • minimum_covering_tree: the minimal tree of table joins covering the target subset.

  • generate_join_sequence: the ordered sequence of Join operations to run.

  • compute_join_resolution: carries out a join sequence and returns the result.

Example

>>> tables = {
...     'A': pd.DataFrame({'b': [1, 2, 3, 33], 'c': [4, 5, 6, 66]}),
...     'B': pd.DataFrame(
...         {
...             'b': [1, 2, 3],
...             'a': [4, 5, 6],
...             'd': [7, 8, 9],
...             'e': [10, 11, 12],
...             'f': [13, 14, 15],
...         }
...     ),
...     'C': pd.DataFrame({'f': [13, 14, 15], 'g': [4, 5, 6]}),
...     'D': pd.DataFrame(
...         {'d': [7, 8, 77], 'e': [10, 11, 77], 'h': [7, 8, 9], 'i': [1, 2, 3]}
...     ),
...     'E': pd.DataFrame({'i': [1, 2, 3], 'j': [4, 5, 6]}),
... }
>>> target_sub_set = {'b', 'g', 'j'}
>>> leaf_edges = get_leaf_edges(tables, target_sub_set)
>>> leaf_edges
[('B', 'C'), ('D', 'E')]
>>> join_sequence = generate_join_sequence(tables, leaf_edges, target_sub_set)
>>> join_sequence
['B', Join('C', remove=['a', 'f']), Join('D', remove=['d', 'e', 'h']), Join('E', remove=['i'])]
>>> join_result = compute_join_resolution(join_sequence, tables)
>>> join_result
   b  g  j
0  1  4  4
1  2  5  5

Functions

compute_join_resolution(resolution_sequence, ...)

Carries resolution_sequence join operations out with tables taken from tables.

determine_remove_fields(labeled_sets, ...)

Determine which fields should be removed for a given table

ensure_join_op(obj)

Return obj if it is already a Join, else wrap it as Join(obj) (no fields removed).

generate_join_sequence(tables, leaf_edges, ...)

Generate a sequence of joins with remove commands based on leaf edges

get_leaf_edges(tables, target_subset[, ...])

Return the covering-tree edges (see minimum_covering_tree) whose second table is a leaf (visited once).

minimum_covering_tree(tables, target_subset)

Return the edges (pairs of table names) of a tree of joins covering target_subset.

update_leaf_edges_after_removal(tables, ...)

Update the list of leaf edges after removing an edge, ensuring that the resulting leaf edges do not lead to the loss of any elements in the target subset.

Classes

Join(table_id[, remove])

A join step: which table to join in next, and which of its fields to drop after.

class tabled.join_tables.Join(table_id, remove=None)[source]#

Bases: object

A join step: which table to join in next, and which of its fields to drop after.

tabled.join_tables.compute_join_resolution(resolution_sequence, tables)[source]#

Carries resolution_sequence join operations out with tables taken from tables.

Parameters:
  • resolution_sequence (Iterable) – An iterable of join operations to carry out. Each join operation is either a table name (str) or a Join object. If it’s a Join object, it’s assumed that the table has already been joined and the fields to remove are in the remove attribute of the object.

  • tables (Mapping[str, DataFrame]) – A mapping of table names to tables (pd.DataFrame)

Return type:

DataFrame

tabled.join_tables.determine_remove_fields(labeled_sets, target_sub_set, joined_tables, current_table)[source]#

Determine which fields should be removed for a given table

Parameters:
  • labeled_sets (dict[str, set[str]]) – The sets of elements labeled by nodes

  • target_sub_set (set[str]) – The target subset of elements that must remain covered

  • joined_tables (set[str]) – The set of tables that have been or will be joined

  • current_table (str) – The current table being processed

Return type:

list[str]

Returns:

A list of fields to remove

tabled.join_tables.ensure_join_op(obj)[source]#

Return obj if it is already a Join, else wrap it as Join(obj) (no fields removed).

tabled.join_tables.generate_join_sequence(tables, leaf_edges, target_sub_set)[source]#

Generate a sequence of joins with remove commands based on leaf edges

Parameters:
  • leaf_edges (list[tuple[str, str]]) – The list of leaf edges to process

  • labeled_sets – The sets of elements labeled by nodes

  • target_sub_set (set[str]) – The target subset of elements that must remain covered

Return type:

list[Join]

Returns:

A list of Join operations

tabled.join_tables.get_leaf_edges(tables, target_subset, start_node=None)[source]#

Return the covering-tree edges (see minimum_covering_tree) whose second table is a leaf (visited once).

Return type:

list[tuple[KT, KT]]

tabled.join_tables.minimum_covering_tree(tables, target_subset, start_node=None)[source]#

Return the edges (pairs of table names) of a tree of joins covering target_subset.

Walks the tables’ column-overlap graph breadth-first from start_node (or an arbitrary table if not given), accumulating edges until every column in target_subset is covered by the tables seen so far.

tabled.join_tables.update_leaf_edges_after_removal(tables, target_sub_set, current_leaf_edges)[source]#

Update the list of leaf edges after removing an edge, ensuring that the resulting leaf edges do not lead to the loss of any elements in the target subset.

Parameters:
  • tables (Mapping[str, DataFrame]) – A mapping of table names to tables (pd.DataFrame).

  • target_sub_set (set[str]) – The target subset of columns that must remain covered.

  • current_leaf_edges (list[tuple[str, str]]) – The current list of leaf edges.

Return type:

list[tuple[str, str]]

Returns:

An updated list of leaf edges.