14

This seems like a very basic operation, but I can't figure out how to do it using the xarray documentation.

I have an xarray DataSet:

dss
<xarray.DataArray (y: 1000, x: 1334)>
dask.array<shape=(1000, 1334), dtype=uint8, chunksize=(222, 58)>
Coordinates:
    band     int32 1
  * y        (y) float64 2.218e+06 2.218e+06 2.218e+06 2.218e+06 2.218e+06 ...
  * x        (x) float64 1.891e+06 1.891e+06 1.891e+06 1.891e+06 1.891e+06 ...
Attributes:
    transform:   (30.0, 0.0, -2493045.0, 0.0, -30.0, 3310005.0, 0.0, 0.0, 1.0)
    crs:         +ellps=GRS80 +lat_0=23 +lat_1=29.5 +lat_2=45.5 +lon_0=-96 +n...
    res:         (30.0, 30.0)
    is_tiled:    1
    nodatavals:  (nan,)

and a numpy array with the correct dimensions:

print(np.shape(nmap))
(1000, 1334)
nmap
array([[ 0.15,  0.1 ,  0.15, ...,  0.05,  0.05,  0.02],
       [ 0.15,  0.1 ,  0.05, ...,  0.05,  0.05,  0.05],
       [ 0.1 ,  0.15,  0.15, ...,  0.05,  0.05,  0.02],
       ..., 
       [ 0.02,  0.02,  0.02, ...,  0.02,  0.02,  0.02],
       [ 0.02,  0.09,  0.09, ...,  0.02,  0.02,  0.02],
       [ 0.02,  0.09,  0.09, ...,  0.02,  0.02,  0.02]])

I would like to add the array to the DataSet. My ultimate goal is to do spatial interpolation using x and y to extract interpolated values of nmap on a new grid.

1
  • The answer by @keisuke-fujii helped me recognize the difference between a Dataset and a DataArray. Now I will re-ask the question with a real Dataset dsc. Why does this not work? --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-35-3ac64cebb960> in <module>() 3 #dsc2 4 # add the Manning's n array ----> 5 dsc2['n']=(('eta_rho','xi_rho'),cnmapi) TypeError: 'method' object does not support item assignment Commented Aug 29, 2018 at 12:44

3 Answers 3

17

Do you want to create a Dataset that contains your numpy array nmap? Or do you want to make an arithmetic dss + nmap?

For the former case, you need to make a Dataset from dss first and assign nmap to it, as your dss is not a Dataset but a DataArray.

To make a Dataset from DataArrays, you can pass a dictionary mapping the array name to the DataArray object. If your array is not a DataArray but a numpy array or dask array, you need a tuple (dimensions, array, [attribute]).

ds = xr.Dataset({'dss': dss, 'nmap': (('y', 'x'), nmap)})

Or another way to do the same thing is

ds = xr.Dataset({})
ds['dss'] = ds
ds['nmap'] = (('y', 'x'), nmap)

For the latter case, simply do

dss + nmap
Sign up to request clarification or add additional context in comments.

1 Comment

ds['nmap'] = (('y', 'x'), nmap) this is what I was searching for. Thanks
11

Suppose you want to add temperature data to a data set:

# Take the following dataset as an example
data_set=xr.Dataset( coords={'lon': (['x', 'y'], lon),
                    'lat': (['x', 'y'], lat),
                    'time': pd.date_range('2014-09-06', periods=3)})
temp=np.array([[25, 24, 20, -12],[23, 21, 22, -11]])
data_set["Temperature"]=(['x', 'y', 'time'],  temp)

1 Comment

+1 for the this answer: I tried first supplying a dict of the dimension names (e.g. {'x','y','time'} but it failed. I had to give it the list in square brackets.
5

The correct way would be by using the assign method, example:

ds = ds.assign(variable_name=(['dim1','dim2'],your_array))

You have to remember that you cannot set a new variable with 2-dimensional data (or more) without explicit dimension names. That's why you need to pass a tuple of (dims, data) instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.