Skip to content

SerializationWarning for coordinate variables goes against CF conventions #10305

Open
@djhoese

Description

@djhoese

What happened?

When saving a variable as a NetCDF coordinate variable (in the CF sense a variable with the same name as its dimension) you get a serialization warning:

SerializationWarning: saving variable x with floating point data as an integer dtype without any _FillValue to use for NaNs

CF coordinate variables are not supposed to have a _FillValue defined:

https://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#missing-data

Missing data is not allowed in coordinate variables.

What did you expect to happen?

For variables that are CF coordinate variables I expect no warning. Or if I explicitly set the _FillValue to None then I would expect no warning.

Minimal Complete Verifiable Example

import xarray as xr
import numpy as np

data = xr.DataArray(np.zeros((2,), dtype=np.float32), dims=("x",))
data.encoding["scale_factor"] = 1.0
data.encoding["add_offset"] = 0.0
data.encoding["dtype"] = "uint16"
data.encoding["_FillValue"] = 0.0

x = xr.DataArray(np.arange(2, dtype=np.float32), dims=("x",))
x.encoding["scale_factor"] = 1.0
x.encoding["add_offset"] = 0.0
x.encoding["dtype"] = "uint16"
x.encoding["_FillValue"] = None

ds = xr.Dataset({
    "data": data,
    "x": x,
})
ds.to_netcdf("test.nc")

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

SerializationWarning: saving variable x with floating point data as an integer dtype without any _FillValue to use for NaNs

Anything else we need to know?

@kmuehlbauer added this check in #7719

This stackoverflow answer says I can set fill value to None to not have a fill value for the variable, but it doesn't seem to change anything regarding the warning:

https://stackoverflow.com/a/45696423/433202

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:24:40) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 6.12.10-76061203-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.3
libnetcdf: 4.9.2

xarray: 2025.4.0
pandas: 2.2.3
numpy: 2.2.5
scipy: 1.15.2
netCDF4: 1.7.2
pydap: None
h5netcdf: 1.6.1
h5py: 3.13.0
zarr: 2.18.7
cftime: 1.6.4
nc_time_axis: None
iris: None
bottleneck: 1.4.2
dask: 2025.4.1
distributed: 2025.4.1
matplotlib: 3.10.1
cartopy: 0.24.0
seaborn: None
numbagg: None
fsspec: 2025.3.2
cupy: None
pint: 0.24.4
sparse: None
flox: None
numpy_groupies: None
setuptools: 80.1.0
pip: 25.1
conda: 25.3.1
pytest: 8.3.5
mypy: 1.15.0
IPython: 9.2.0
sphinx: 8.2.3

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