Skip to content

Add get_hierarchy_dataframe and Hierarchy.rebase - #1510

Merged
nicolasbisurgi merged 3 commits into
masterfrom
1509-hierarchy-dataframe-and-rebase
Nov 30, 2026
Merged

nicolasbisurgi merged 3 commits into
masterfrom
1509-hierarchy-dataframe-and-rebase

Conversation

@nicolasbisurgi

Copy link
Copy Markdown
Collaborator

Closes #1509

Why

The main goal is to make it easy to take a hierarchy from one TM1 model and recreate it in another, either as is or at a coarser grain.

Today we can build a hierarchy from a DataFrame with update_or_create_hierarchy_from_dataframe(), but there's no mirror operation that gives us an existing hierarchy in that same shape. ElementService.get_elements_dataframe() comes close, but its default output doesn't feed straight back in: the type column is called Type instead of ElementType, attribute columns don't carry their type suffix (so numeric attributes would be recreated as strings), and custom TM1 level names (e.g. Family, SKU) aren't recognized as parent columns.

The second need comes from planning models. A common setup is that actuals arrive at SKU level, but planning happens at product family level (e.g. 3 levels higher). The planning model needs the same hierarchy with the families as leaves, so the actuals can be loaded 1:1 from the families' consolidated cells in the source cube.

What changes

HierarchyService.get_hierarchy_dataframe(dimension_name, hierarchy_name, leaf_level=None, ...)

This is a thin wrapper around get_elements_dataframe() (no logic is duplicated) that normalizes the output, so the round trip just works:

df = tm1_source.hierarchies.get_hierarchy_dataframe("Product", "Product")
tm1_target.hierarchies.update_or_create_hierarchy_from_dataframe("Product", "Product", df)

Elements with multiple parents get one row per path, so alternate rollups survive the trip. I also moved the level-column detection out of update_or_create_hierarchy_from_dataframe() into a small shared helper (_identify_level_columns), because both functions need to read those columns the same way. Its behaviour is unchanged.

Hierarchy.rebase(leaf_level)

This cuts the hierarchy at a TM1 level (counted from the bottom, leaves are level 0). The elements on that level become numeric leaves and everything below them is removed, so a hierarchy with its top at level 8 rebased to level 3 ends up with its top at level 5. Element names are kept, and the method returns the new leaves, which is exactly the element set we'd use for the source view when moving the data.

Passing leaf_level to get_hierarchy_dataframe() applies the same rebase to the DataFrame. The decision of what to keep is made on the full hierarchy fetched from TM1 (not on the DataFrame), and attribute values, including those of the new leaves, carry over to the target.

How the cut works (and why)

The rule is built so that totals are preserved: if each new leaf is loaded with the consolidated value of the same element in the original hierarchy, every remaining consolidation adds up to the same total as before. The way to guarantee that is that no remaining consolidation ever loses a child:

  • Ragged hierarchies: an element below the cut that hangs directly under a consolidation above the cut (or has no parent) is kept as a leaf, because otherwise that branch would silently disappear from the totals.
  • Alternate rollups (e.g. a Promo consolidation under Total that also holds SKUs from the families): they're kept as leaves too, and a UserWarning lists the new leaves that share descendants. Their values overlap, but they overlap in the source in exactly the same way, so the totals still reconcile.
  • String elements aren't part of the consolidation structure, so they're left untouched.

One thing to bear in mind is that we initially considered dropping alternate rollups. Working through the numbers showed that dropping them is what would actually break reconciliation with the source, which is why they're kept and flagged instead.

Tests

  • Tests/Hierarchy_test.py: 14 new unit tests for rebase (balanced, ragged, alternate rollups, string elements, level 0 as a no-op, cutting at the top, invalid levels, case and space insensitivity, circular references). One of them consolidates sample values before and after the rebase and asserts every remaining total matches.
  • Tests/HierarchyService_test.py: 4 new integration tests (DataFrame shape, full round trip into a second dimension, rebase via leaf_level, and skip_consolidations combined with leaf_level).
  • The full Hierarchy_test.py and HierarchyService_test.py suites pass against a TM1 11.8 instance (99 tests), and the new integration tests also pass with use_blob=True. black and ruff are clean on the changed files.

Also worth flagging: renaming custom TM1 level names to levelXXX is implemented, but it hasn't been run against a dimension that actually has custom level names.

Out of scope

Moving the cube data itself. Loading the source's level N consolidated cells into the target's new leaves remains a separate step.

As a new feature, I'd suggest release:minor when merging. Happy to walk through the rebase rule or adjust anything that doesn't fit how you'd like this exposed.

🤖 Generated with Claude Code

HierarchyService.get_hierarchy_dataframe materializes a hierarchy in the
shape update_or_create_hierarchy_from_dataframe expects, so a hierarchy can
be copied to another server. It wraps ElementService.get_elements_dataframe
and normalizes the element type column, attribute type suffixes and level
column names.

Hierarchy.rebase cuts a hierarchy at a TM1 level so those elements become
the new leaves, e.g. to plan at product family instead of SKU level. Totals
are preserved: no remaining consolidation loses a child, so short branches
in ragged hierarchies and alternate rollups are kept as leaves, with a
warning when new leaves overlap. get_hierarchy_dataframe(leaf_level=...)
applies the same rebase to the DataFrame.

Closes #1509

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nicolasbisurgi nicolasbisurgi self-assigned this Nov 12, 2026
@nicolasbisurgi nicolasbisurgi added the release:minor Triggers major version bump (e.g.: 1.4.1 → 1.5.0) label Nov 12, 2026
- Restore the pandas / admin decorators on
  update_or_create_hierarchy_from_dataframe. They had moved onto
  get_hierarchy_dataframe, which made a read require admin rights and
  let non-admins start an update that failed halfway.
- Compare edge weights as numbers in
  update_or_create_hierarchy_from_dataframe. String weights such as
  '1.000000' never matched the weights in TM1, so every unchanged edge
  was deleted and added again.
- Make Hierarchy.rebase(0) a true no-op. Consolidations without
  children were turned into Numeric elements.
- Rebase before retrieving the elements in get_hierarchy_dataframe, so
  an invalid leaf_level fails early.
- Replace the row-wise DataFrame.apply in _rebase_hierarchy_dataframe
  with itertuples.

Add HierarchyService_dataframe_test.py, which covers these cases
against a mocked server and runs without TM1.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@MariusWirtz

Copy link
Copy Markdown
Collaborator

Looks good overall, and I made a small enhancement.

Will merge when the integration tests complete.

@github-actions

Copy link
Copy Markdown
Contributor

Tests completed for environment: tm1-11-cloud. Check artifacts for details.

"rebase" reads as git rebase to developers and as re-indexing to a base
year to finance users. "prune_to_level" says what happens: the hierarchy
is cut back so that the given TM1 level becomes the new leaf level.

- Rename the private helper to _prune_hierarchy_dataframe
- Update docstrings and the overlap warning; clarify that levels are
  counted from the bottom, not as depth from the top
- Rename the tests accordingly

The leaf_level parameter is unchanged. No alias for the old name, as
rebase was never released.
@github-actions

Copy link
Copy Markdown
Contributor

Tests completed for environment: tm1-11-cloud. Check artifacts for details.

@nicolasbisurgi
nicolasbisurgi merged commit 02a2541 into master Nov 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:minor Triggers major version bump (e.g.: 1.4.1 → 1.5.0)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add HierarchyService.get_hierarchy_dataframe and Hierarchy.rebase to materialize and rebase hierarchies

3 participants