Repository: pydata/xarray

Issue:
BUG: Indexing by 0-dimensional array
```python
In [1]: import xarray as xr
   ...: import numpy as np
   ...: 
   ...: a = np.arange(10)
   ...: a[np.array(0)]
   ...: 
Out[1]: 0

In [2]: da = xr.DataArray(a, dims='x')
   ...: da[np.array(0)]
   ...: 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-d30fdfc612ec> in <module>()
      1 da = xr.DataArray(a, dims='x')
----> 2 da[np.array(0)]

/home/keisukefujii/Dropbox/projects/xarray.git/xarray/core/dataarray.pyc in __getitem__(self, key)
    478         else:
    479             # xarray-style array indexing
--> 480             return self.isel(**self._item_key_to_dict(key))
    481 
    482     def __setitem__(self, key, value):

/home/keisukefujii/Dropbox/projects/xarray.git/xarray/core/dataarray.pyc in isel(self, drop, **indexers)
    759         DataArray.sel
    760         """
--> 761         ds = self._to_temp_dataset().isel(drop=drop, **indexers)
    762         return self._from_temp_dataset(ds)
    763 

/home/keisukefujii/Dropbox/projects/xarray.git/xarray/core/dataset.py in isel(self, drop, **indexers)
   1390         for name, var in iteritems(self._variables):
   1391             var_indexers = {k: v for k, v in indexers_list if k in var.dims}
-> 1392             new_var = var.isel(**var_indexers)
   1393             if not (drop and name in var_indexers):
   1394                 variables[name] = new_var

/home/keisukefujii/Dropbox/projects/xarray.git/xarray/core/variable.pyc in isel(self, **indexers)
    851             if dim in indexers:
    852                 key[i] = indexers[dim]
--> 853         return self[tuple(key)]
    854 
    855     def squeeze(self, dim=None):

/home/keisukefujii/Dropbox/projects/xarray.git/xarray/core/variable.pyc in __getitem__(self, key)
    619         array `x.values` directly.
    620         """
--> 621         dims, indexer, new_order = self._broadcast_indexes(key)
    622         data = as_indexable(self._data)[indexer]
    623         if new_order:

/home/keisukefujii/Dropbox/projects/xarray.git/xarray/core/variable.pyc in _broadcast_indexes(self, key)
    477         # key can be mapped as an OuterIndexer.
    478         if all(not isinstance(k, Variable) for k in key):
--> 479             return self._broadcast_indexes_outer(key)
    480 
    481         # If all key is 1-dimensional and there are no duplicate labels,

/home/keisukefujii/Dropbox/projects/xarray.git/xarray/core/variable.pyc in _broadcast_indexes_outer(self, key)
    542             new_key.append(k)
    543 
--> 544         return dims, OuterIndexer(tuple(new_key)), None
    545 
    546     def _nonzero(self):

/home/keisukefujii/Dropbox/projects/xarray.git/xarray/core/indexing.py in __init__(self, key)
    368                     raise TypeError('invalid indexer array for {}, must have '
    369                                     'exactly 1 dimension: '
--> 370                                     .format(type(self).__name__, k))
    371                 k = np.asarray(k, dtype=np.int64)
    372             else:

TypeError: invalid indexer array for OuterIndexer, must have exactly 1 dimension: 
```

Indexing by a 0d-array should be identical to the indexing by a scalar.

Discussion:

Patch:
diff --git a/doc/whats-new.rst b/doc/whats-new.rst
index fe8332c3..6018a184 100644
--- a/doc/whats-new.rst
+++ b/doc/whats-new.rst
@@ -106,6 +106,8 @@ Enhancements
 
 Bug fixes
 ~~~~~~~~~
+- Support indexing with a 0d-np.ndarray (:issue:`1921`).
+  By `Keisuke Fujii <https://github.com/fujiisoup>`_.
 - Added warning in api.py of a netCDF4 bug that occurs when
   the filepath has 88 characters (:issue:`1745`).
   By `Liam Brannigan <https://github.com/braaannigan>`_.
diff --git a/xarray/core/variable.py b/xarray/core/variable.py
index 14fc3480..267dc02c 100644
--- a/xarray/core/variable.py
+++ b/xarray/core/variable.py
@@ -463,10 +463,14 @@ class Variable(common.AbstractArray, utils.NdimSizeLenMixin):
         key = self._item_key_to_tuple(key)  # key is a tuple
         # key is a tuple of full size
         key = indexing.expanded_indexer(key, self.ndim)
-        # Convert a scalar Variable as an integer
+        # Convert a scalar Variable to an integer
         key = tuple(
             k.data.item() if isinstance(k, Variable) and k.ndim == 0 else k
             for k in key)
+        # Convert a 0d-array to an integer
+        key = tuple(
+            k.item() if isinstance(k, np.ndarray) and k.ndim == 0 else k
+            for k in key)
 
         if all(isinstance(k, BASIC_INDEXING_TYPES) for k in key):
             return self._broadcast_indexes_basic(key)


Test patch:
diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py
index d018df8a..c8f74762 100644
--- a/xarray/tests/test_variable.py
+++ b/xarray/tests/test_variable.py
@@ -627,6 +627,12 @@ class VariableSubclassTestCases(object):
         v_new = v[np.array([0])[0]]
         assert_array_equal(v_new, v_data[0])
 
+        v_new = v[np.array(0)]
+        assert_array_equal(v_new, v_data[0])
+
+        v_new = v[Variable((), np.array(0))]
+        assert_array_equal(v_new, v_data[0])
+
     def test_getitem_fancy(self):
         v = self.cls(['x', 'y'], [[0, 1, 2], [3, 4, 5]])
         v_data = v.compute().data