Skip to content

Cannot build xarrays with hashable UUIDs as dims #10634

@SoundDesignerToBe

Description

@SoundDesignerToBe

What happened?

Due to serialization issues, I have been working on a converter that replaces UUID objects in different places of xarray.DataArrays with strings. Upon creating some test scenarios, I ran into a bug.

The first issue is that the uuid objects appear to not work as dimension despite the fact that they are hashable.

The second issue is that attempt to build the DataArray yielded a misleading (or wrong?) error message.

What did you expect to happen?

I expected either the DataArray to be constructed without error OR to throw a relevant error message that would deny using UUIDs as dimension labels.

Minimal Complete Verifiable Example

import sys
from typing import Hashable
import uuid

import numpy as np
import xarray as xr

sys.version 
# Out[33]: '3.12.11 (main, Jun 12 2025, 00:00:00) [GCC 15.1.1 20250521 (Red Hat 15.1.1-2)]'

xr.__version__
# Out[29]: '2025.7.1'

np.__version__
# Out[30]: '2.3.2'

some_id = uuid.uuid4()
some_id_2 = uuid.uuid4()

assert isinstance(uuid.UUID, Hashable)  # True
assert isinstance(some_id, Hashable)  # True

dims = (some_id, some_id_2)
xr.DataArray(
    np.asarray([[0.0, 1.0]]),
    dims=dims,
    coords={some_id: 1, some_id_2: [uuid.uuid4() for _ in range(2)]},
)




Traceback (most recent call last):
  File "[...]/scripts/debug_xarray.py", line 16, in <module>
    xr.DataArray(
  File "[...]/.venv/lib64/python3.12/site-packages/xarray/core/dataarray.py", line 461, in __init__
    coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]/.venv/lib64/python3.12/site-packages/xarray/core/dataarray.py", line 182, in _infer_coords_and_dims
    new_coords[k] = as_variable(v, name=k, auto_convert=False)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]/.venv/lib64/python3.12/site-packages/xarray/core/variable.py", line 170, in as_variable
    obj = Variable(name, data, fastpath=True)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]/.venv/lib64/python3.12/site-packages/xarray/core/variable.py", line 379, in __init__
    super().__init__(
  File "[...]/.venv/lib64/python3.12/site-packages/xarray/namedarray/core.py", line 261, in __init__
    self._dims = self._parse_dimensions(dims)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]/.venv/lib64/python3.12/site-packages/xarray/namedarray/core.py", line 505, in _parse_dimensions
    dims = (dims,) if isinstance(dims, str) else tuple(dims)
                                                 ^^^^^^^^^^^
TypeError: 'UUID' object is not iterable

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

Anything else we need to know?

No response

Environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions