atoti.hierarchies module

class atoti.hierarchies.Hierarchies

Manage the hierarchies.

Example

>>> prices_df = pd.DataFrame(
...     columns=["Nation", "City", "Color", "Price"],
...     data=[
...         ("France", "Paris", "red", 20.0),
...         ("France", "Lyon", "blue", 15.0),
...         ("France", "Toulouse", "green", 10.0),
...         ("UK", "London", "red", 20.0),
...         ("UK", "Manchester", "blue", 15.0),
...     ],
... )
>>> table = session.read_pandas(prices_df, table_name="Prices")
>>> cube = session.create_cube(table, mode="manual")
>>> h = cube.hierarchies
>>> h["Nation"] = {"Nation": table["Nation"]}
>>> list(h)
[('Prices', 'Nation')]

A hierarchy can be renamed by creating a new one with the same levels and then removing the old one.

>>> h["Country"] = h["Nation"].levels
>>> del h["Nation"]
>>> list(h)
[('Prices', 'Country')]

The update() method is overridden to batch hierarchy creation operations for improved performance:

>>> h.update(
...     {
...         ("Geography", "Geography"): [table["Nation"], table["City"]],
...         "Color": {"Color": table["Color"]},
...     }
... )
>>> list(h)
[('Prices', 'Color'), ('Geography', 'Geography'), ('Prices', 'Country')]
update(__m: Mapping[HierarchyKey, _HierarchyDescription], **kwargs: _HierarchyDescription) None
update(__m: Iterable[Tuple[HierarchyKey, _HierarchyDescription]], **kwargs: _HierarchyDescription) None
update(**kwargs: _HierarchyDescription) None

This method batches the updates for improved performance.