DataTree constructor error message says dict even though dict is not accepted
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
When I try to create a DataTree with a dict as the first argument, I get a TypeError:
>>> xr.DataTree(dict())
...
TypeError: data object is not an xarray.Dataset, dict, or None: {}
What did you expect to happen?
A dictionary should be accepted, or the error message should not say that the argument could be a dict.
I suppose this is a leftover from some earlier iteration of the API?
Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!
import xarray as xr
xr.show_versions()
# your reproducer code ...
xr.DataTree(dict())
Steps to reproduce
No response
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
File .../xarray/xarray/core/datatree.py:547, in DataTree.__init__(self, dataset, children, name)
517 def __init__(
518 self,
519 dataset: Dataset | Coordinates | None = None,
520 children: Mapping[str, DataTree] | None = None,
521 name: str | None = None,
522 ):
523 """
524 Create a single node of a DataTree.
525
(...) 545 DataTree.from_dict
546 """
--> 547 self._set_node_data(_to_new_dataset(dataset))
549 # comes after setting node data as this will check for clashes between child names and existing variable names
550 super().__init__(name=name, children=children)
File .../xarray/xarray/core/datatree.py:143, in _to_new_dataset(data)
141 ds = Dataset()
142 else:
--> 143 raise TypeError(f"data object is not an xarray.Dataset, dict, or None: {data}")
144 return ds
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: 80b6a92605ab246a4c95362fdcdf99e1c8b18a6e
python: 3.13.5 (main, Jun 12 2025, 12:40:22) [Clang 20.1.4 ]
python-bits: 64
OS: Linux
OS-release: 7.1.4-204.fc44.x86_64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.6
libnetcdf: 4.9.3
xarray: 2026.7.1.dev24+g80b6a9260.d20260806
pandas: 3.0.5
numpy: 2.4.6
scipy: 1.18.0
netCDF4: 1.7.4
pydap: 3.5.10
h5netcdf: 1.8.1
h5py: 3.16.0
zarr: 3.3.0
cftime: 1.6.5
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.6.0
dask: 2026.7.1
distributed: 2026.7.1
matplotlib: 3.11.1
cartopy: 0.25.0
seaborn: 0.13.2
numbagg: 0.9.4
fsspec: 2026.7.0
cupy: None
pint: None
sparse: 0.19.1
flox: 0.11.2
numpy_groupies: 0.11.3
setuptools: None
pip: None
conda: None
pytest: 9.1.1
mypy: 1.19.1
IPython: 9.16.1
sphinx: 8.2.3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in xarray/core/datatree.py at DataTree.init and _to_new_dataset, especially the error raised at line 143. Reproduce xr.DataTree(dict()) and update the behavior or message so it no longer contradicts the accepted input types; verify the resulting TypeError or successful construction matches the documented API.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 72/100