-
- Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
What happened?
When aligning two Datasets, indexes that are already aligned (require no reindexing) are not considered when checking for index conflicts.
More concretely:
When I attempt to align two Datasets that share two indexes for the same dimension of which one is aligned (requires no reindexing) and the other is not, no error is raised if these indexes conflict with one another. The mismatched index is used to align the data.
What did you expect to happen?
I would expect an AlignmentError to be raised when an attempt is made to align objects with conflicting indexes in all situations.
Minimal Complete Verifiable Example
from xarray import align, Dataset ds1 = Dataset(coords={"x": [1, 2, 3], "xb": ("x", [4, 5, 6])}).set_xindex("xb") # Swap two values in only "xb" only ds2 = Dataset(coords={"x": [1, 2, 3], "xb": ("x", [4, 6, 5])}).set_xindex("xb") # Swap two (different) values in both coordinates ds3 = Dataset(coords={"x": [2, 1, 3], "xb": ("x", [4, 6, 5])}).set_xindex("xb") align(ds1, ds2) # --> no error (which is wrong) align(ds1, ds3) # --> AlignmentError (as expected)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?
Until recently, alignment it was not possible when multiple indexes shared a dimension. This functionality was introduced in xarray v2025.04.0 and originally merged in PR #8436 by @benbovy .
The problem is that the index consistency checks ignore indexes that require no reindexing.
# Source: xarray/structure/alignment.py def _get_dim_pos_indexers( self, matching_indexes: dict[MatchingIndexKey, Index], ) -> dict[Hashable, Any]: dim_pos_indexers: dict[Hashable, Any] = {} dim_index: dict[Hashable, Index] = {} for key, aligned_idx in self.aligned_indexes.items(): obj_idx = matching_indexes.get(key) if obj_idx is not None and self.reindex[key]: ####################################################################### # ↓ None of this code is run for indexes that require no reindexing ↓ # ####################################################################### indexers = obj_idx.reindex_like(aligned_idx, **self.reindex_kwargs) for dim, idxer in indexers.items(): if dim in self.exclude_dims: raise AlignmentError( f"cannot reindex or align along dimension {dim!r} because " "it is explicitly excluded from alignment. This is likely caused by " "wrong results returned by the `reindex_like` method of this index:\n" f"{obj_idx!r}" ) if dim in dim_pos_indexers and not np.array_equal( idxer, dim_pos_indexers[dim] ): raise AlignmentError( f"cannot reindex or align along dimension {dim!r} because " "of conflicting re-indexers returned by multiple indexes\n" f"first index: {obj_idx!r}\nsecond index: {dim_index[dim]!r}\n" ) dim_pos_indexers[dim] = idxer dim_index[dim] = obj_idx return dim_pos_indexersEnvironment
xarray: 2025.9.0
pandas: 2.3.2
numpy: 2.3.2
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: 9.5.0
sphinx: None