Skip to content

Commit 8bd456c

Browse files
Make illegal path-like variable names when constructing a DataTree from a Dataset (#9378)
* Make illegal path-like variable names when constructing a DataTree from a Dataset * Updated whats-new.rst * PR comments * Revert diff * Update xarray/core/datatree.py Co-authored-by: Tom Nicholas <[email protected]> * Update xarray/core/datatree.py Co-authored-by: Tom Nicholas <[email protected]> * Update xarray/tests/test_datatree.py Co-authored-by: Tom Nicholas <[email protected]> * Update expected Exception message in test * Merge changes from #9476 * Fix --------- Co-authored-by: Tom Nicholas <[email protected]>
1 parent 4323b19 commit 8bd456c

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ Breaking changes
100100
Bug fixes
101101
~~~~~~~~~
102102

103+
- Make illegal path-like variable names when constructing a DataTree from a Dataset
104+
(:issue:`9339`, :pull:`9378`)
105+
By `Etienne Schalk <https://github.com/etienneschalk>`_.
103106
- Fix bug with rechunking to a frequency when some periods contain no data (:issue:`9360`).
104107
By `Deepak Cherian <https://github.com/dcherian>`_.
105108
- Fix bug causing `DataTree.from_dict` to be sensitive to insertion order (:issue:`9276`, :pull:`9292`).

xarray/core/datatree.py

+13
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ def check_alignment(
156156
check_alignment(child_path, child_ds, base_ds, child.children)
157157

158158

159+
def _check_for_slashes_in_names(variables: Iterable[Hashable]) -> None:
160+
offending_variable_names = [
161+
name for name in variables if isinstance(name, str) and "/" in name
162+
]
163+
if len(offending_variable_names) > 0:
164+
raise ValueError(
165+
"Given variables have names containing the '/' character: "
166+
f"{offending_variable_names}. "
167+
"Variables stored in DataTree objects cannot have names containing '/' characters, as this would make path-like access to variables ambiguous."
168+
)
169+
170+
159171
class DatasetView(Dataset):
160172
"""
161173
An immutable Dataset-like view onto the data in a single DataTree node.
@@ -453,6 +465,7 @@ def __init__(
453465
super().__init__(name=name, children=children)
454466

455467
def _set_node_data(self, dataset: Dataset):
468+
_check_for_slashes_in_names(dataset.variables)
456469
data_vars, coord_vars = _collect_data_and_coord_variables(dataset)
457470
self._data_variables = data_vars
458471
self._node_coord_variables = coord_vars

xarray/tests/test_datatree.py

+17
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ def test_child_gets_named_on_attach(self):
8383
mary = DataTree(children={"Sue": sue}) # noqa
8484
assert mary.children["Sue"].name == "Sue"
8585

86+
def test_dataset_containing_slashes(self):
87+
xda: xr.DataArray = xr.DataArray(
88+
[[1, 2]],
89+
coords={"label": ["a"], "R30m/y": [30, 60]},
90+
)
91+
xds: xr.Dataset = xr.Dataset({"group/subgroup/my_variable": xda})
92+
with pytest.raises(
93+
ValueError,
94+
match=re.escape(
95+
"Given variables have names containing the '/' character: "
96+
"['R30m/y', 'group/subgroup/my_variable']. "
97+
"Variables stored in DataTree objects cannot have names containing '/' characters, "
98+
"as this would make path-like access to variables ambiguous."
99+
),
100+
):
101+
DataTree(xds)
102+
86103

87104
class TestPaths:
88105
def test_path_property(self):

0 commit comments

Comments
 (0)