Repository: numpy/numpy

Issue:
ix_() returned array has dtype=float64 when given empty range
```
>>> np.ix_(range(0,0))
(array([], dtype=float64),)
```

The correct dtype should be int32, since float indices are usually not accepted as indices in numpy arrays. That dtype is returned for non-empty ranges:

```
>>> np.ix_(range(0,1))[0].dtype
dtype('int32')
```



Discussion:

Resolving patch:
diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py
--- a/numpy/lib/index_tricks.py
+++ b/numpy/lib/index_tricks.py
@@ -7,7 +7,7 @@
 from numpy.core.numeric import (
     asarray, ScalarType, array, alltrue, cumprod, arange
     )
-from numpy.core.numerictypes import find_common_type
+from numpy.core.numerictypes import find_common_type, issubdtype
 
 from . import function_base
 import numpy.matrixlib as matrix
@@ -71,17 +71,17 @@ def ix_(*args):
     """
     out = []
     nd = len(args)
-    baseshape = [1]*nd
-    for k in range(nd):
-        new = _nx.asarray(args[k])
-        if (new.ndim != 1):
+    for k, new in enumerate(args):
+        new = asarray(new)
+        if new.ndim != 1:
             raise ValueError("Cross index must be 1 dimensional")
-        if issubclass(new.dtype.type, _nx.bool_):
-            new = new.nonzero()[0]
-        baseshape[k] = len(new)
-        new = new.reshape(tuple(baseshape))
+        if new.size == 0:
+            # Explicitly type empty arrays to avoid float default
+            new = new.astype(_nx.intp)
+        if issubdtype(new.dtype, _nx.bool_):
+            new, = new.nonzero()
+        new.shape = (1,)*k + (new.size,) + (1,)*(nd-k-1)
         out.append(new)
-        baseshape[k] = 1
     return tuple(out)
 
 class nd_grid(object):


Test patch:
diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py
--- a/numpy/lib/tests/test_index_tricks.py
+++ b/numpy/lib/tests/test_index_tricks.py
@@ -7,7 +7,7 @@
     )
 from numpy.lib.index_tricks import (
     mgrid, ndenumerate, fill_diagonal, diag_indices, diag_indices_from,
-    index_exp, ndindex, r_, s_
+    index_exp, ndindex, r_, s_, ix_
     )
 
 
@@ -169,6 +169,34 @@ def test_simple_1(self):
         assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]])
 
 
+class TestIx_(TestCase):
+    def test_regression_1(self):
+        # Test empty inputs create ouputs of indexing type, gh-5804
+        # Test both lists and arrays
+        for func in (range, np.arange):
+            a, = np.ix_(func(0))
+            assert_equal(a.dtype, np.intp)
+
+    def test_shape_and_dtype(self):
+        sizes = (4, 5, 3, 2)
+        # Test both lists and arrays
+        for func in (range, np.arange):
+            arrays = np.ix_(*[func(sz) for sz in sizes])
+            for k, (a, sz) in enumerate(zip(arrays, sizes)):
+                assert_equal(a.shape[k], sz)
+                assert_(all(sh == 1 for j, sh in enumerate(a.shape) if j != k))
+                assert_(np.issubdtype(a.dtype, int))
+
+    def test_bool(self):
+        bool_a = [True, False, True, True]
+        int_a, = np.nonzero(bool_a)
+        assert_equal(np.ix_(bool_a)[0], int_a)
+
+    def test_1d_only(self):
+        idx2d = [[1, 2, 3], [4, 5, 6]]
+        assert_raises(ValueError, np.ix_, idx2d)
+
+
 def test_c_():
     a = np.c_[np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])]
     assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]])