Open sehoffmann opened 1 year ago
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!
Hmm not sure that it is something related to the explicit indexes refactor?
v2022.10.0 (after the refactor) raised a slightly more meaningful error message:
>>> xr.Dataset().to_array()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[5], line 1
----> 1 xarray.Dataset().to_array()
File ~/Git/github/benbovy/xarray/xarray/core/dataset.py:6079, in Dataset.to_array(self, dim, name)
6077 data_vars = [self.variables[k] for k in self.data_vars]
6078 broadcast_vars = broadcast_variables(*data_vars)
-> 6079 data = duck_array_ops.stack([b.data for b in broadcast_vars], axis=0)
6081 dims = (dim,) + broadcast_vars[0].dims
6082 variable = Variable(dims, data, self.attrs, fastpath=True)
File ~/Git/github/benbovy/xarray/xarray/core/duck_array_ops.py:287, in stack(arrays, axis)
285 def stack(arrays, axis=0):
286 """stack() with better dtype promotion rules."""
--> 287 return _stack(as_shared_dtype(arrays), axis=axis)
File ~/Git/github/benbovy/xarray/xarray/core/duck_array_ops.py:187, in as_shared_dtype(scalars_or_arrays)
182 arrays = [asarray(x) for x in scalars_or_arrays]
183 # Pass arrays directly instead of dtypes to result_type so scalars
184 # get handled properly.
185 # Note that result_type() safely gets the dtype from dask arrays without
186 # evaluating them.
--> 187 out_type = dtypes.result_type(*arrays)
188 return [x.astype(out_type, copy=False) for x in arrays]
File ~/Git/github/benbovy/xarray/xarray/core/dtypes.py:183, in result_type(*arrays_and_dtypes)
178 if any(issubclass(t, left) for t in types) and any(
179 issubclass(t, right) for t in types
180 ):
181 return np.dtype(object)
--> 183 return np.result_type(*arrays_and_dtypes)
File <__array_function__ internals>:200, in result_type(*args, **kwargs)
ValueError: at least one array or dtype is required
What happened?
What did you expect to happen?
The most reasonable way to handle this in my opinion would be to return an empty, i.e. default constructed,
xr.DataArray
:Minimal Complete Verifiable Example
No response
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
No response
Environment