Repository: dask/dask

Issue:
Dask array repeat mishandles negative axis
When providing `repeat` with a valid negative value for `axis`, it appears to mishandle and raise an `IndexError` instead. An example of the problem is included in the details below along with the environment used to reproduce it.

Note: These all come from `conda-forge` where possible, but it seems `conda` stopped exporting that info. Have raised that problem in issue ( https://github.com/conda/conda/issues/5706 ).

Traceback:

<details>

```python
In [1]: import dask.array as da

In [2]: a = da.ones((4, 6), chunks=(2, 3))

In [3]: da.repeat(a, 2, axis=1)
Out[3]: dask.array<concatenate, shape=(4, 12), dtype=float64, chunksize=(2, 6)>

In [4]: da.repeat(a, 2, axis=-1)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-1ea79a1dccaa> in <module>()
----> 1 da.repeat(a, 2, axis=-1)

/zopt/conda2/envs/daskenv/lib/python2.7/site-packages/dask/array/core.pyc in repeat(a, repeats, axis)
   3971               for s in slices]
   3972 
-> 3973     slabs = [a[slc] for slc in slices]
   3974 
   3975     out = []

/zopt/conda2/envs/daskenv/lib/python2.7/site-packages/dask/array/core.pyc in __getitem__(self, index)
   1233             return self
   1234 
-> 1235         dsk, chunks = slice_array(out, self.name, self.chunks, index)
   1236 
   1237         dsk2 = sharedict.merge(self.dask, (out, dsk))

/zopt/conda2/envs/daskenv/lib/python2.7/site-packages/dask/array/slicing.pyc in slice_array(out_name, in_name, blockdims, index)
    158 
    159     # Pass down to next function
--> 160     dsk_out, bd_out = slice_with_newaxes(out_name, in_name, blockdims, index)
    161 
    162     bd_out = tuple(map(tuple, bd_out))

/zopt/conda2/envs/daskenv/lib/python2.7/site-packages/dask/array/slicing.pyc in slice_with_newaxes(out_name, in_name, blockdims, index)
    180 
    181     # Pass down and do work
--> 182     dsk, blockdims2 = slice_wrap_lists(out_name, in_name, blockdims, index2)
    183 
    184     if where_none:

/zopt/conda2/envs/daskenv/lib/python2.7/site-packages/dask/array/slicing.pyc in slice_wrap_lists(out_name, in_name, blockdims, index)
    220                for i in index)
    221     if not len(blockdims) == len(index):
--> 222         raise IndexError("Too many indices for array")
    223 
    224     for bd_size, i in zip(shape, index):

IndexError: Too many indices for array
```

</details>

<br>
Environment:
<br>
<br>

<details>

```yaml
name: daskenv
channels:
- conda-forge
- defaults
dependencies:
- alabaster=0.7.10=py27_0
- appnope=0.1.0=py27_0
- asn1crypto=0.22.0=py27_0
- babel=2.3.4=py27_0
- backports.lzma=0.0.8=py27_0
- backports.shutil_get_terminal_size=1.0.0=py27_1
- backports_abc=0.5=py27_0
- bkcharts=0.2=py27_0
- blas=1.1=openblas
- bokeh=0.12.6=py27_0
- boto3=1.4.4=py27_0
- botocore=1.5.84=py27_0
- ca-certificates=2017.4.17=0
- certifi=2017.4.17=py27_0
- cffi=1.10.0=py27_0
- chardet=3.0.2=py27_1
- click=6.7=py27_0
- cloudpickle=0.3.1=py27_0
- configparser=3.5.0=py27_0
- coverage=4.4.1=py27_0
- cryptography=1.9=py27_0
- cycler=0.10.0=py27_0
- dask=0.15.1=py27_0
- decorator=4.1.1=py27_0
- distributed=1.18.0=py27_0
- docutils=0.13.1=py27_0
- enum34=1.1.6=py27_1
- flake8=3.3.0=py27_0
- freetype=2.7=1
- functools32=3.2.3.2=py27_1
- futures=3.0.5=py27_0
- graphviz=2.38.0=4
- h5py=2.7.0=np113py27_1
- hdf5=1.8.18=0
- heapdict=1.0.0=py27_0
- idna=2.5=py27_0
- imagesize=0.7.1=py27_0
- ipaddress=1.0.18=py27_0
- ipython=5.4.1=py27_0
- ipython_genutils=0.2.0=py27_0
- jinja2=2.9.5=py27_0
- jmespath=0.9.3=py27_0
- jpeg=9b=0
- libffi=3.2.1=3
- libgfortran=3.0.0=0
- libpng=1.6.28=0
- libtiff=4.0.6=7
- locket=0.2.0=py27_1
- markupsafe=1.0=py27_0
- matplotlib=2.0.2=np113py27_0
- mccabe=0.6.1=py27_0
- msgpack-python=0.4.8=py27_0
- ncurses=5.9=10
- numpy=1.13.1=py27_blas_openblas_200
- numpydoc=0.7.0=py27_0
- openblas=0.2.19=2
- openssl=1.0.2l=0
- pandas=0.20.3=py27_1
- partd=0.3.8=py27_0
- pathlib2=2.3.0=py27_0
- pexpect=4.2.1=py27_0
- pickleshare=0.7.3=py27_0
- pip=9.0.1=py27_0
- prompt_toolkit=1.0.14=py27_0
- psutil=5.2.1=py27_0
- ptyprocess=0.5.2=py27_0
- py=1.4.34=py27_0
- pycodestyle=2.3.1=py27_0
- pycparser=2.18=py27_0
- pyfftw=0.10.4=np113py27_1
- pyflakes=1.5.0=py27_0
- pygments=2.2.0=py27_0
- pyopenssl=16.2.0=py27_0
- pyparsing=2.2.0=py27_0
- pysocks=1.6.7=py27_0
- pytest=3.1.3=py27_0
- pytest-cov=2.5.1=py27_0
- python=2.7.13=1
- python-dateutil=2.6.1=py27_0
- python-graphviz=0.7.1=py27_0
- pytz=2017.2=py27_0
- pyyaml=3.12=py27_1
- readline=6.2=0
- requests=2.18.1=py27_0
- s3fs=0.1.2=py27_0
- s3transfer=0.1.10=py27_1
- scandir=1.5=py27_1
- scipy=0.19.1=np113py27_blas_openblas_200
- setuptools=33.1.1=py27_0
- simplegeneric=0.8.1=py27_0
- singledispatch=3.4.0.3=py27_0
- six=1.10.0=py27_1
- snowballstemmer=1.2.1=py27_0
- sortedcontainers=1.5.3=py27_0
- sphinx=1.6.3=py27_0
- sphinx_rtd_theme=0.2.4=py27_0
- sphinxcontrib-websupport=1.0.1=py27_0
- sqlite=3.13.0=1
- ssl_match_hostname=3.5.0.1=py27_1
- subprocess32=3.2.7=py27_0
- tblib=1.3.2=py27_0
- tk=8.5.19=1
- toolz=0.8.2=py27_0
- tornado=4.5.1=py27_0
- traitlets=4.3.2=py27_0
- typing=3.6.1=py27_0
- urllib3=1.21.1=py27_1
- wcwidth=0.1.7=py27_0
- wheel=0.29.0=py27_0
- xz=5.2.2=0
- yaml=0.1.6=0
- zict=0.1.2=py27_0
- zlib=1.2.11=0
- pip:
  - backports-abc==0.5
  - backports.shutil-get-terminal-size==1.0.0
  - backports.ssl-match-hostname==3.5.0.1
  - dask (/zopt/conda2/envs/daskenv/lib/python2.7/site-packages)==0.15.1
  - ipython-genutils==0.2.0
  - prompt-toolkit==1.0.14
  - sphinx-rtd-theme==0.2.4
```

</details>


Discussion:

Resolving patch:
diff --git a/dask/array/core.py b/dask/array/core.py
--- a/dask/array/core.py
+++ b/dask/array/core.py
@@ -3955,6 +3955,11 @@ def repeat(a, repeats, axis=None):
     if not isinstance(repeats, Integral):
         raise NotImplementedError("Only integer valued repeats supported")
 
+    if -a.ndim <= axis < 0:
+        axis += a.ndim
+    elif not 0 <= axis <= a.ndim - 1:
+        raise ValueError("axis(=%d) out of bounds" % axis)
+
     if repeats == 1:
         return a
 


Test patch:
diff --git a/dask/array/tests/test_array_core.py b/dask/array/tests/test_array_core.py
--- a/dask/array/tests/test_array_core.py
+++ b/dask/array/tests/test_array_core.py
@@ -2764,7 +2764,7 @@ def test_repeat():
     d = da.from_array(x, chunks=(4, 5, 3))
 
     repeats = [1, 2, 5]
-    axes = [0, 1, 2]
+    axes = [-3, -2, -1, 0, 1, 2]
 
     for r in repeats:
         for a in axes:
@@ -2781,6 +2781,10 @@ def test_repeat():
     with pytest.raises(NotImplementedError):
         da.repeat(d, 2)
 
+    for invalid_axis in [3, -4]:
+        with pytest.raises(ValueError):
+            da.repeat(d, 2, axis=invalid_axis)
+
     x = np.arange(5)
     d = da.arange(5, chunks=(2,))