dropna

UnitsAwareDataArray.dropna(dim: Hashable, *, how: Literal['any', 'all'] = 'any', thresh: int | None = None) Self

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

Return type:

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)> Size: 128B
array([[ 0.,  4.,  2.,  9.],
       [nan, nan, nan, nan],
       [nan,  4.,  2.,  0.],
       [ 3.,  1.,  0.,  0.]])
Coordinates:
    lat      (Y) float64 32B -20.0 -20.25 -20.5 -20.75
    lon      (X) float64 32B 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)> Size: 64B
array([[0., 4., 2., 9.],
       [3., 1., 0., 0.]])
Coordinates:
    lat      (Y) float64 16B -20.0 -20.75
    lon      (X) float64 32B 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)> Size: 96B
array([[ 0.,  4.,  2.,  9.],
       [nan,  4.,  2.,  0.],
       [ 3.,  1.,  0.,  0.]])
Coordinates:
    lat      (Y) float64 24B -20.0 -20.5 -20.75
    lon      (X) float64 32B 10.0 10.25 10.5 10.75
Dimensions without coordinates: Y, X