xarray.DataArray.dropna#

DataArray.dropna(dim, *, how='any', thresh=None)[source]#

Returns a new array with dropped labels for missing values along the provided dimension.

Parameters:
  • dim (Hashable) โ€“ Dimension along which to drop missing values. Dropping along multiple dimensions simultaneously is not yet supported.

  • how ({"any", "all"}, default: "any") โ€“

    • any : if any NA values are present, drop that label

    • all : if all values are NA, drop that label

  • thresh (int or None, default: None) โ€“ If supplied, require this many non-NA values.

Returns:

dropped (DataArray)

Examples

>>> temperature = [
...     [0, 4, 2, 9],
...     [np.nan, np.nan, np.nan, np.nan],
...     [np.nan, 4, 2, 0],
...     [3, 1, 0, 0],
... ]
>>> da = xr.DataArray(
...     data=temperature,
...     dims=["Y", "X"],
...     coords=dict(
...         lat=("Y", np.array([-20.0, -20.25, -20.50, -20.75])),
...         lon=("X", np.array([10.0, 10.25, 10.5, 10.75])),
...     ),
... )
>>> da
<xarray.DataArray (Y: 4, X: 4)>
array([[ 0.,  4.,  2.,  9.],
       [nan, nan, nan, nan],
       [nan,  4.,  2.,  0.],
       [ 3.,  1.,  0.,  0.]])
Coordinates:
    lat      (Y) float64 -20.0 -20.25 -20.5 -20.75
    lon      (X) float64 10.0 10.25 10.5 10.75
Dimensions without coordinates: Y, X
>>> da.dropna(dim="Y", how="any")
<xarray.DataArray (Y: 2, X: 4)>
array([[0., 4., 2., 9.],
       [3., 1., 0., 0.]])
Coordinates:
    lat      (Y) float64 -20.0 -20.75
    lon      (X) float64 10.0 10.25 10.5 10.75
Dimensions without coordinates: Y, X

Drop values only if all values along the dimension are NaN:

>>> da.dropna(dim="Y", how="all")
<xarray.DataArray (Y: 3, X: 4)>
array([[ 0.,  4.,  2.,  9.],
       [nan,  4.,  2.,  0.],
       [ 3.,  1.,  0.,  0.]])
Coordinates:
    lat      (Y) float64 -20.0 -20.5 -20.75
    lon      (X) float64 10.0 10.25 10.5 10.75
Dimensions without coordinates: Y, X