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

Function to_numpy

xarray/namedarray/pycompat.py:99–127  ·  view source on GitHub ↗
(
    data: duckarray[Any, Any], **kwargs: dict[str, Any]
)

Source from the content-addressed store, hash-verified

97
98
99def to_numpy(
100 data: duckarray[Any, Any], **kwargs: dict[str, Any]
101) -> np.ndarray[Any, np.dtype[Any]]:
102 from xarray.core.indexing import ExplicitlyIndexed
103 from xarray.namedarray.parallelcompat import get_chunked_array_type
104
105 try:
106 # for tests only at the moment
107 return data.to_numpy() # type: ignore[no-any-return,union-attr]
108 except AttributeError:
109 pass
110
111 if isinstance(data, ExplicitlyIndexed):
112 data = data.get_duck_array() # type: ignore[no-untyped-call]
113
114 # TODO first attempt to call .to_numpy() once some libraries implement it
115 if is_chunked_array(data):
116 chunkmanager = get_chunked_array_type(data)
117 data, *_ = chunkmanager.compute(data, **kwargs)
118 if isinstance(data, array_type("cupy")):
119 data = data.get()
120 # pint has to be imported dynamically as pint imports xarray
121 if isinstance(data, array_type("pint")):
122 data = data.magnitude
123 if isinstance(data, array_type("sparse")):
124 data = data.todense()
125 data = np.asarray(data)
126
127 return data
128
129
130def to_duck_array(data: Any, **kwargs: dict[str, Any]) -> duckarray[_ShapeType, _DType]:

Callers 6

decode_cf_datetimeFunction · 0.90
decode_cf_timedeltaFunction · 0.90
to_numpyMethod · 0.90
dropnaMethod · 0.90
safe_cast_to_indexFunction · 0.90

Calls 8

get_chunked_array_typeFunction · 0.90
is_chunked_arrayFunction · 0.85
array_typeFunction · 0.85
to_numpyMethod · 0.45
get_duck_arrayMethod · 0.45
computeMethod · 0.45
getMethod · 0.45
todenseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…