Skip to content

Clip function with minimum or maximum nan value #5205

Description

@babameme

Hi team,

  1. Concise description about the problem: Clip function with minimum or maximum nan value

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:

xarray.DataArrayx: 3
array([1., 3., 4.])
Coordinates: (0)
Attributes: (0)

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:

xarray.DataArrayx: 3y: 6
array([[ 1., 1., 2., 3., 4., 4.],
[ 6., 7., 8., 9., 10., 10.],
[nan, nan, nan, nan, nan, nan]])
Coordinates:
x
(x)
int64
0 1 2
y
(y)
int64
0 1 2 3 4 5
Attributes: (0)

  1. Describe the solution i would like:

The clip function perform well with 2-dims as it done in 1-dims

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions