-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
What happened?
With the h5netcdf engine, when a dataarray is opened but not yet loaded, then sliced such that it contains no elements, it is then in a broken state where any access to the array will cause a ValueError.
What did you expect to happen?
Access of the .values property to return an empty array.
Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# "netCDF4==1.7.2", # Importing netCDF4==1.7.3 somehow breaks the h5netcdf engine
# ]
# ///
import xarray as xr
import numpy as np
xr.show_versions()
# Works, using netcdf5 engine instead of h5netcdf engine
da = xr.DataArray(np.zeros(10))
da.to_netcdf('test.nc', engine='netcdf4')
print(xr.open_dataarray('test.nc', engine='netcdf4').isel(dim_0=[]).values)
# Works, using h5netcdf engine and ensuring data is loaded before index
da = xr.DataArray(np.zeros(10))
da.to_netcdf('test.nc', engine='h5netcdf')
print(xr.open_dataarray('test.nc', engine='h5netcdf').load().isel(dim_0=[]).values)
# Raises ValueError: zero-size array to reduction operation maximum which has no identity
da.to_netcdf('test.nc', engine='h5netcdf')
print(xr.open_dataarray('test.nc', engine='h5netcdf').isel(dim_0=[]).values)
Steps to reproduce
uv run issue.py
with included script. Note that it seems the newest version of netCDF4-python (1.7.3) introduced some sort of breaking change with xarray and the h5netcdf engine does not work if it's been imported and I've pinned it to 1.7.2 in the above script.
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.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
[]
[]
Traceback (most recent call last):
File "/home/rbelter/Documents/OMS/pygems-processing-2/test.py", line 27, in <module>
print(xr.open_dataarray('test.nc', engine='h5netcdf').isel(dim_0=[]).values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/dataarray.py", line 798, in values
return self.variable.values
^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/variable.py", line 556, in values
return _as_array_or_item(self._data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/variable.py", line 336, in _as_array_or_item
data = np.asarray(data)
^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/indexing.py", line 577, in __array__
return np.asarray(self.get_duck_array(), dtype=dtype, copy=copy)
^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/indexing.py", line 943, in get_duck_array
duck_array = self.array.get_duck_array()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/indexing.py", line 897, in get_duck_array
return self.array.get_duck_array()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/indexing.py", line 737, in get_duck_array
array = self.array[self.key]
~~~~~~~~~~^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/backends/h5netcdf_.py", line 67, in __getitem__
return indexing.explicit_indexing_adapter(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/indexing.py", line 1128, in explicit_indexing_adapter
raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/indexing.py", line 1178, in decompose_indexer
return _decompose_outer_indexer(indexer, shape, indexing_support)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/xarray/core/indexing.py", line 1368, in _decompose_outer_indexer
(np.max(k) - np.min(k) + 1.0) / len(np.unique(k))
^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/numpy/_core/fromnumeric.py", line 3164, in max
return _wrapreduction(a, np.maximum, 'max', axis, None, out,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rbelter/.cache/uv/archive-v0/Xb5P1rZOgF_J9Dg-HT-jN/lib/python3.12/site-packages/numpy/_core/fromnumeric.py", line 86, in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: zero-size array to reduction operation maximum which has no identity
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 6.11.0-29-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.4-development
xarray: 2025.10.2.dev13+gb5e4b0e02
pandas: 2.3.3
numpy: 2.3.4
scipy: 1.16.2
netCDF4: 1.7.2
pydap: 3.5.8
h5netcdf: 1.7.2
h5py: 3.15.1
zarr: 3.1.3
cftime: 1.6.5
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.6.0
dask: 2025.10.0
distributed: 2025.10.0
matplotlib: 3.10.7
cartopy: 0.25.0
seaborn: 0.13.2
numbagg: 0.9.3
fsspec: 2025.9.0
cupy: None
pint: None
sparse: 0.17.0
flox: 0.10.7
numpy_groupies: 0.11.3
setuptools: None
pip: None
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None