Simple Indexing#
named_arrays generalizes the numpy indexing model using dicts insted of tuples.
This means that indexing the array depends on the name of the axis instead of it’s position.
[1]:
import named_arrays as na
Say we have a given 2D array, with axis names row and col.
[2]:
ScalarArray(
ndarray=[[1, 2, 3],
[4, 5, 6]],
axes=('row', 'col'),
)
If we want to select the top-left element of a, we need to use a dict with the row and column set to zero.
It would be nice if we could use keyword arguments to __getitem__ directly, but this is not supported by Python.
See PEP 637 for more details.
[3]:
a[dict(row=0, col=0)]
[3]:
ScalarArray(ndarray=np.int64(1), axes=())
If we just want to select the first row, use a dict with only the row set to zero.
[4]:
a[dict(row=0)]
[4]:
ScalarArray(
ndarray=[1, 2, 3],
axes=('col',),
)
To select only the first two columns, we need to use a slice object.
Slicing notation is not supported.
[5]:
ScalarArray(
ndarray=[[1, 2],
[4, 5]],
axes=('row', 'col'),
)
If the axis given by the item key does not exist, it is ignored. This allows vector and function indexing to work more intuitively and efficiently.
[6]:
a[dict(foo=1)]
[6]:
ScalarArray(
ndarray=[[1, 2, 3],
[4, 5, 6]],
axes=('row', 'col'),
)