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

Make illegal path-like variable names when constructing a DataTree from a Dataset #9378

3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Deprecations
Bug fixes
~~~~~~~~~

- Make illegal path-like variable names when constructing a DataTree from a Dataset
(:issue:`9339`, :pull:`9378`)
By `Etienne Schalk <https://github.com/etienneschalk>`_.
- Fix bug with rechunking to a frequency when some periods contain no data (:issue:`9360`).
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix bug causing `DataTree.from_dict` to be sensitive to insertion order (:issue:`9276`, :pull:`9292`).
Expand Down
16 changes: 15 additions & 1 deletion xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ def _check_alignment(
_check_alignment(child_path, child_ds, base_ds, child.children)


def _check_for_slashes_in_names(variables: Iterable[Hashable]) -> None:
offending_variable_names = [
name for name in variables if isinstance(name, str) and "/" in name
]
if len(offending_variable_names) > 0:
raise KeyError(
etienneschalk marked this conversation as resolved.
Show resolved Hide resolved
f"Given Dataset contains path-like variable names: {offending_variable_names}. "
"A Dataset represents a group, and a single group "
"cannot have path-like variable names. "
etienneschalk marked this conversation as resolved.
Show resolved Hide resolved
)


class DatasetView(Dataset):
"""
An immutable Dataset-like view onto the data in a single DataTree node.
Expand Down Expand Up @@ -459,7 +471,9 @@ def __init__(
children = {}

super().__init__(name=name)
self._set_node_data(_coerce_to_dataset(data))
ds = _coerce_to_dataset(data)
self._set_node_data(ds)
_check_for_slashes_in_names(ds.variables)
etienneschalk marked this conversation as resolved.
Show resolved Hide resolved
TomNicholas marked this conversation as resolved.
Show resolved Hide resolved
self.parent = parent
self.children = children

Expand Down
17 changes: 17 additions & 0 deletions xarray/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ def test_child_gets_named_on_attach(self):
mary: DataTree = DataTree(children={"Sue": sue}) # noqa
assert sue.name == "Sue"

def test_dataset_containing_slashes(self):
xda: xr.DataArray = xr.DataArray(
[[1, 2]],
coords={"label": ["a"], "R30m/y": [30, 60]},
)
xds: xr.Dataset = xr.Dataset({"group/subgroup/my_variable": xda})
with pytest.raises(
KeyError,
etienneschalk marked this conversation as resolved.
Show resolved Hide resolved
match=re.escape(
"Given Dataset contains path-like variable names: "
"['R30m/y', 'group/subgroup/my_variable']. "
"A Dataset represents a group, and a single group cannot "
"have path-like variable names. "
),
):
DataTree(xds)


class TestPaths:
def test_path_property(self):
Expand Down
Loading