Skip to content

xarray allows several types for netcdf attributes. Is it expected ? #7093

Open
@ghislainp

Description

@ghislainp

What is your issue?

Xarray is permissive regarding the type of the attributes. If using a wrong type, the error reveals the valid types: For serialization to netCDF files, its value must be of one of the following types: str, Number, ndarray, number, list, tuple

Using a non iterable type used to raise an Exception when reading the saved netcdf, but this is now solved with #7085

The pending question is whether it is valid to save netcdf attributes with type other than a string or not.
The following lines are working (in a notebook):

xr.DataArray([1, 2, 3], attrs={'units': 1}, name='x').to_netcdf("tmp.nc")
!ncdump tmp.nc

xr.DataArray([1, 2, 3], attrs={'units': np.nan}, name='x').to_netcdf("tmp.nc")
!ncdump tmp.nc

xr.DataArray([1, 2, 3], attrs={'units': ['xarray', 'is', 'very', 'permissive', ]}, name='x').to_netcdf("tmp.nc")
!ncdump tmp.nc

On the other hand, the following line raises an error:

xr.DataArray([1, 2, 3], attrs={'units': None}, name='x').to_netcdf("tmp.nc")

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs discussiontopic-metadataRelating to the handling of metadata (i.e. attrs and encoding)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions