Skip to content

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
Discussion options

You must be logged in to vote

I think the issue is that np.nan == np.nan returns False. This will work:

da.where(lambda arr: arr.notnull(), df.mean_S)

However, you might also want to try .fillna:

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

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@DaniJonesOcean
Comment options

Answer selected by DaniJonesOcean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants