Reputation: 6892
I am trying to populate a DataArray with some meta information about cells to add it to a Dataset.
airtemps = xr.tutorial.load_dataset('air_temperature')
airtemps = airtemps.sel(time=slice('2013-01-01', '2013-12-31'))
I can set an entire new set of data given the existing dimensions:
airtemps['some_data'] = ([ 'lat', 'lon'], np.random.rand(25,53))
What I would like is to add a single data point at a given coordinate, basically
airtemps.some_data.sel_points(lat=75., lon=200.) = "New data"
Can this be done?
Upvotes: 2
Views: 2049
Reputation: 9603
It doesn't work with sel_points
, but instead of assignment with .sel
you can use the .loc
indexer, e.g.,
airtemps.some_data.loc[dict(lat=75., lon=200.)] = "New data"
Note that this will only work if the point (lat=75., lon=200.)
already exists in airtemps
. Xarray does not support inserting values at new coordinates, because this cannot be done efficiently with its NumPy based data structures. If you need to insert new values, you will need to create a new xarray data structure with appropriate values from scratch.
Upvotes: 5
Reputation: 6434
Take a look at this section in the xarray docs. None of the .sel
methods allow for assignment.
Any assignment should be done using normal indexing:
airtemps.some_data[dict(lat=0, lon=0)] = 4
There has been talk of extending xarray's ability to assign by coordinate label but we don't have that feature currently. Until then, we just have to come up with a label to index position mapping and then follow the syntax above.
Upvotes: 3