From ca6b96ebd1aa456f19f03eb209f100a5a99331f0 Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Thu, 2 Apr 2026 13:32:13 +0200 Subject: [PATCH] Do not crash on arrays of strings --- lib/ncdata/dataset_like.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/ncdata/dataset_like.py b/lib/ncdata/dataset_like.py index 3702f95..d8c2bf0 100644 --- a/lib/ncdata/dataset_like.py +++ b/lib/ncdata/dataset_like.py @@ -281,21 +281,27 @@ def _get_scaling(self): add_offset = np.array(0, dtype=self.dtype) return is_scaled, scale_factor, add_offset - def _get_fillvalue(self): + def _get_fillvalue(self) -> int | float | None: """ Calculate any applicable fill-value from attributes and netCDF4 defaults. Notes ----- * this must be checked dynamically, as the attributes could change. - * for byte data, there is no netCDF default fill, so the result can be None. + * a default fill-value is not defined for all numpy dtypes, so the + * result can be None. """ fv = self._ncdata.avals.get("_FillValue", None) - if fv is None: - if self.dtype.itemsize != 1: - # NOTE: single-byte types have NO default fill-value - dtype_code = self.dtype.str[1:] - fv = netCDF4.default_fillvals[dtype_code] + if fv is None and self.dtype.itemsize != 1: + # NOTE: from https://docs.unidata.ucar.edu/netcdf-c/4.9.2/attribute_conventions.html + # It is not necessary to define your own _FillValue attribute for a + # variable if the default fill value for the type of the variable + # is adequate. However, use of the default fill value for data type + # byte is not recommended. + # NOTE: xarray does not use default fill-values: + # https://github.com/pydata/xarray/issues/2742 + dtype_code = self.dtype.str[1:] + fv = netCDF4.default_fillvals.get(dtype_code) return fv def _maskandscale_inner_to_apparent(self, array):