Skip to content

Objects with multiple identical xindexs on a dim aren't alignable #9474

Description

@max-sixty

What happened?

I'm less up to speed on the xindex functionality, but a colleagues shared a similar issue to this, so some people are finding their way to them (good news!)

But one issue — it seems like here if we have an index u and v on the u dimension, we can't do something like foo + foo

What did you expect to happen?

Either it works, or — like the old days — it doesn't work but then we shouldn't allow getting the object into that state

Minimal Complete Verifiable Example

ds = xr.Dataset(
    data_vars=dict(
        x=(("u",), [1, 2, 3, 4]),
        y=(("u",), [100, 200, 300, 400]),
    ),
    coords=dict(
        u=[1, 2, 3, 4],
        v=(("u",), [1, 2, 1, 2]),
    ),
).set_xindex("v")

# <xarray.Dataset> Size: 128B
# Dimensions:  (u: 4)
# Coordinates:
#   * u        (u) int64 32B 1 2 3 4
#   * v        (u) int64 32B 1 2 1 2
# Data variables:
#     x        (u) int64 32B 1 2 3 4
#     y        (u) int64 32B 100 200 300 400

ds.x * ds.y

# Error 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

File ~/workspace/xarray/xarray/core/_typed_ops.py:253, in DataArrayOpsMixin.__mul__(self, other)
    252 def __mul__(self, other: DaCompatible) -> Self:
--> 253     return self._binary_op(other, operator.mul)

File ~/workspace/xarray/xarray/core/dataarray.py:4747, in DataArray._binary_op(self, other, f, reflexive)
   4745 if isinstance(other, DataArray):
   4746     align_type = OPTIONS["arithmetic_join"]
-> 4747     self, other = align(self, other, join=align_type, copy=False)
   4748 other_variable_or_arraylike: DaCompatible = getattr(other, "variable", other)
   4749 other_coords = getattr(other, "coords", None)

File ~/workspace/xarray/xarray/core/alignment.py:887, in align(join, copy, indexes, exclude, fill_value, *objects)
    691 """
    692 Given any number of Dataset and/or DataArray objects, returns new
    693 objects with aligned indexes and dimension sizes.
   (...)
    877 
    878 """
    879 aligner = Aligner(
    880     objects,
    881     join=join,
   (...)
    885     fill_value=fill_value,
    886 )
--> 887 aligner.align()
    888 return aligner.results

File ~/workspace/xarray/xarray/core/alignment.py:578, in Aligner.align(self)
    576 self.find_matching_indexes()
    577 self.find_matching_unindexed_dims()
--> 578 self.assert_no_index_conflict()
    579 self.align_indexes()
    580 self.assert_unindexed_dim_sizes_equal()

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

ValueError: cannot re-index or align objects with conflicting indexes found for the following dimensions: 'u' (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

Details

INSTALLED VERSIONS

commit: 9735216
python: 3.11.10 (main, Sep 7 2024, 01:03:31) [Clang 15.0.0 (clang-1500.3.9.4)]
python-bits: 64
OS: Darwin
OS-release: 23.6.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: None
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2

xarray: 2024.7.1.dev68+gcc74d3ae
pandas: 2.2.2
numpy: 2.0.2
scipy: 1.14.1
netCDF4: 1.7.1.post2
pydap: None
h5netcdf: 1.3.0
h5py: 3.11.0
zarr: 2.18.3
cftime: 1.6.4
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.4.0
dask: 2024.8.2
distributed: 2024.8.2
matplotlib: 3.9.2
cartopy: None
seaborn: 0.13.2
numbagg: 0.8.1
fsspec: 2024.9.0
cupy: None
pint: None
sparse: None
flox: 0.9.11
numpy_groupies: 0.11.2
setuptools: 69.2.0
pip: 24.0
conda: None
pytest: 8.3.3
mypy: 1.11.2
IPython: 8.24

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions