pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.57k stars 1.07k forks source link

align fails when more than one xindex is set #8436

Closed mathause closed 10 months ago

mathause commented 10 months ago

What happened?

I tried a DataArray with more than one dimension coordinate. Unfortunately xr.align fails, which disallows any arithmetic operation - even when the coords are exactly the same.

What did you expect to happen?

No response

Minimal Complete Verifiable Example

import numpy as np
import xarray as xr

data = np.arange(12).reshape(3, 4)

y = [10, 20, 30]
s = ["a", "b", "c"]

x = [1, 2, 3, 4]

da = xr.DataArray(data, dims=("y", "x"), coords={"x": x, "y": y, "s": ("y", s)})
da = da.set_xindex("s")

xr.align(da, da.y) # errors
da + da # errors
da + da.x # errors

MVCE confirmation

Relevant log output

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/mathause/code/mesmer/devel/prepare_for_surfer.ipynb Cell 28 line 1
     12 da = xr.DataArray(data, dims=("y", "x"), coords={"x": x, "y": y, "s": ("y", s)})
     13 da = da.set_xindex("s")
---> 15 xr.align(da, da.y) # errors
     17 da + da.x # errors

File ~/.conda/envs/mesmer_dev/lib/python3.9/site-packages/xarray/core/alignment.py:888, in align(join, copy, indexes, exclude, fill_value, *objects)
    692 """
    693 Given any number of Dataset and/or DataArray objects, returns new
    694 objects with aligned indexes and dimension sizes.
ref='~/.conda/envs/mesmer_dev/lib/python3.9/site-packages/xarray/core/alignment.py:0'>0</a>;32m   (...)
    878 
    879 """
    880 aligner = Aligner(
    881     objects,
    882     join=join,
ref='~/.conda/envs/mesmer_dev/lib/python3.9/site-packages/xarray/core/alignment.py:0'>0</a>;32m   (...)
    886     fill_value=fill_value,
    887 )
--> 888 aligner.align()
    889 return aligner.results

File ~/.conda/envs/mesmer_dev/lib/python3.9/site-packages/xarray/core/alignment.py:573, in Aligner.align(self)
    571 self.find_matching_indexes()
    572 self.find_matching_unindexed_dims()
--> 573 self.assert_no_index_conflict()
    574 self.align_indexes()
    575 self.assert_unindexed_dim_sizes_equal()

File ~/.conda/envs/mesmer_dev/lib/python3.9/site-packages/xarray/core/alignment.py:318, in Aligner.assert_no_index_conflict(self)
    314 if dup:
    315     items_msg = ", ".join(
    316         f"{k!r} ({v} conflicting indexes)" for k, v in dup.items()
    317     )
--> 318     raise ValueError(
    319         "cannot re-index or align objects with conflicting indexes found for "
    320         f"the following {msg}: {items_msg}\n"
    321         "Conflicting indexes may occur when\n"
    322         "- they relate to different sets of coordinate and/or dimension names\n"
    323         "- they don't have the same type\n"
    324         "- they may be used to reindex data along common dimensions"
    325     )

ValueError: cannot re-index or align objects with conflicting indexes found for the following dimensions: 'y' (2 conflicting indexes)
Conflicting indexes may occur when
- they relate to different sets of coordinate and/or dimension names
- they don't have the same type
- they may be used to reindex data along common dimensions

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS ------------------ commit: feba6984aa914327408fee3c286dae15969d2a2f python: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:40:32) [GCC 12.3.0] python-bits: 64 OS: Linux OS-release: 6.2.0-36-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.14.2 libnetcdf: 4.9.2 xarray: 2023.9.1.dev8+gf6d69a1f pandas: 2.1.1 numpy: 1.24.4 scipy: 1.11.3 netCDF4: 1.6.4 pydap: installed h5netcdf: 1.2.0 h5py: 3.9.0 Nio: None zarr: 2.16.1 cftime: 1.6.2 nc_time_axis: 1.4.1 PseudoNetCDF: 3.2.2 iris: 3.7.0 bottleneck: 1.3.7 dask: 2023.9.2 distributed: None matplotlib: 3.8.0 cartopy: 0.22.0 seaborn: 0.12.2 numbagg: 0.2.2 fsspec: 2023.9.2 cupy: None pint: 0.20.1 sparse: 0.14.0 flox: 0.7.2 numpy_groupies: 0.10.1 setuptools: 68.2.2 pip: 23.2.1 conda: None pytest: 7.4.2 mypy: 1.5.1 IPython: 8.15.0 sphinx: None
headtr1ck commented 10 months ago

That's a duplicate of https://github.com/pydata/xarray/issues/8236 and https://github.com/pydata/xarray/issues/7695

mathause commented 10 months ago

And just like you, I did not find the duplicates :shrug: