Skip to content

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Sep 25, 2025

Copy link
Collaborator

@mathause mathause left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward to this! Should the path of the node be added to the error message if the merge fails?

tree1 = xr.DataTree.from_dict({"/a/b": 1})
tree2 = xr.DataTree.from_dict({"/a/b": 2})
xr.merge([tree1, tree2])

e.g. using

def _handle_errors_with_path_context(path: str):

@veni-vidi-vici-dormivi FYI

@shoyer
Copy link
Member Author

shoyer commented Sep 26, 2025

Looking forward to this! Should the path of the node be added to the error message if the merge fails?

Great suggestion, done!

@github-actions github-actions bot added the topic-DataTree Related to the implementation of a DataTree class label Sep 26, 2025
Copy link
Collaborator

@mathause mathause left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! With this we can replace our custom datatree-merge function (which uses map_over_datasets) - I just checked and our tests still pass.

@shoyer
Copy link
Member Author

shoyer commented Oct 6, 2025

Nice! With this we can replace our custom datatree-merge function (which uses map_over_datasets) - I just checked and our tests still pass.

Thanks for checking! Let me know if you have any suggestions for further test coverage.

@shoyer shoyer added the plan to merge Final call for comments label Oct 7, 2025
Comment on lines 827 to 828
def depth(kv):
return kv[0].count("/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could use

def depth(self) -> int:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tree.level turns out to be the right property. depth refers (somewhat confusing, IMO) to the number of levels below the given node.

Comment on lines +831 to +850
# Merge datasets, including inherited indexes to ensure alignment.
datasets = [node.dataset for node in nodes]
with add_path_context_to_errors(key):
merge_result = merge_core(
datasets,
compat=compat,
join=join,
combine_attrs=combine_attrs,
)
# Remove inherited coordinates/indexes/dimensions.
for var_name in list(merge_result.coord_names):
if not any(var_name in node._coord_variables for node in nodes):
del merge_result.variables[var_name]
merge_result.coord_names.remove(var_name)
for index_name in list(merge_result.indexes):
if not any(index_name in node._node_indexes for node in nodes):
del merge_result.indexes[index_name]
for dim in list(merge_result.dims):
if not any(dim in node._node_dims for node in nodes):
del merge_result.dims[dim]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain / add comments explaining why this can't be done by just using node.to_dataset(inherit=False) then merging the resulting datasets?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a comment about this above already: Merge datasets, including inherited indexes to ensure alignment

@shoyer shoyer enabled auto-merge (squash) October 8, 2025 16:50
@shoyer shoyer merged commit 20d3773 into pydata:main Oct 8, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments topic-DataTree Related to the implementation of a DataTree class

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support DataTree in xarray.merge

3 participants