python xarray – update subset of values
Question:
I need to update values in a Xarray DataArray. There are only certain values I want to update, which are stored in a dataframe together with their coordinates.
One can also reformulate the problem as both (i) update a non-continuous (non-rectangular) area of values in a DataArray and (ii) left-join dataset with new values on matching coords and rewrite old values.
Intial DataArray (Dataset):
import xarray as xr
import pandas as pd
ds = xr.Dataset(
data_vars = {"x": (("a", "b"), np.zeros((3, 4)))},
coords = {"a": np.arange(3), "b": np.arange(4)})
ds.x.values
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
New values to assign with their coordinates:
new_val = pd.DataFrame([
(0, 1, 10),
(1, 0, 11),
(2, 2, 12)],
columns=['a', 'b', 'x'])
Result I want to get:
array([[0., 10., 0., 0.],
[11., 0., 0., 0.],
[0., 0., 12., 0.]])
I was trying to use methods both in Combining data and in Assigning values with indexing turials, but no luck so far.
Answers:
Result can be achieved using combine_first
.
# set index and convert to xr Dataset
new_val_idx = new_val.set_index(['a', 'b'])
new_val_ds = xr.Dataset.from_dataframe(new_val_idx)
combined = new_val_ds.combine_first(ds)
combined.x.values
array([[ 0., 10., 0., 0.],
[11., 0., 0., 0.],
[ 0., 0., 12., 0.]])
I need to update values in a Xarray DataArray. There are only certain values I want to update, which are stored in a dataframe together with their coordinates.
One can also reformulate the problem as both (i) update a non-continuous (non-rectangular) area of values in a DataArray and (ii) left-join dataset with new values on matching coords and rewrite old values.
Intial DataArray (Dataset):
import xarray as xr
import pandas as pd
ds = xr.Dataset(
data_vars = {"x": (("a", "b"), np.zeros((3, 4)))},
coords = {"a": np.arange(3), "b": np.arange(4)})
ds.x.values
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
New values to assign with their coordinates:
new_val = pd.DataFrame([
(0, 1, 10),
(1, 0, 11),
(2, 2, 12)],
columns=['a', 'b', 'x'])
Result I want to get:
array([[0., 10., 0., 0.],
[11., 0., 0., 0.],
[0., 0., 12., 0.]])
I was trying to use methods both in Combining data and in Assigning values with indexing turials, but no luck so far.
Result can be achieved using combine_first
.
# set index and convert to xr Dataset
new_val_idx = new_val.set_index(['a', 'b'])
new_val_ds = xr.Dataset.from_dataframe(new_val_idx)
combined = new_val_ds.combine_first(ds)
combined.x.values
array([[ 0., 10., 0., 0.],
[11., 0., 0., 0.],
[ 0., 0., 12., 0.]])