Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly improve DataTree repr #9064

Merged
merged 8 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ def diff_datatree_repr(a: DataTree, b: DataTree, compat):

def _single_node_repr(node: DataTree) -> str:
"""Information about this node, not including its relationships to other nodes."""
node_info = f"DataTree('{node.name}')"
node_info = f"DataTree(name={node.name!r})"
shoyer marked this conversation as resolved.
Show resolved Hide resolved

if node.has_data or node.has_attrs:
ds_info = "\n" + repr(node.ds)
Expand Down Expand Up @@ -1053,8 +1053,8 @@ def datatree_repr(dt: DataTree):

# Tack on info about whether or not root node has a parent at the start
first_line = lines[0]
parent = f'"{dt.parent.name}"' if dt.parent is not None else "None"
first_line_with_parent = first_line[:-1] + f", parent={parent})"
parent = dt.parent.name if dt.parent is not None else None
first_line_with_parent = first_line[:-1] + f", parent={parent!r})"
lines[0] = first_line_with_parent

return "\n".join(lines)
Expand Down
22 changes: 22 additions & 0 deletions xarray/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,28 @@ def test_operation_with_attrs_but_no_data(self):
dt.sel(dim_0=0)


class TestRepr:
def test_repr(self):
dt: DataTree = DataTree(
data=xr.Dataset({"a": 1}),
children={"b": DataTree(), "c": DataTree(xr.Dataset({"d": 2}))},
)
result = repr(dt)
expected = dedent(
"""\
DataTree(name=None, parent=None)
│ Dimensions: ()
│ Data variables:
│ a int64 8B 1
├── DataTree(name='b')
└── DataTree(name='c')
Dimensions: ()
Data variables:
d int64 8B 2"""
)
assert result == expected


class TestRestructuring:
def test_drop_nodes(self):
sue = DataTree.from_dict({"Mary": None, "Kate": None, "Ashley": None})
Expand Down
Loading