Skip to main content
Version: Python

partition_by

partition_by partitions a table into constituent tables (also called subtables) via one or more key columns. The resultant object is called a partitioned table. A partitioned table is a table with a column containing other Deephaven tables, plus additional key columns that are used to index and access particular constituent tables. All constituent tables of a single partitioned table must have the same schema.

Syntax

table.partition_by(by: Union[str, list[str]], drop_keys: bool = False) -> PartitionedTable

Parameters

ParameterTypeDescription
byUnion[str, list[str]]

The column(s) by which to group data.

drop_keys optionalbool

Whether to drop key columns in the constituent tables. Default is False.

Returns

A PartitionedTable containing a subtable for each group.

Examples

The following example partitions a table into subtables using a single key column. After creating the partitioned table, keys is used to generate a table showing all of the unique keys in partitioned_table. Constituent tables are then grabbed by index with constituent_tables.

from deephaven import new_table
from deephaven.column import string_col, int_col

source = new_table(
[
string_col("X", ["A", "B", "A", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "N", "O", "N", "P", "M", "O", "P", "M"]),
int_col("Number", [55, 76, 20, 130, 230, 50, 73, 137, 214]),
]
)

partitioned_table = source.partition_by(by="X")

partition_keys = partitioned_table.keys()

const_table1 = partitioned_table.constituent_tables[0]
const_table2 = partitioned_table.constituent_tables[1]
const_table3 = partitioned_table.constituent_tables[2]

The following example partitions the same source table by two key columns. As a result, the keys that define partitioned_table are unique combinations of values in the X and Y columns. After source is partitioned into subtables, get_constituent is used to grab a constituent table based on its key.

from deephaven import new_table
from deephaven.column import string_col, int_col

source = new_table(
[
string_col("X", ["A", "B", "A", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "N", "O", "N", "P", "M", "O", "P", "M"]),
int_col("Number", [55, 76, 20, 130, 230, 50, 73, 137, 214]),
]
)

partitioned_table = source.partition_by(by=["X", "Y"])

result_a_m = partitioned_table.get_constituent(key_values=["A", "M"])