Repository: blaze/dask (python)

Issue:
dask.array take issue
I'm getting an error attempting to call da.take(a, axis=1). Sorry I don't have minimal reproducible example, but here's the context:

![image](https://cloud.githubusercontent.com/assets/703554/12915762/41bc6d68-cf25-11e5-8475-efbf21f2bf76.png)

...and here's the stack trace:

```
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-fde38592888e> in <module>()
----> 1 gds = da.take(gd, pop1_sample_indices, axis=1)
      2 gds

/usr/local/lib/python3.5/dist-packages/dask/array/core.py in take(a, indices, axis)
   1851         return _take_dask_array_from_numpy(a, indices, axis)
   1852     else:
-> 1853         return a[(slice(None),) * axis + (indices,)]
   1854 
   1855 

/usr/local/lib/python3.5/dist-packages/dask/array/core.py in __getitem__(self, index)
    985             return self
    986 
--> 987         dsk, chunks = slice_array(out, self.name, self.chunks, index)
    988 
    989         return Array(merge(self.dask, dsk), out, chunks, dtype=self._dtype)

/usr/local/lib/python3.5/dist-packages/dask/array/slicing.py in slice_array(out_name, in_name, blockdims, index)
    117     """
    118     index = replace_ellipsis(len(blockdims), index)
--> 119     index = tuple(map(sanitize_index, index))
    120     blockdims = tuple(map(tuple, blockdims))
    121 

/usr/local/lib/python3.5/dist-packages/dask/array/slicing.py in sanitize_index(ind)
     53         return ind
     54 
---> 55     raise IndexError("Invalid index", i)
     56 
     57 

NameError: name 'i' is not defined
```

Weird thing is, I don't know why dask is even trying to raise an IndexError, the indices are perfectly valid for the array.

Pull request description:
Sanitize index
Closes #971

cc @alimanfoo

Patch:
diff --git a/dask/array/slicing.py b/dask/array/slicing.py
index a29967df4..f8410b88a 100644
--- a/dask/array/slicing.py
+++ b/dask/array/slicing.py
@@ -38,7 +38,7 @@ def sanitize_index(ind):
             raise IndexError("Bad index.  Must be integer-like: %s" % ind)
         else:
             return ind2
-    if isinstance(ind, np.ndarray):
+    if hasattr(ind, 'tolist'):
         ind = ind.tolist()
     if isinstance(ind, list) and ind and isinstance(ind[0], bool):
         ind = [a for a, b in enumerate(ind) if b]
@@ -52,7 +52,7 @@ def sanitize_index(ind):
     if ind is None:
         return ind
 
-    raise IndexError("Invalid index", i)
+    raise TypeError("Invalid index type", type(ind), ind)
 
 
 def slice_array(out_name, in_name, blockdims, index):


Test patch:
diff --git a/dask/array/tests/test_slicing.py b/dask/array/tests/test_slicing.py
index 2d861bce7..732289df3 100644
--- a/dask/array/tests/test_slicing.py
+++ b/dask/array/tests/test_slicing.py
@@ -5,7 +5,8 @@ import dask
 from dask.compatibility import skip
 import dask.array as da
 from dask.array import Array
-from dask.array.slicing import slice_array, _slice_1d, take, new_blockdim
+from dask.array.slicing import (slice_array, _slice_1d, take, new_blockdim,
+        sanitize_index)
 from operator import getitem
 import numpy as np
 from toolz import merge
@@ -467,3 +468,11 @@ def test_slicing_consistent_names():
     assert same_keys(a[0], a[0])
     assert same_keys(a[:, [1, 2, 3]], a[:, [1, 2, 3]])
     assert same_keys(a[:, 5:2:-1], a[:, 5:2:-1])
+
+
+def test_sanitize_index():
+    pd = pytest.importorskip('pandas')
+    with pytest.raises(TypeError):
+        sanitize_index('Hello!')
+
+    assert sanitize_index(pd.Series([1, 2, 3])) == [1, 2, 3]