-
Hi team,
I see that with 1-dim DataArray, we can clip with minimum or maximum nan value: xx = xr.DataArray([1, 3, 5], dims = "x")
xx.clip(np.nan, 4) Result:
But with 2-dims DataArray, if one of two bounds is np.nan, we will receive nan value: da = xr.DataArray(np.arange(18).reshape(3, 6), coords = {"x": range(3), "y":range(6)}, dims = ("x", "y"))
lower = xr.DataArray([1, 2, np.nan], coords = {"x": [0, 1, 2]}, dims = ["x"])
upper = xr.DataArray([4, 10, 14], coords = {"x": [0, 1, 2]}, dims = ["x"])
lower = xr.broadcast(da, lower)[1]
upper = xr.broadcast(da, upper)[1]
da.clip(lower, upper) Result:
The clip function perform well with 2-dims as it done in 1-dims |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
Hi @babameme — please could you fill out the issue template, including formatting the code reasonably? |
Beta Was this translation helpful? Give feedback.
-
Hi @max-sixty , this is the first time i request feature in github. Can you introduce how to fill out the issue template ? |
Beta Was this translation helpful? Give feedback.
-
Sure — when you created the issue, there was a template in the text box — could you fill that out? |
Beta Was this translation helpful? Give feedback.
-
If that's unclear, check out some other issues in the repo and use those as a template for yours. |
Beta Was this translation helpful? Give feedback.
-
Hi @max-sixty , i update my request |
Beta Was this translation helpful? Give feedback.
-
It's still not great but it's sufficient! What you want isn't available in numpy, the library we wrap: In [3]: da.values.clip(lower.values, upper.values)
Out[3]:
array([[ 1., 1., 2., 3., 4., 4.],
[ 6., 7., 8., 9., 10., 10.],
[nan, nan, nan, nan, nan, nan]]) But I think what you want is this: In [11]: lower_inf = lower.where(lower.notnull(), np.inf)
In [14]: lower_inf
Out[14]:
<xarray.DataArray (x: 3, y: 6)>
array([[ 1., 1., 1., 1., 1., 1.],
[ 2., 2., 2., 2., 2., 2.],
[inf, inf, inf, inf, inf, inf]])
Coordinates:
* x (x) int64 0 1 2
* y (y) int64 0 1 2 3 4 5
In [12]: da.clip(lower_inf, upper)
Out[12]:
<xarray.DataArray (x: 3, y: 6)>
array([[ 1., 1., 2., 3., 4., 4.],
[ 6., 7., 8., 9., 10., 10.],
[14., 14., 14., 14., 14., 14.]])
Coordinates:
* x (x) int64 0 1 2
* y (y) int64 0 1 2 3 4 5
* ``` |
Beta Was this translation helpful? Give feedback.
-
I got it. Thank you very much |
Beta Was this translation helpful? Give feedback.
-
Wait, do you see the result on the 3rd row:
The original row is
the lower bound is inf, and the upper bound is 14. I think the result should be:
|
Beta Was this translation helpful? Give feedback.
-
I got the sign wrong, corrected: In [4]: lower_inf = lower.where(lower.notnull(), -np.inf)
In [5]: da.clip(lower_inf, upper)
Out[5]:
<xarray.DataArray (x: 3, y: 6)>
array([[ 1., 1., 2., 3., 4., 4.],
[ 6., 7., 8., 9., 10., 10.],
[12., 13., 14., 14., 14., 14.]])
Coordinates:
* x (x) int64 0 1 2
* y (y) int64 0 1 2 3 4 5 |
Beta Was this translation helpful? Give feedback.
I got the sign wrong, corrected: