`Dataset.to_array()` throws `IndexError` for empty datasets
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
>>> xr.__version__
'2023.4.2'
>>> xr.Dataset().to_array()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/mnt/qb/work2/goswami0/gkd021/conda/envs/wb/lib/python3.10/site-packages/xarray/core/dataset.py", line 6114, in to_array
data = duck_array_ops.stack([b.data for b in broadcast_vars], axis=0)
File "/mnt/qb/work2/goswami0/gkd021/conda/envs/wb/lib/python3.10/site-packages/xarray/core/duck_array_ops.py", line 326, in stack
xp = get_array_namespace(arrays[0])
IndexError: list index out of range
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:
>>> xr.DataArray()
<xarray.DataArray ()>
array(nan)
Minimal Complete Verifiable Example
No response
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
Relevant log output
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1160.76.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.1
xarray: 2023.4.2
pandas: 1.5.2
numpy: 1.23.5
scipy: 1.9.3
netCDF4: 1.6.3
pydap: None
h5netcdf: 1.1.0
h5py: 3.8.0
Nio: None
zarr: 2.14.2
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: 1.3.5
dask: 2022.7.0
distributed: 2022.7.0
matplotlib: 3.6.2
cartopy: 0.21.1
seaborn: None
numbagg: None
fsspec: 2022.11.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 65.6.3
pip: 22.3.1
conda: None
pytest: 7.1.2
mypy: None
IPython: 8.8.0
sphinx: 5.0.2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in xarray/core/dataset.py at Dataset.to_array, then reproduce the issue with xr.Dataset().to_array() using the example in the report. The change is complete when an empty Dataset returns an empty, default-constructed DataArray instead of raising IndexError, with regression coverage for that case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100