pydata / pydata/xarray

DataArray attributes not present in DataSet. Coherency problem between DataSet and NetCDF file

Open
#5,208 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

usage question
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.