Skip to content

Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identical #11777

Open

Description

@JoeyTan21

What happened?

When an attribute value is a dict that contains a NumPy array with more than one element, comparing two objects that carry this attribute fails:

  • DataArray.identical and xr.testing.assert_identical raise "ValueError: The truth value of an array with more than one element is ambiguous", even when the attributes are equal (and also when they differ, instead of returning False / raising AssertionError).
  • xr.merge with combine_attrs="drop_conflicts" drops the attribute although both values are equal, and combine_attrs="no_conflicts" raises a MergeError whose message shows two identical values.

Top-level array attributes and lists of arrays already work, because xarray.core.utils.equivalent special-cases ndarray and list. A dict falls through to ==, and dict equality calls bool() on the element-wise array comparison.

What did you expect to happen?

Nested dicts to be compared value by value, the same way top-level attrs are compared by dict_equiv: equal values give True / are kept on merge, differing values give False.

Minimal Complete Verifiable Example

import numpy as np
import xarray as xr

a = xr.DataArray([1, 2], dims="x", attrs={"calibration": {"coeffs": np.array([1.0, 2.0])}})
b = xr.DataArray([1, 2], dims="x", attrs={"calibration": {"coeffs": np.array([1.0, 2.0])}})

a.identical(b)  # ValueError
xr.merge([xr.Dataset(attrs=a.attrs), xr.Dataset(attrs=b.attrs)], combine_attrs="drop_conflicts").attrs  # {}
xr.merge([xr.Dataset(attrs=a.attrs), xr.Dataset(attrs=b.attrs)], combine_attrs="no_conflicts")  # MergeError

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

a.identical(b): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
merge drop_conflicts attrs: {}
merge no_conflicts attrs: MergeError: combine_attrs='no_conflicts', but some values are not the same. Merging {'calibration': {'coeffs': array([1., 2.])}} with {'calibration': {'coeffs': array([1., 2.])}}

Anything else we need to know?

Possible fix: in xarray.core.utils.equivalent, when both values are dicts, return dict_equiv(first, second), which already uses equivalent for the values. Only plain dicts, not Mapping, so Datasets are not affected. I have a small patch with a regression test and a whats-new entry and would be happy to open a PR.

Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.

Environment

Details

INSTALLED VERSIONS

commit: None
python: 3.12.13 (main, Jun 11 2026, 03:55:21) [Clang 22.1.3 ]
python-bits: 64
OS: Darwin
OS-release: 24.3.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None

xarray: 2026.9.0
pandas: 3.0.6
numpy: 2.5.4
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: None
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None

Activity

  1. added
    needs triageIssue that has not been reviewed by xarray team member
    on Oct 21, 2026
  2. anshurajbisoyi98-ctrl commented on Oct 28, 2026

    @anshurajbisoyi98-ctrl
    Contributor

    I can reproduce this on current main (aee3dc5, 2026.9.1.dev151+gaee3dc556) with numpy 2.5.4 and pandas 3.1.0. I ran the example from the issue and added one case where the nested values differ:

    a.identical(b)  [equal]   -> ValueError: The truth value of an array with more than one element is ambiguous
    a.identical(c)  [differ]  -> ValueError (expected False)
    assert_identical(a, b)    -> ValueError
    merge drop_conflicts      -> {}  (attr dropped although equal)
    merge no_conflicts        -> MergeError
    

    So the differing case also raises instead of returning False. #11782 looks like it targets this.

    xr.show_versions()
    INSTALLED VERSIONS
    ------------------
    python: 3.13.12
    python-bits: 64
    OS: Darwin
    OS-release: 25.6.0
    machine: arm64
    
    xarray: 2026.9.1.dev151+gaee3dc556
    pandas: 3.1.0
    numpy: 2.5.4
    pip: 25.3
    
    Not installed: scipy, netCDF4, pydap, h5netcdf, h5py, zarr, cftime, nc_time_axis, iris, bottleneck, dask, distributed, matplotlib, cartopy, seaborn, numbagg, fsspec, cupy, pint, sparse, flox, numpy_groupies, setuptools, conda, pytest, mypy, IPython, sphinx
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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