This behaviour is explained by the fact that, as opposed to indexing with a slice, integer indexing with say i
, will return the same values as a slice i:i+1
but with the dimensionality of the returned object reduced by 1
. This is explained in the docs:
In particular, a selection tuple with the p-th element an integer (and all other entries :) returns the corresponding sub-array with dimension N - 1
We could write a simple subclass to take a closer look at how np.ndarray
handles indexing, and see what the __getitem__
dunder is receiving in each call:
class ndarray_getitem_print(np.ndarray):
def __getitem__(self, t):
print(t)
return super().__getitem__(t)
Now let's instanciate ndarray_getitem_print
and see what are the differences when indexing with a slice and an integer:
a = ndarray_getitem_print((5,5))
a[:,0:1]
(slice(None, None, None), slice(0, 1, None))
(-5, -1)
(-4, -1)
(-3, -1)
(-2, -1)
(-1, -1)
ndarray_getitem_print([[1.],
[1.],
[1.],
[1.],
[1.]])
Whereas indexing along the second axis with a 0
, will be producing an output ndarray where each item has a one dimensional shape, i.e (-k,)
a[:,0]
(slice(None, None, None), 0)
(-5,)
(-4,)
(-3,)
(-2,)
(-1,)
ndarray_getitem_print([1., 1., 1., 1., 1.])