MCPcopy Index your code
hub / github.com/pydata/xarray / expanded_indexer

Function expanded_indexer

xarray/core/indexing.py:231–258  ·  view source on GitHub ↗

Given a key for indexing an ndarray, return an equivalent key which is a tuple with length equal to the number of dimensions. The expansion is done by replacing all `Ellipsis` items with the right number of full slices and then padding the key with full slices so that it reaches the

(key, ndim)

Source from the content-addressed store, hash-verified

229
230
231def expanded_indexer(key, ndim):
232 """Given a key for indexing an ndarray, return an equivalent key which is a
233 tuple with length equal to the number of dimensions.
234
235 The expansion is done by replacing all `Ellipsis` items with the right
236 number of full slices and then padding the key with full slices so that it
237 reaches the appropriate dimensionality.
238 """
239 if not isinstance(key, tuple):
240 # numpy treats non-tuple keys equivalent to tuples of length 1
241 key = (key,)
242 new_key = []
243 # handling Ellipsis right is a little tricky, see:
244 # https://numpy.org/doc/stable/reference/arrays.indexing.html#advanced-indexing
245 found_ellipsis = False
246 for k in key:
247 if k is Ellipsis:
248 if not found_ellipsis:
249 new_key.extend((ndim + 1 - len(key)) * [slice(None)])
250 found_ellipsis = True
251 else:
252 new_key.append(slice(None))
253 else:
254 new_key.append(k)
255 if len(new_key) > ndim:
256 raise IndexError("too many indices")
257 new_key.extend((ndim - len(new_key)) * [slice(None)])
258 return tuple(new_key)
259
260
261def normalize_slice(sl: slice, size: int) -> slice:

Callers 4

__getitem__Method · 0.85
_updated_keyMethod · 0.85
_oindex_getMethod · 0.85
_vindex_getMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…