When I do numpy indexing, sometimes the index can be an empty list, in that way I want numpy to also return a empty array. For example:
a = np.array([1, 2, 3])
b = []
print a[b]
This works perfectly fine! when the result gives me:
result:[]
But when I use ndarray as indexer, strange things happened:
a = np.array([1, 2, 3])
b = []
c = np.array(b)
print a[c]
This gives me an error:
IndexError: arrays used as indices must be of integer (or boolean) type
However, When I do this:
a = np.array([1, 2, 3])
b = []
d = np.arange(0, a.size)[b]
print a[d]
Then it works perfectly again:
result:[]
But when I check the type of c and d, they returns the same! Even the shape and everything:
print type(c), c.shape
print type(d), d.shape
result:<type 'numpy.ndarray'> (0L,)
result:<type 'numpy.ndarray'> (0L,)
So I was wondering if there is anything wrong with it? How come a[c] doesn't work but a[d] works? Can you explain it for me? Thank you!