Skip to content

Assignment with vectorized indexing doesn't work with a scalar right-hand-side #1744

Closed
@shoyer

Description

@shoyer

Building on the example from #1743, I tried a version only using xarray.Variable objects on the left-hand-side. To my surprise, this still doesn't work:

In [8]: xarr.variable[l_indices.variable, c_indices.variable] = 2
---------------------------------------------------------------------------
AxisError                                 Traceback (most recent call last)
<ipython-input-8-75558576dcaf> in <module>()
----> 1 xarr.variable[l_indices.variable, c_indices.variable] = 2

~/dev/xarray/xarray/core/variable.py in __setitem__(self, key, value)
    653             value = np.moveaxis(value, new_order, range(len(new_order)))
    654
--> 655         self._indexable_data[index_tuple] = value
    656
    657     @property

~/dev/xarray/xarray/core/indexing.py in __setitem__(self, key, value)
    660     def __setitem__(self, key, value):
    661         array, key = self._indexing_array_and_key(key)
--> 662         array[key] = value
    663
    664

~/dev/xarray/xarray/core/nputils.py in __setitem__(self, key, value)
    132         mixed_positions, vindex_positions = _advanced_indexer_subspaces(key)
    133         self._array[key] = np.moveaxis(value, vindex_positions,
--> 134                                        mixed_positions)

~/conda/envs/xarray-dev/lib/python3.5/site-packages/numpy/core/numeric.py in moveaxis(a, source, destination)
   1599         transpose = a.transpose
   1600
-> 1601     source = normalize_axis_tuple(source, a.ndim, 'source')
   1602     destination = normalize_axis_tuple(destination, a.ndim, 'destination')
   1603     if len(source) != len(destination):

~/conda/envs/xarray-dev/lib/python3.5/site-packages/numpy/core/numeric.py in normalize_axis_tuple(axis, ndim, argname, allow_duplicate)
   1534     except TypeError:
   1535         axis = tuple(axis)
-> 1536     axis = tuple(normalize_axis_index(ax, ndim, argname) for ax in axis)
   1537     if not allow_duplicate and len(set(axis)) != len(axis):
   1538         if argname:

~/conda/envs/xarray-dev/lib/python3.5/site-packages/numpy/core/numeric.py in <genexpr>(.0)
   1534     except TypeError:
   1535         axis = tuple(axis)
-> 1536     axis = tuple(normalize_axis_index(ax, ndim, argname) for ax in axis)
   1537     if not allow_duplicate and len(set(axis)) != len(axis):
   1538         if argname:

AxisError: source: axis 0 is out of bounds for array of dimension 0

Assignment does work when the right-hand-side argument is an xarray.Variable, e.g.,

In [11]: xarr.variable[l_indices.variable, c_indices.variable] = xr.Variable((), 100)

In [12]: xarr
Out[12]:
<xarray.DataArray (y: 5, x: 5)>
array([[  0, 100,   2,   3,   4],
       [  5,   6,   7, 100,   9],
       [100,  11,  12,  13,  14],
       [ 15,  16, 100,  18,  19],
       [ 20,  21,  22,  23,  24]])
Dimensions without coordinates: y, x

Apparently we need a little more test coverage for assignment with vectorized indexing :).

CC @fujiisoup

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