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 ofJoinoperations 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
|
Carries |
|
Determine which fields should be removed for a given table |
|
Return |
|
Generate a sequence of joins with remove commands based on leaf edges |
|
Return the covering-tree edges (see |
|
Return the edges (pairs of table names) of a tree of joins covering |
|
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
|
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:
objectA 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_sequencejoin operations out with tables taken fromtables.- 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 theremoveattribute 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:
- Return type:
- Returns:
A list of fields to remove
- tabled.join_tables.ensure_join_op(obj)[source]#
Return
objif it is already aJoin, else wrap it asJoin(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
- 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).
- 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 intarget_subsetis 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:
- Return type:
- Returns:
An updated list of leaf edges.