pydata / pydata/xarray

Cannot build xarrays with hashable UUIDs as dims

Open
#10,634 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The traceback points to xarray/core/dataarray.py, xarray/core/variable.py, and xarray/namedarray/core.py; reproduce the MVCE first and trace dimension and coordinate handling from there. Done means the UUID-based DataArray either constructs successfully or reports a relevant error instead of the shown TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.