How can I replace missing values in a dataset with a value that depends on a coordinate? #5292
Answered
by
keewis
DaniJonesOcean
asked this question in
Q&A
-
Beta Was this translation helpful? Give feedback.
Answered by
keewis
May 12, 2021
Replies: 1 comment 1 reply
-
I think the issue is that da.where(lambda arr: arr.notnull(), df.mean_S) However, you might also want to try In [10]: arr = xr.DataArray(
...: [[nan, 0, 5, 3], [7, 5, 3, nan], [6, 9, 1, 4], [8, 7, 3, nan], [nan, 8, 1, 4]],
...: dims=("x", "z"),
...: coords={
...: "x": [-1, 0, 1, 2, 3],
...: "z": [10, 11, 12, 13],
...: }
...: )
...: arr
Out[10]:
<xarray.DataArray (x: 5, z: 4)>
array([[nan, 0., 5., 3.],
[ 7., 5., 3., nan],
[ 6., 9., 1., 4.],
[ 8., 7., 3., nan],
[nan, 8., 1., 4.]])
Coordinates:
* x (x) int64 -1 0 1 2 3
* z (z) int64 10 11 12 13
In [11]: values = xr.DataArray(np.arange(88, 104), dims="z", coords={"z": range(16)})
...: values
Out[11]:
<xarray.DataArray (z: 16)>
array([ 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103])
Coordinates:
* z (z) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
In [12]: arr.fillna(values)
Out[12]:
<xarray.DataArray (x: 5, z: 4)>
array([[ 98., 0., 5., 3.],
[ 7., 5., 3., 101.],
[ 6., 9., 1., 4.],
[ 8., 7., 3., 101.],
[ 98., 8., 1., 4.]])
Coordinates:
* z (z) int64 10 11 12 13
* x (x) int64 -1 0 1 2 3 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
DaniJonesOcean
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the issue is that
np.nan == np.nan
returnsFalse
. This will work:However, you might also want to try
.fillna
: