@@ -679,6 +679,8 @@ def as_indexable(array):
679679 return DaskIndexingAdapter (array )
680680 if hasattr (array , "__array_function__" ):
681681 return NdArrayLikeIndexingAdapter (array )
682+ if hasattr (array , "__array_namespace__" ):
683+ return ArrayApiIndexingAdapter (array )
682684
683685 raise TypeError (f"Invalid array type: { type (array )} " )
684686
@@ -1288,6 +1290,49 @@ def __init__(self, array):
12881290 self .array = array
12891291
12901292
1293+ class ArrayApiIndexingAdapter (ExplicitlyIndexedNDArrayMixin ):
1294+ """Wrap an array API array to use explicit indexing."""
1295+
1296+ __slots__ = ("array" ,)
1297+
1298+ def __init__ (self , array ):
1299+ if not hasattr (array , "__array_namespace__" ):
1300+ raise TypeError (
1301+ "ArrayApiIndexingAdapter must wrap an object that "
1302+ "implements the __array_namespace__ protocol"
1303+ )
1304+ self .array = array
1305+
1306+ def __getitem__ (self , key ):
1307+ if isinstance (key , BasicIndexer ):
1308+ return self .array [key .tuple ]
1309+ elif isinstance (key , OuterIndexer ):
1310+ # manual orthogonal indexing (implemented like DaskIndexingAdapter)
1311+ key = key .tuple
1312+ value = self .array
1313+ for axis , subkey in reversed (list (enumerate (key ))):
1314+ value = value [(slice (None ),) * axis + (subkey , Ellipsis )]
1315+ return value
1316+ else :
1317+ if isinstance (key , VectorizedIndexer ):
1318+ raise TypeError ("Vectorized indexing is not supported" )
1319+ else :
1320+ raise TypeError (f"Unrecognized indexer: { key } " )
1321+
1322+ def __setitem__ (self , key , value ):
1323+ if isinstance (key , (BasicIndexer , OuterIndexer )):
1324+ self .array [key .tuple ] = value
1325+ else :
1326+ if isinstance (key , VectorizedIndexer ):
1327+ raise TypeError ("Vectorized indexing is not supported" )
1328+ else :
1329+ raise TypeError (f"Unrecognized indexer: { key } " )
1330+
1331+ def transpose (self , order ):
1332+ xp = self .array .__array_namespace__ ()
1333+ return xp .permute_dims (self .array , order )
1334+
1335+
12911336class DaskIndexingAdapter (ExplicitlyIndexedNDArrayMixin ):
12921337 """Wrap a dask array to support explicit indexing."""
12931338
0 commit comments