Python Xarray add DataArray to Dataset -


very simple question can't find answer online. have dataset , want add named dataarray it. dataset.add({"new_array": new_data_array}). know merge , update , concatenate, understanding merge merging 2 or more datasets , concatenate concatenating 2 or more dataarrays form dataarray, , haven't quite understood update yet. i've tried dataset.update({"new_array": new_data_array}) following error.

invalidindexerror: reindexing valid uniquely valued index objects 

i've tried dataset["new_array"] = new_data_array , same error.

update

i've found out problem of coordinates have duplicate values, didn't know about. coordinates used index, xarray gets confused (understandably) when trying combine shared coordinates. below example works.

names = ["joaquin", "manolo", "xavier"] n = xarray.dataarray([23, 98, 23], coords={"name": names}) print(n) print("======") m = numpy.random.randint(0, 256, (3, 4, 4)).astype(numpy.uint8) mm = xarray.dataarray(m, dims=["name", "row", "column"], coords=[names, range(4), range(4)]) print(mm) print("======") n_dataset = n.rename("number").to_dataset() n_dataset["mm"] = mm print(n_dataset) 

output:

<xarray.dataarray (name: 3)> array([23, 98, 23]) coordinates:   * name     (name) <u7 'joaquin' 'manolo' 'xavier' ====== <xarray.dataarray (name: 3, row: 4, column: 4)> array([[[ 55,  63, 250, 211],         [204, 151, 164, 237],         [182,  24, 211,  12],         [183, 220,  35,  78]],         [[208,   7,  91, 114],         [195,  30, 108, 130],         [ 61, 224, 105, 125],         [ 65,   1, 132, 137]],         [[ 52, 137,  62, 206],         [188, 160, 156, 126],         [145, 223, 103, 240],         [141,  38,  43,  68]]], dtype=uint8) coordinates:   * name     (name) <u7 'joaquin' 'manolo' 'xavier'   * row      (row) int64 0 1 2 3   * column   (column) int64 0 1 2 3 ====== <xarray.dataset> dimensions:  (column: 4, name: 3, row: 4) coordinates:   * name     (name) object 'joaquin' 'manolo' 'xavier'   * row      (row) int64 0 1 2 3   * column   (column) int64 0 1 2 3 data variables:     number   (name) int64 23 98 23     mm       (name, row, column) uint8 55 63 250 211 204 151 164 237 182 24 ... 

the above code uses names index. if change code little bit, names has duplicate, names = ["joaquin", "manolo", "joaquin"], invalidindexerror.

code:

names = ["joaquin", "manolo", "joaquin"] n = xarray.dataarray([23, 98, 23], coords={"name": names}) print(n) print("======") m = numpy.random.randint(0, 256, (3, 4, 4)).astype(numpy.uint8) mm = xarray.dataarray(m, dims=["name", "row", "column"], coords=[names, range(4), range(4)]) print(mm) print("======") n_dataset = n.rename("number").to_dataset() n_dataset["mm"] = mm print(n_dataset) 

output:

<xarray.dataarray (name: 3)> array([23, 98, 23]) coordinates:   * name     (name) <u7 'joaquin' 'manolo' 'joaquin' ====== <xarray.dataarray (name: 3, row: 4, column: 4)> array([[[247,   3,  20, 141],         [ 54, 111, 224,  56],         [144, 117, 131, 192],         [230,  44, 174,  14]],         [[225, 184, 170, 248],         [ 57, 105, 165,  70],         [220, 228, 238,  17],         [ 90, 118,  87,  30]],         [[158, 211,  31, 212],         [ 63, 172, 190, 254],         [165, 163, 184,  22],         [ 49, 224, 196, 244]]], dtype=uint8) coordinates:   * name     (name) <u7 'joaquin' 'manolo' 'joaquin'   * row      (row) int64 0 1 2 3   * column   (column) int64 0 1 2 3 ====== --------------------------------------------------------------------------- invalidindexerror                         traceback (most recent call last) <ipython-input-12-50863379cefe> in <module>()       8 print("======")       9 n_dataset = n.rename("number").to_dataset() ---> 10 n_dataset["mm"] = mm      11 print(n_dataset)  /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/xarray/core/dataset.py in __setitem__(self, key, value)     536             raise notimplementederror('cannot yet use dictionary key '     537                                       'to set dataset values') --> 538         self.update({key: value})     539      540     def __delitem__(self, key):  /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/xarray/core/dataset.py in update(self, other, inplace)    1434             dataset.    1435         """ -> 1436         variables, coord_names, dims = dataset_update_method(self, other)    1437     1438         return self._replace_vars_and_dims(variables, coord_names, dims,  /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/xarray/core/merge.py in dataset_update_method(dataset, other)     492     priority_arg = 1     493     indexes = dataset.indexes --> 494     return merge_core(objs, priority_arg=priority_arg, indexes=indexes)  /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/xarray/core/merge.py in merge_core(objs, compat, join, priority_arg, explicit_coords, indexes)     373     coerced = coerce_pandas_values(objs)     374     aligned = deep_align(coerced, join=join, copy=false, indexes=indexes, --> 375                          skip_single_target=true)     376     expanded = expand_variable_dicts(aligned)     377   /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/xarray/core/alignment.py in deep_align(list_of_variable_maps, join, copy, indexes, skip_single_target)     162      163     aligned = partial_align(*targets, join=join, copy=copy, indexes=indexes, --> 164                             skip_single_target=skip_single_target)     165      166     key, aligned_obj in zip(keys, aligned):  /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/xarray/core/alignment.py in partial_align(*objects, **kwargs)     122         valid_indexers = dict((k, v) k, v in joined_indexes.items()     123                               if k in obj.dims) --> 124         result.append(obj.reindex(copy=copy, **valid_indexers))     125      126     return tuple(result)  /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/xarray/core/dataset.py in reindex(self, indexers, method, tolerance, copy, **kw_indexers)    1216     1217         variables = alignment.reindex_variables( -> 1218             self.variables, self.indexes, indexers, method, tolerance, copy=copy)    1219         return self._replace_vars_and_dims(variables)    1220   /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/xarray/core/alignment.py in reindex_variables(variables, indexes, indexers, method, tolerance, copy)     234             target = utils.safe_cast_to_index(indexers[name])     235             indexer = index.get_indexer(target, method=method, --> 236                                         **get_indexer_kwargs)     237      238             to_shape[name] = len(target)  /library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/pandas/indexes/base.py in get_indexer(self, target, method, limit, tolerance)    2080     2081         if not self.is_unique: -> 2082             raise invalidindexerror('reindexing valid uniquely'    2083                                     ' valued index objects')    2084   invalidindexerror: reindexing valid uniquely valued index objects 

so it's not bug in xarray such. nevertheless, wasted many hours trying find bug, , wish error message more informative. hope xarray collaborators fix soon. (put in uniqueness check on coordinates before attempting merge.)

in case, method provided answer below still works.

you need make sure dimensions of new dataarray same in dataset. following should work:

dataset['new_array_name'] = new_array 

here complete example try out:

# create dimensions x = np.linspace(-10,10,10) y = np.linspace(-20,20,20) (yy, xx) = np.meshgrid(y,x)  # make 2 different dataarrays equal dimensions var1 = xray.dataarray(np.random.randn(len(x),len(y)),coords=[x, y],dims=['x','y']) var2 = xray.dataarray(-xx**2+yy**2,coords=[x, y],dims=['x','y'])  # save 1 dataarray dataset ds = var1.to_dataset(name = 'var1')  # add second dataarray existing dataset (ds) ds['var2'] = var2 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -