Skip to content

Assigning data to vector-indexed data doesn't seem to work #1743

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

Closed
mraspaud opened this issue Nov 28, 2017 · 4 comments
Closed

Assigning data to vector-indexed data doesn't seem to work #1743

mraspaud opened this issue Nov 28, 2017 · 4 comments

Comments

@mraspaud
Copy link
Contributor

mraspaud commented Nov 28, 2017

Code Sample

import xarray as xr
import numpy as np
import dask.array as da

arr = np.arange(25).reshape((5, 5))

l_indices = xr.DataArray(np.array(((0, 1), (2, 3))), dims=['lines', 'cols'])
c_indices = xr.DataArray(np.array(((1, 3), (0, 2))), dims=['lines', 'cols'])

xarr = xr.DataArray(da.from_array(arr, chunks=10), dims=['y', 'x'])

print(xarr[l_indices, c_indices])

xarr[l_indices, c_indices] = 2

Problem description

This crashes on the last line with a

IndexError: Unlabeled multi-dimensional array cannot be used for indexing: [[0 1]
 [2 3]]

I'm expecting to be able to do assignment this way, and it doesn't work.

Expected Output

Expected output is the modified array with 2's in the indicated positions

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 2.7.5.final.0 python-bits: 64 OS: Linux OS-release: 3.10.0-693.2.2.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: None.None

xarray: 0.10.0
pandas: 0.21.0
numpy: 1.13.3
scipy: 0.18.1
netCDF4: 1.1.8
h5netcdf: 0.4.2
Nio: None
bottleneck: None
cyordereddict: None
dask: 0.15.4
matplotlib: 1.2.0
cartopy: None
seaborn: None
setuptools: 36.2.1
pip: 9.0.1
conda: None
pytest: 3.1.3
IPython: 5.1.0
sphinx: 1.3.6

@mraspaud
Copy link
Contributor Author

@shoyer this is what I showed you earlier

@jhamman
Copy link
Member

jhamman commented Nov 28, 2017

@mraspaud - dask arrays don't support assignment so your last line should not work. More importantly though, what indexing/broadcasting are you expecting l_indices, and c_indices to provide?

@mraspaud
Copy link
Contributor Author

@jhamman Same problem rises with a regular numpy array as a source for the DataArray.

The use case I have is spatial resampling: assigning source data pixels to a destination grid. So in the example provided above, l_indices and c_indices would be the shape of the of the destination grid and tell us which pixels to take from the source data.

@shoyer
Copy link
Member

shoyer commented Nov 28, 2017

cc @fujiisoup

It looks like we forgot to update DataArray.__setitem__ to handle conversion of DataArray -> Variable objects, which we do for __getitem__ in Dataset._validate_indexers. DataArray.__setitem__ currently just passes on the key directly to Variable.__setitem__.

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

No branches or pull requests

3 participants