Skip to content

potential netcdf4 backend bug involving coordinate collapse with Dataset.isel #11601

Description

@rorlija1

What happened?

Uploading time-sequential xarray datasets to zarr, my group sometimes uses datasets with no time steps in the time axis to ensure correct sizing and variables consistent between the uploaded dataset and the existing zarr store. However, when saving an xarray dataset as an intermediate netcdf before uploading and then re-opening this netcdf with xarray, we see unexpected behavior of Dataset.isel().

What did you expect to happen?

The range coordinate is collapsed into size 1 from size 100 when opening via .isel(time=[]).

Minimal Complete Verifiable Example

# /// script
# requires-python = ">=3.14"
# dependencies = [
#     "netcdf4>=1.7.4",
#     "xarray>=2026.7.0",
# ]
# ///
# Please delete this header if you have _not_ tested this script with `uv run`!

from pathlib import Path
from tempfile import TemporaryDirectory
import numpy as np
import xarray as xr
from xarray.testing import assert_identical

xr.show_versions()

ds = xr.Dataset(
        {
            "LDR": (("time", "range"), np.full((24 * 60, 100), 7.0)),
            "Z": (("time", "range"), np.full((24 * 60, 100), 8.0)),
            "pow": (("time",), np.full((24 * 60), 9.0)),
        },
        coords={
            "time": (
                ("time",),
                np.datetime64("2020-01-01")
                + np.timedelta64(60, "s") * np.arange(24 * 60),
            ),
            "range": (("range",), np.arange(100)),
            "sensor_alt": ((), 20.0),
            "lat": ((), 10.0),
            "lon": ((), 20.0),
        },
    )

with TemporaryDirectory() as dir:
    fp = Path(dir) / "test.nc"
    ds.to_netcdf(fp)
    nc = xr.open_dataset(fp, engine='netcdf4')
    assert_identical(ds.isel(time=[]), ds.isel(time=slice(0)))
    assert_identical(nc.isel(time=[]), nc.isel(time=slice(0)))

Steps to reproduce

uv run ...

Relevant log output

Traceback (most recent call last):
  File "/Users/rowan/Desktop/work/bco/xarray-bug.py", line 40, in <module>
    assert_identical(nc.isel(time=[]), nc.isel(time=slice(0)))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rowan/.cache/uv/environments-v2/xarray-bug-ca39439d02a1cdc1/lib/python3.14/site-packages/xarray/testing/assertions.py", line 33, in wrapper
    return func(*args, **kwargs)
  File "/Users/rowan/.cache/uv/environments-v2/xarray-bug-ca39439d02a1cdc1/lib/python3.14/site-packages/xarray/testing/assertions.py", line 200, in assert_identical
    assert a.identical(b), formatting.diff_dataset_repr(a, b, "identical")
           ~~~~~~~~~~~^^^
AssertionError: Left and right Dataset objects are not identical
Differing data variables:
L   LDR         (time, range) float64 0B 
R   LDR         (time, range) float64 0B 
L   Z           (time, range) float64 0B 
R   Z           (time, range) float64 0B

Anything else we need to know?

No response

Environment

Details

INSTALLED VERSIONS

commit: None
python: 3.14.2 (main, Jan 27 2026, 23:32:07) [Clang 21.1.4 ]
python-bits: 64
OS: Darwin
OS-release: 25.6.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('C', 'UTF-8')
libhdf5: 1.14.6
libnetcdf: 4.9.3

xarray: 2026.7.0
pandas: 3.0.6
numpy: 2.5.3
scipy: None
netCDF4: 1.7.4
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: 1.6.5
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: None
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions