Description
What happened?
Writing data to netCDF succeeds, but decoding appears to always fail.
To reproduce:
import xarray
import pandas as pd
ds = xarray.Dataset({'lead_time': pd.timedelta_range('0h', '12h', freq='h', unit='s')})
ds.to_netcdf('on-disk2.nc', format='NETCDF4')
xarray.open_dataset('on-disk2.nc')
Raises:
ValueError: Failed to decode variable 'lead_time': failed to prevent overwriting existing key dtype in attrs on variable 'lead_time'. This is probably an encoding field used by xarray to describe how a variable is serialized. To proceed, remove this key from the variable's attributes manually.
Full traceback:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[/usr/local/lib/python3.11/dist-packages/xarray/conventions.py](https://localhost:8080/#) in decode_cf_variables(variables, attributes, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables, use_cftime, decode_timedelta)
403 try:
--> 404 new_vars[k] = decode_cf_variable(
405 k,
8 frames
[/usr/local/lib/python3.11/dist-packages/xarray/conventions.py](https://localhost:8080/#) in decode_cf_variable(name, var, concat_characters, mask_and_scale, decode_times, decode_endianness, stack_char_dim, use_cftime, decode_timedelta)
213 )
--> 214 var = decode_timedelta.decode(var, name=name)
215 if decode_times:
[/usr/local/lib/python3.11/dist-packages/xarray/coding/times.py](https://localhost:8080/#) in decode(self, variable, name)
1511 )
-> 1512 dtype = pop_to(attrs, encoding, "dtype", name=name)
1513 dtype = np.dtype(dtype)
[/usr/local/lib/python3.11/dist-packages/xarray/coding/common.py](https://localhost:8080/#) in pop_to(source, dest, key, name)
126 if value is not None:
--> 127 safe_setitem(dest, key, value, name=name)
128 return value
[/usr/local/lib/python3.11/dist-packages/xarray/coding/common.py](https://localhost:8080/#) in safe_setitem(dest, key, value, name)
107 var_str = f" on variable {name!r}" if name else ""
--> 108 raise ValueError(
109 f"failed to prevent overwriting existing key {key} in attrs{var_str}. "
ValueError: failed to prevent overwriting existing key dtype in attrs on variable 'lead_time'. This is probably an encoding field used by xarray to describe how a variable is serialized. To proceed, remove this key from the variable's attributes manually.
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
[/tmp/ipython-input-5-3486297382.py](https://localhost:8080/#) in <cell line: 0>()
4 ds = xarray.Dataset({'lead_time': pd.timedelta_range('0h', '12h', freq='h', unit='s')})
5 ds.to_netcdf('on-disk2.nc', format='NETCDF4')
----> 6 xarray.open_dataset('on-disk2.nc')
[/usr/local/lib/python3.11/dist-packages/xarray/backends/api.py](https://localhost:8080/#) in open_dataset(filename_or_obj, engine, chunks, cache, decode_cf, mask_and_scale, decode_times, decode_timedelta, use_cftime, concat_characters, decode_coords, drop_variables, inline_array, chunked_array_type, from_array_kwargs, backend_kwargs, **kwargs)
685
686 overwrite_encoded_chunks = kwargs.pop("overwrite_encoded_chunks", None)
--> 687 backend_ds = backend.open_dataset(
688 filename_or_obj,
689 drop_variables=drop_variables,
[/usr/local/lib/python3.11/dist-packages/xarray/backends/netCDF4_.py](https://localhost:8080/#) in open_dataset(self, filename_or_obj, mask_and_scale, decode_times, concat_characters, decode_coords, drop_variables, use_cftime, decode_timedelta, group, mode, format, clobber, diskless, persist, auto_complex, lock, autoclose)
679 store_entrypoint = StoreBackendEntrypoint()
680 with close_on_error(store):
--> 681 ds = store_entrypoint.open_dataset(
682 store,
683 mask_and_scale=mask_and_scale,
[/usr/local/lib/python3.11/dist-packages/xarray/backends/store.py](https://localhost:8080/#) in open_dataset(self, filename_or_obj, mask_and_scale, decode_times, concat_characters, decode_coords, drop_variables, use_cftime, decode_timedelta)
45 encoding = filename_or_obj.get_encoding()
46
---> 47 vars, attrs, coord_names = conventions.decode_cf_variables(
48 vars,
49 attrs,
[/usr/local/lib/python3.11/dist-packages/xarray/conventions.py](https://localhost:8080/#) in decode_cf_variables(variables, attributes, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables, use_cftime, decode_timedelta)
413 )
414 except Exception as e:
--> 415 raise type(e)(f"Failed to decode variable {k!r}: {e}") from e
416 if decode_coords in [True, "coordinates", "all"]:
417 var_attrs = new_vars[k].attrs
ValueError: Failed to decode variable 'lead_time': failed to prevent overwriting existing key dtype in attrs on variable 'lead_time'. This is probably an encoding field used by xarray to describe how a variable is serialized. To proceed, remove this key from the variable's attributes manually.
This appears to be an unintended consequence of #10101
What did you expect to happen?
This should succeed. After all, Xarray wrote the data in the first place!
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.
Environment
INSTALLED VERSIONS
commit: None
python: 3.11.13 (main, Jun 4 2025, 08:57:29) [GCC 11.4.0]
python-bits: 64
OS: Linux
OS-release: 6.1.123+
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.6
libnetcdf: None
xarray: 2025.6.1
pandas: 2.2.2
numpy: 2.0.2
scipy: 1.15.3
netCDF4: None
pydap: None
h5netcdf: 1.6.1
h5py: 3.14.0
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: 1.4.2
dask: 2024.12.1
distributed: 2024.12.1
matplotlib: 3.10.0
cartopy: None
seaborn: 0.13.2
numbagg: None
fsspec: 2025.3.2
cupy: 13.3.0
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.2.0
pip: 24.1.2
conda: None
pytest: 8.3.5
mypy: None
IPython: 7.34.0
sphinx: 8.2.3