I am just starting to use Xarray DataArray and I was not sure how to add an new coordinate axis to an existing dimension. Can anyone point me in the correct direction.
So I have an existing array that looks like:
demo_results = np.zeros([10, 20, 5])
cols = ['c1', 'c2', 'c3', 'c4', 'c5']
t = xr.DataArray(demo_results, coords={'sim': cols}, dims=('run', 'year', 'sim'))
So this will give me a 3 dimensional array with 5 coordinates along the 'sim' dimension. Now I wanted to add an additional coordinate on the 'sim' dimension named 'c6', but I was not sure how to do that? I did not see any info or examples in the documentation. In pandas I would just reference the new dimension like df.loc[:, 'c6] = 0 and that would work. But I tried something like that with Xarray such as:
t.loc['sim', 'c6'] = 0
but I go an error: TypeError: invalid indexer array, does not have integer dtype: array('sim', dtype='<U3')
I guess I am still getting the hang of DataArray indexing.
Xarray uses numpy/dask arrays under the hood so automatic extension is not supported (as it is in Pandas).
If you know the names and number of columns ahead of time, you can create the full area ahead of time. E.g.
demo_results = np.zeros([10, 20, 6])
cols = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6']
t = xr.DataArray(demo_results, coords={'sim': cols}, dims=('run', 'year', 'sim'))
t.loc[{'sim': 'c6'}] = -999. # or whatever value you like
Note, this will not work when you're using dask arrays, which do not support item assignment.
If you are using dask, cannot create the full array ahead of time, you can alway use xarray.concat:
demo_results = np.zeros([10, 20, 1]) - 999.
cols = ['c6']
t6 = xr.DataArray(demo_results, coords={'sim': cols}, dims=('run', 'year', 'sim'))
# concat old+new
t = xr.concat([t, t6], dim='sim')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With