Skip to content

Can't read zero-size netCDF timedelta array #10310

Closed
@deepers

Description

@deepers

What happened?

When I write a zero-sized timedelta DataArray to a netCDF file, I can't read it back again.

What did you expect to happen?

I should be able to read back the array that was serialized to disk.

Minimal Complete Verifiable Example

import numpy as np
import xarray as xr
da = xr.DataArray(np.array([], dtype='timedelta64[ns]'))
da.to_netcdf('/tmp/foo.nc')
xr.load_dataarray('/tmp/foo.nc', decode_timedelta=True)

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 "/tmp/xarray-bug.py", line 5, in <module>
    xr.load_dataarray('/tmp/foo.nc', decode_timedelta=True)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/backends/api.py", line 336, in load_dataarray
    return da.load()
           ~~~~~~~^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/core/dataarray.py", line 1175, in load
    ds = self._to_temp_dataset().load(**kwargs)
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/core/dataset.py", line 551, in load
    v.load()
    ~~~~~~^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/core/variable.py", line 956, in load
    self._data = to_duck_array(self._data, **kwargs)
                 ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/namedarray/pycompat.py", line 143, in to_duck_array
    return data.get_duck_array()  # type: ignore[no-untyped-call, no-any-return]
           ~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/core/indexing.py", line 837, in get_duck_array
    self._ensure_cached()
    ~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/core/indexing.py", line 834, in _ensure_cached
    self.array = as_indexable(self.array.get_duck_array())
                              ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/core/indexing.py", line 791, in get_duck_array
    return self.array.get_duck_array()
           ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/core/indexing.py", line 661, in get_duck_array
    array = array.get_duck_array()
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/coding/common.py", line 76, in get_duck_array
    return self.func(self.array.get_duck_array())
           ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/xarray/coding/times.py", line 664, in decode_cf_timedelta
    _check_timedelta_range(np.nanmin(num_timedeltas), unit, time_unit)
                           ~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/tmp/.venv/xarray-bug/lib64/python3.13/site-packages/numpy/lib/_nanfunctions_impl.py", line 357, in nanmin
    res = np.fmin.reduce(a, axis=axis, out=out, **kwargs)
ValueError: zero-size array to reduction operation fmin which has no identity

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.13.3 (main, Apr 22 2025, 00:00:00) [GCC 15.0.1 20250418 (Red Hat 15.0.1-0)]
python-bits: 64
OS: Linux
OS-release: 6.14.4-300.fc42.x86_64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: ('en_CA', 'UTF-8')
libhdf5: None
libnetcdf: None

xarray: 2025.4.0
pandas: 2.2.3
numpy: 2.2.5
scipy: 1.15.3
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: 24.3.1
conda: None
pytest: None
mypy: None
IPython: 9.2.0
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions