Skip to content

In-place addition of arrays with the same coords but in a different order #3910

Description

@mancellin

I have two DataArrays with the same dimension, but the index is in a different order.
Adding them with A + B works fine, but the in-place addition fails.

MCVE Code Sample

import numpy as np
import xarray as xr

n = 5

d1 = np.arange(n)
np.random.shuffle(d1)
A = xr.DataArray(np.ones(n), coords=[('dim', d1)])

d2 = np.arange(n)
np.random.shuffle(d2)
B = xr.DataArray(np.ones(n), coords=[('dim', d2)])

print(A + B)
A += B

Expected Output

A = A + B is working fine. I would expect A += B to do the same.

Problem Description

The in-place addition A += B fails:

Traceback (most recent call last):
  File "/home/matthieu/xarray-test.py", line 15, in <module>
    A += B
  File "/opt/anaconda3/envs/xarray-tests/lib/python3.8/site-packages/xarray/core/dataarray.py", line 2619, in func
    with self.coords._merge_inplace(other_coords):
  File "/opt/anaconda3/envs/xarray-tests/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/opt/anaconda3/envs/xarray-tests/lib/python3.8/site-packages/xarray/core/coordinates.py", line 140, in _merge_inplace
    variables, indexes = merge_coordinates_without_align(
  File "/opt/anaconda3/envs/xarray-tests/lib/python3.8/site-packages/xarray/core/merge.py", line 328, in merge_coordinates_withou
t_align
    return merge_collected(filtered, prioritized)
  File "/opt/anaconda3/envs/xarray-tests/lib/python3.8/site-packages/xarray/core/merge.py", line 210, in merge_collected
    raise MergeError(
xarray.core.merge.MergeError: conflicting values for index 'dim' on objects to be combined:
first value: Int64Index([1, 2, 0, 3, 4], dtype='int64', name='dim')
second value: Int64Index([4, 2, 3, 1, 0], dtype='int64', name='dim')

Versions

Output of `xr.show_versions()` INSTALLED VERSIONS ------------------ commit: None python: 3.8.2 (default, Mar 26 2020, 15:53:00) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 4.19.112-1-MANJARO machine: x86_64 processor: byteorder: little LC_ALL: None LANG: fr_FR.UTF-8 LOCALE: fr_FR.UTF-8 libhdf5: None libnetcdf: None

xarray: 0.15.0
pandas: 1.0.3
numpy: 1.18.1
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
setuptools: 46.1.1.post20200323
pip: 20.0.2
conda: None
pytest: None
IPython: None
sphinx: None

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions