You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This code fails with error:
"TypeError: cannot directly convert an xarray.Dataset into a numpy array. Instead, create an xarray.DataArray first, either with indexing on the Dataset or by invoking the to_dataarray() method."
xarray.Dataset should not care about being put into numpy.ndarray if dtype=object.
Reason
Using np.array([ <whatever> ], dtype=object) should work for any objects put into a numpy array.
Assignment via index is possible (see above). Hence, this behavior is inconsistent.
Application
I came across this when using xarray.DataArray to store results from ray tracing. The data contains (among other data) points where rays have been reflected or diffracted. The number of points is variable. So I wanted to store the information of the reflection/refraction points into a separate xarray.Dataset and store this in an xarray.DataArray with dtype=object (via detour of a numpy array).
Workaround
I currently work around this limitation by subclassing xarray.Dataset:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Situation
xarray.Dataset
explicitly restricts being put into anumpy.ndarray
even if one setsdtype=object
:This code fails with error:
"TypeError: cannot directly convert an xarray.Dataset into a numpy array. Instead, create an xarray.DataArray first, either with indexing on the Dataset or by invoking the
to_dataarray()
method."However, this works:
Proposal:
xarray.Dataset
should not care about being put intonumpy.ndarray
ifdtype=object
.Reason
np.array([ <whatever> ], dtype=object)
should work for any objects put into a numpy array.Application
I came across this when using
xarray.DataArray
to store results from ray tracing. The data contains (among other data) points where rays have been reflected or diffracted. The number of points is variable. So I wanted to store the information of the reflection/refraction points into a separatexarray.Dataset
and store this in anxarray.DataArray
withdtype=object
(via detour of a numpy array).Workaround
I currently work around this limitation by subclassing
xarray.Dataset
:Beta Was this translation helpful? Give feedback.
All reactions