Skip to content

DataTree constructor error message says dict even though dict is not accepted #11514

Description

@mgunyho

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

Details

INSTALLED VERSIONS

commit: 80b6a92
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds triageIssue that has not been reviewed by xarray team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions