Skip to content

check_dim_order=False not working well when passing xr.Dataset to xarray.testing.assert_equal #10704

@alvarosg

Description

@alvarosg

What happened?

xarray.testing.assert_equal with check_dim_order=False

fails with two datasets that only differ in the dim_order of individual data vars or coords when the relative order of the dims is not consistent across all variables in the dataset.

I believe this is because this line
b.transpose(*a.dims)
is getting applied when a is a dataset, but a.dims order is not meaningful when a is a dataset.

I believe potential solutions are:

  • transpose the dims on a var by var basis.
  • Do something like
dim_order = list(a.dims)
b = b.transpose(*dim_order)
a = a.transpose(*dim_order)  # Note this is necesary as not all vars in `a` may actually follow a.dims order.

What did you expect to happen?

xarray.testing.assert_equal with check_dim_order=False

should not fail with two datasets only differ in the dim_order of individual data vars or coords.

Minimal Complete Verifiable Example

import xarray
import numpy as np

dataset_1 = xarray.Dataset(
    {"foo": xarray.DataArray(np.zeros([4, 5]), dims=("a", "b")),
     "bar": xarray.DataArray(np.zeros([5, 4]), dims=("b", "a"))})
    
dataset_2 = xarray.Dataset(
    {"foo": xarray.DataArray(np.zeros([5, 4]), dims=("b", "a")),
     "bar": xarray.DataArray(np.zeros([4, 5]), dims=("a", "b"))})

xarray.testing.assert_equal(dataset_1["foo"], dataset_2["foo"], check_dim_order=False)  # passes
xarray.testing.assert_equal(dataset_1["bar"], dataset_2["bar"], check_dim_order=False)  # passes

xarray.testing.assert_equal(dataset_1, dataset_2, check_dim_order=False)  # Fails with log below.

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

> AssertionError                            Traceback (most recent call last)
/tmp/ipython-input-1957692165.py in <cell line: 0>()
      2 xarray.testing.assert_equal(dataset_1["bar"], dataset_2["bar"], check_dim_order=False)
      3 
----> 4 xarray.testing.assert_equal(dataset_1, dataset_2, check_dim_order=False)

    [... skipping hidden 1 frame]

/usr/local/lib/python3.12/dist-packages/xarray/testing/assertions.py in assert_equal(a, b, check_dim_order)
    144         assert a.equals(b), formatting.diff_array_repr(a, b, "equals")
    145     elif isinstance(a, Dataset):
--> 146         assert a.equals(b), formatting.diff_dataset_repr(a, b, "equals")
    147     elif isinstance(a, Coordinates):
    148         assert a.equals(b), formatting.diff_coords_repr(a, b, "equals")

AssertionError: Left and right Dataset objects are not equal
Differing data variables:
L   bar      (b, a) float64 160B 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
R   bar      (a, b) float64 160B 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0

Anything else we need to know?

No response

Environment

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