🍾 Xarray is now 10 years old! 🎉

xarray.DataArray.fillna

xarray.DataArray.fillna#

DataArray.fillna(value)[source]#

Fill missing values in this object.

This operation follows the normal broadcasting and alignment rules that xarray uses for binary arithmetic, except the result is aligned to this object (join='left') instead of aligned to the intersection of index coordinates (join='inner').

Parameters:

value (scalar, ndarray or DataArray) – Used to fill all matching missing values in this array. If the argument is a DataArray, it is first aligned with (reindexed to) this array.

Returns:

filled (DataArray)

Examples

>>> da = xr.DataArray(
...     np.array([1, 4, np.nan, 0, 3, np.nan]),
...     dims="Z",
...     coords=dict(
...         Z=("Z", np.arange(6)),
...         height=("Z", np.array([0, 10, 20, 30, 40, 50])),
...     ),
... )
>>> da
<xarray.DataArray (Z: 6)> Size: 48B
array([ 1.,  4., nan,  0.,  3., nan])
Coordinates:
  * Z        (Z) int64 48B 0 1 2 3 4 5
    height   (Z) int64 48B 0 10 20 30 40 50

Fill all NaN values with 0:

>>> da.fillna(0)
<xarray.DataArray (Z: 6)> Size: 48B
array([1., 4., 0., 0., 3., 0.])
Coordinates:
  * Z        (Z) int64 48B 0 1 2 3 4 5
    height   (Z) int64 48B 0 10 20 30 40 50

Fill NaN values with corresponding values in array:

>>> da.fillna(np.array([2, 9, 4, 2, 8, 9]))
<xarray.DataArray (Z: 6)> Size: 48B
array([1., 4., 4., 0., 3., 9.])
Coordinates:
  * Z        (Z) int64 48B 0 1 2 3 4 5
    height   (Z) int64 48B 0 10 20 30 40 50