ndindex

The primary entry-point to the ndindex API is the ndindex() function, which converts Python index objects into ndindex objects.

ndindex.ndindex(obj)

Convert an object into an ndindex type.

Invalid indices will raise IndexError, TypeError, or ValueError (generally, the same error NumPy would raise if the index were used on an array).

Indices are created by calling the ndindex with raw index objects:

>>> from ndindex import ndindex
>>> ndindex(slice(0, 10))
Slice(0, 10, None)
>>> ndindex((slice(0, 10), 0))
Tuple(slice(0, 10, None), 0)

Indices can also be created by calling ndindex with getitem syntax.

>>> ndindex[1]
Integer(1)
>>> ndindex[0:10]
Slice(0, 10, None)
>>> ndindex(0:10)
Traceback (most recent call last):
...
    ndindex(0:10)
             ^
SyntaxError: invalid syntax

The ndindex[idx] form should generally be preferred when creating an index from a tuple or slice literal, since ndindex(a:b) is not syntactically valid and must be typed as ndindex(slice(a, b)). Additionally, the ndindex[idx] syntax does not require parentheses when creating a tuple index:

>>> ndindex[0, 1]
Tuple(0, 1)
>>> ndindex(0, 1) 
Traceback (most recent call last):
...
TypeError: NDIndexConstructor.__call__() takes 2 positional arguments but 3 were given
>>> ndindex((0, 1))
Tuple(0, 1)