adding attributes and metadata to a dataset using xarray

Solutions on MaxInterview for adding attributes and metadata to a dataset using xarray by the best coders in the world

showing results for - "adding attributes and metadata to a dataset using xarray"
Julieta
22 Apr 2019
1In [1]: data = np.random.rand(4, 3)
2
3In [2]: locs = ['IA', 'IL', 'IN']
4
5In [3]: times = pd.date_range('2000-01-01', periods=4)
6
7In [4]: foo = xr.DataArray(data, coords=[times, locs], dims=['time', 'space'])
8
9In [5]: foo
10Out[5]: 
11<xarray.DataArray (time: 4, space: 3)>
12array([[0.127, 0.967, 0.26 ],
13       [0.897, 0.377, 0.336],
14       [0.451, 0.84 , 0.123],
15       [0.543, 0.373, 0.448]])
16Coordinates:
17  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
18  * space    (space) <U2 'IA' 'IL' 'IN'
19
Ambrine
04 Nov 2019
1#import xarray
2import xarray as xr
3
4#open dataset
5ds = xr.open_dataset(file_name.nc)
6
7#add attributes and metadata in different ways
8ds.attrs = {attribute name : attribute value}
9ds.attrs[attribute name] = attribute value