MCPcopy
hub / github.com/pydata/xarray / _as_array_or_item

Function _as_array_or_item

xarray/core/variable.py:321–344  ·  view source on GitHub ↗

Return the given values as a numpy array, or as an individual item if it's a 0d datetime64 or timedelta64 array. Importantly, this function does not copy data if it is already an ndarray - otherwise, it will not be possible to update Variable values in place. This function mostly e

(data)

Source from the content-addressed store, hash-verified

319
320
321def _as_array_or_item(data):
322 """Return the given values as a numpy array, or as an individual item if
323 it's a 0d datetime64 or timedelta64 array.
324
325 Importantly, this function does not copy data if it is already an ndarray -
326 otherwise, it will not be possible to update Variable values in place.
327
328 This function mostly exists because 0-dimensional ndarrays with
329 dtype=datetime64 are broken :(
330 https://github.com/numpy/numpy/issues/4337
331 https://github.com/numpy/numpy/issues/7619
332
333 TODO: remove this (replace with np.asarray) once these issues are fixed
334 """
335 data = np.asarray(data)
336 if data.ndim == 0:
337 kind = data.dtype.kind
338 if kind in "mM":
339 unit, _ = np.datetime_data(data.dtype)
340 if kind == "M":
341 data = np.datetime64(data, unit)
342 elif kind == "m":
343 data = np.timedelta64(data, unit)
344 return data
345
346
347class Variable(NamedArray, AbstractArray, VariableArithmetic):

Callers 1

valuesMethod · 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…