getitem#
- named_arrays.getitem(a, item)#
Recursively index an arbitrarily-nested structure with a named index.
The index
itemis applied only to the leaves ofathat are compatible with thenamed_arraysAPI (seenamed_array_like()). Containers (dict,list,tuple) anddataclassesare traversed recursively, and any other leaf value is returned unchanged.- Parameters:
a (Any) – The structure to index. May be a named array, a (possibly nested)
dict,list, ortupleof named arrays, adataclassesinstance whose fields are named arrays, or any other value (which is returned unchanged).item (dict | AbstractArray) – The named index to apply to each array-like leaf of
a, for example adictmapping axis names to index arrays.
- Returns:
A structure with the same shape as
awhere every array-like leafhas been indexed by
item.
- Return type:
Examples
import numpy as np import named_arrays as na a = na.arange(0, 5, axis="x") index = {"x": na.ScalarArray(np.array([1, 3]), axes="x")} na.getitem({"foo": a, "bar": 2 * a}, index)
{'foo': ScalarArray( ndarray=[1, 3], axes=('x',), ), 'bar': ScalarArray( ndarray=[2, 6], axes=('x',), )}Notes
A
tupleleaf is rebuilt as a plaintuple, so the subtype of atyping.NamedTupleis not preserved.