Skip to content

Show inherited indexes separately in DataTree repr - #11561

Open
mokashang wants to merge 1 commit into
pydata:mainfrom
mokashang:fix/datatree-repr-indexes
Open

Show inherited indexes separately in DataTree repr#11561
mokashang wants to merge 1 commit into
pydata:mainfrom
mokashang:fix/datatree-repr-indexes

Conversation

@mokashang

Copy link
Copy Markdown
Contributor

Description

DataTree already distinguishes Coordinates: (defined at this node)
from Inherited coordinates: (inherited from an ancestor), but its
text and HTML reprs lump every index into a single Indexes: section.
That makes it easy to mistake an inherited index for one added at the
current node, and there was a TODO in _datatree_node_repr
calling this out.

Fix: split the indexes the same way coordinates are split.

  • Indexes: shows only indexes whose coord names come from
    node._node_indexes.
  • Inherited indexes: shows only indexes whose coord names all come
    from inherited_vars(node._indexes). Like Inherited coordinates:,
    it is only rendered when root=True, so the whole-tree repr still
    attributes each inherited index to the ancestor that defines it and
    does not repeat it on every descendant.

Both the text repr (_datatree_node_repr in formatting.py) and
the HTML repr (_datatree_node_sections in formatting_html.py)
use the same split helper, _split_node_indexes, which walks
_get_indexes_dict(node.xindexes) once and buckets each (names, Index) pair by whether every name in the group is inherited. That
preserves the existing grouping behaviour for indexes that span
multiple coordinates (e.g. PandasMultiIndex).

filter_nondefault_indexes is applied independently to each bucket,
so the display_default_indexes option keeps its current meaning.
The HTML displayed_line_count accounting is updated to include the
new section.

Before, for a tree where the child inherits a non-default RangeIndex
from its parent, the child's repr looked like this:

<xarray.DataTree 'child'>
Group: /child
    Dimensions:  (x: 4)
    Inherited coordinates:
      * x        (x) float64 32B 0 1 2 3
    Data variables:
        data     (x) float64 32B 10.0 20.0 30.0 40.0

After:

<xarray.DataTree 'child'>
Group: /child
    Dimensions:  (x: 4)
    Inherited coordinates:
      * x        (x) float64 32B 0 1 2 3
    Data variables:
        data     (x) float64 32B 10.0 20.0 30.0 40.0
    Inherited indexes:
        x        RangeIndex (start=0, stop=4, step=1)

Checklist

DataTree already distinguishes ``Coordinates:`` (defined at this node)
from ``Inherited coordinates:`` (inherited from an ancestor), but its
text and HTML reprs lump every index into a single ``Indexes:`` section.
That makes it easy to mistake an inherited index for one added at the
current node, and the TODO in ``_datatree_node_repr`` called this out.

Split the indexes the same way coordinates are split:

* ``Indexes:`` shows only indexes whose coord names come from
  ``node._node_indexes``.
* ``Inherited indexes:`` shows only indexes whose coord names all come
  from ``inherited_vars(node._indexes)``. Like ``Inherited coordinates:``
  it is only rendered when ``root=True``, so the whole-tree repr still
  attributes each inherited index to the ancestor that defines it and
  does not repeat it on every descendant.

The split is done by ``_split_node_indexes``, which walks
``_get_indexes_dict(node.xindexes)`` once and buckets each ``(names,
Index)`` pair by whether every name in the group is inherited. That
preserves the existing grouping behavior for indexes that span multiple
coordinates (e.g. ``PandasMultiIndex``).

``filter_nondefault_indexes`` runs on each bucket independently so the
``display_default_indexes`` option keeps its current meaning. The HTML
``_datatree_node_sections`` line-count accounting is updated to match
the new section.

Fixes pydata#10578
@welcome

welcome Bot commented Sep 4, 2026

Copy link
Copy Markdown

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Indexes not displayed in DataTree repr

1 participant