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

Function last_n_items

xarray/core/formatting.py:107–130  ·  view source on GitHub ↗

Returns the last n_desired items of an array

(array, n_desired)

Source from the content-addressed store, hash-verified

105
106
107def last_n_items(array, n_desired):
108 """Returns the last n_desired items of an array"""
109 # Unfortunately, we can't just do array.flat[-n_desired:] here because it
110 # might not be a numpy.ndarray. Moreover, access to elements of the array
111 # could be very expensive (e.g. if it's only available over DAP), so go out
112 # of our way to get them in a single call to __getitem__ using only slices.
113 from xarray.core.variable import Variable
114
115 if (n_desired == 0) or (array.size == 0):
116 return []
117
118 if n_desired < array.size:
119 indexer = _get_indexer_at_least_n_items(array.shape, n_desired, from_end=True)
120 if isinstance(array, ExplicitlyIndexed):
121 indexer = BasicIndexer(indexer)
122 array = array[indexer]
123
124 # We pass variable objects in to handle indexing
125 # with indexer above. It would not work with our
126 # lazy indexing classes at the moment, so we cannot
127 # pass Variable._data
128 if isinstance(array, Variable):
129 array = array._data
130 return ravel(to_duck_array(array))[-n_desired:]
131
132
133def last_item(array):

Callers 1

format_array_flatFunction · 0.85

Calls 4

BasicIndexerClass · 0.90
ravelFunction · 0.90
to_duck_arrayFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…