In numpy, what does indexing an array with the empty tuple vs. ellipsis do?

The treatment of A[...] is a special case, optimised to always return A itself:

if (op == Py_Ellipsis) {
    Py_INCREF(self);
    return (PyObject *)self;
}

Anything else that should be equivalent e.g. A[:], A[(Ellipsis,)], A[()], A[(slice(None),) * A.ndim] will instead return a view of the entirety of A, whose base is A:

>>> A[()] is A
False
>>> A[()].base is A
True

This seems an unnecessary and premature optimisation, as A[(Ellipsis,)] and A[()] will always give the same result (an entire view on A). From looking at https://github.com/numpy/numpy/commit/fa547b80f7035da85f66f9cbabc4ff75969d23cd it seems that it was originally required because indexing with ... didn't work properly on 0d arrays (previously to https://github.com/numpy/numpy/commit/4156b241aa3670f923428d4e72577a9962cdf042 it would return the element as a scalar), then extended to all arrays for consistency; since then, indexing has been fixed on 0d arrays so the optimisation isn't required, but it's managed to stick around vestigially (and there's probably some code that depends on A[...] is A being true).


According to the official Numpy documentation, the differences is clear:

An empty (tuple) index is a full scalar index into a zero dimensional array. x[()] returns a scalar if x is zero dimensional and a view otherwise. On the other hand x[...] always returns a view.

When an ellipsis (...) is present but has no size (i.e. replaces zero :) the result will still always be an array. A view if no advanced index is present, otherwise a copy.

>>> import numpy as np
>>> # ---------------------------------- #
>>> # when `x` is at least 1 dimensional #
>>> # ---------------------------------- #
>>> x = np.linspace(0, 10, 100)
>>> x.shape
(100,)
>>> x.ndim
1
>>> a = x[()]
>>> b = x[...]
>>> id(x), id(a), id(b)
(4559933568, 4561560080, 4585410192)
>>> id(x.base), id(a.base), id(b.base)
(4560914432, 4560914432, 4560914432)
>>> # ---------------------------- #
>>> # when `z` is zero dimensional #
>>> # ---------------------------- #
>>> z = np.array(3.14)
>>> z.shape
()
>>> z.ndim
0
>>> a = z[()]
>>> b = z[...]
>>> type(a), type(b)
(<class 'numpy.float64'>, <class 'numpy.ndarray'>)
>>> id(z), id(a), id(b)
(4585422896, 4586829384, 4561560080)
>>> id(z.base), id(a.base), id(b.base)
(4557260904, 4557260904, 4585422896)
>>> b.base is z
True

While in the example you've given, the empty tuple and ellipsis give a similar result, in general they serve different purposes. When indexing an array, A[i, j, k] == A[(i, j, k)] and specifically A[...] == A[(Ellipsis,)]. Here the tuple simply serves as a container for indexing elements. This can be useful when you need to manipulate the index as a variable, for example you can do:

index = (0,) * A.ndim
A[index]

Notice that because the tuple is the container for indexing elements, it cannot be combined with other indices, for example A[(), 0] == A[[], 0] and A[(), 0] != A[..., 0].

Because an array A can be indexed with fewer indices than A.ndim, indexing with an empty tuple is a natural extension of that behavior and it can be useful in some situations, for example the above code snipit will work when A.ndim == 0.

In short, the tuple serves as a container for indexing elements, which is allowed to be empty, while the Ellipsis is one of the possible indexing elements.