DataArray attributes not present in DataSet. Coherency problem between DataSet and NetCDF file
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
When I create a DataSet from DataArrays, attributes are lost.
When are create attributes in a DataSet, they are know shown by print (DataSet), but are written in the NetCDF file.
Below is python code showing the xarray behaviour in details.
My requests :
- When creating a DataSet from DataArrays, DataArrays attributes should be incorporated in the DataSet. (maybe optional)
- Attributes present in a DataSet should appear with a
print (DataSet). Like for DataArrays.
Thanks,
Olivier
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import xarray as xr
# Creates DataArrays
nt = 4
time = np.arange (nt) * 86400.0
time = xr.DataArray (time, coords=[time,], dims=["time",])
aa = time * 2.0
# Adding attributes to DataArrays
time.attrs['units'] = "second"
aa.attrs['units'] = "whatever"
# Attributes are visible in the DataArrays
print ('----------> time DataArray: ')
print (time)
print ('----------> aa DataArray : ' )
print (aa)
print ('----------> aa attributes : ')
print (aa.attrs )
# Creating a Dataset
ds = xr.Dataset(
{ "aa": (["time",], aa), },
coords={"time": (["time",], time), }, )
# Attributes are not visible in the Dataset
print ('----------> DataSet before setting attributes')
print (ds)
# My request #1 : attributes of the DataArrays should be added to the DataSet (may be optional)
print ('----------> Attributes of aa in DataSet : none')
print ( ds['aa'].attrs )
print ('----------> Attributes of aa outside DataSet : still here')
print ( aa.attrs )
print ('----------> Attributes are not written to the NetCDF file')
ds.to_netcdf ('sample1.nc')
# Adding attributes directly to the Dataset
ds['time'].attrs['units'] = "second"
ds['aa'].attrs['units'] = "whatever"
# Attributes are still not visible in the Dataset
print ('----------> DataSet after setting attributes : attributes not shown' )
print (ds)
# My request #2 : attributes added to the DataSet should be printed
print ('----------> But they are written in the NetCDF file')
ds.to_netcdf ('sample2.nc')
# MyRequest : coherency between the DataSet and the NetCDF file
# What if I read a NetCDF file
dt = xr.open_dataset ( 'sample2.nc')
print ('----------> DataSet read in a NetCDF file : Attributes are not shown')
print (dt)
print ('----------> Attributes of aa in DataSet : present')
print ( dt['aa'].attrs )
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied Python reproducer, focusing on xr.Dataset construction, Dataset printing, and to_netcdf. Trace how DataArray attributes are handled when inserted into a Dataset and how Dataset repr displays attributes. Done means the expected attribute propagation and display behavior are defined and covered consistently with NetCDF serialization.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100