-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In-place addition of arrays with the same coords but in a different order #3910
Comments
Yes, this is unfortunate. The reasoning: xarray/xarray/core/dataarray.py Lines 2618 to 2621 in 732b6cd
It may be possible to at align in some cases (e.g. if the indexes are bijective / one-to-one, or the values are already floats). Or a better error message; even one containing that comment would be better. |
@mancellin Are you up for sending in a PR with a better error message? |
I can submit a PR. But the comment cited above is not totally clear to me. The purpose of the conversion to floats is to have NaNs in case the shapes do not match. So the core of the issue is that A + B might not have the same shape as A, and thus in general A + B cannot replace A in-place. |
Thanks in advance @mancellin Your comment is almost exactly right. It's that they might not align fully, rather than the shape; i.e. if your example had |
Yes. But the not-in-place addition |
Yes exactly! And I think in-place might be surprising if it changed the indexes of the left item; i.e. if you got this result from In [17]:
...:
...: import numpy as np
...: import xarray as xr
...:
...: n = 5
...:
...: d1 = np.arange(1, n+1)
...: 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)])
...:
...: A + B
Out[17]:
<xarray.DataArray (dim: 4)>
array([2., 2., 2., 2.])
Coordinates:
* dim (dim) int64 3 2 1 4 |
Well actually, I would be at least as surprised if So now I understand why this is not supported. I'll submit the PR soon. |
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
Expected Output
A = A + B
is working fine. I would expectA += B
to do the same.Problem Description
The in-place addition
A += B
fails: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: Nonexarray: 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
The text was updated successfully, but these errors were encountered: