đŸ Xarray is now 10 years old! đ

## Contents

Pad this array along one or more dimensions.

Warning

This function is experimental and its behaviour is likely to change especially regarding padding of dimension coordinates (or IndexVariables).

When using one of the modes (âedgeâ, âreflectâ, âsymmetricâ, âwrapâ), coordinates will be padded with the same mode, otherwise coordinates are padded using the âconstantâ mode with fill_value dtypes.NA.

Parameters:

• mode ({"constant", "edge", "linear_ramp", "maximum", "mean", "median", "minimum", "reflect", "symmetric", "wrap"}, default: "constant") â How to pad the DataArray (taken from numpy docs):

• âconstantâ: Pads with a constant value.

• âedgeâ: Pads with the edge values of array.

• âlinear_rampâ: Pads with the linear ramp between end_value and the array edge value.

• âmaximumâ: Pads with the maximum value of all or part of the vector along each axis.

• âmeanâ: Pads with the mean value of all or part of the vector along each axis.

• âmedianâ: Pads with the median value of all or part of the vector along each axis.

• âminimumâ: Pads with the minimum value of all or part of the vector along each axis.

• âreflectâ: Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.

• âsymmetricâ: Pads with the reflection of the vector mirrored along the edge of the array.

• âwrapâ: Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.

• stat_length (int, tuple or mapping of Hashable to tuple, default: None) â Used in âmaximumâ, âmeanâ, âmedianâ, and âminimumâ. Number of values at edge of each axis used to calculate the statistic value. {dim_1: (before_1, after_1), âŠ dim_N: (before_N, after_N)} unique statistic lengths along each dimension. ((before, after),) yields same before and after statistic lengths for each dimension. (stat_length,) or int is a shortcut for before = after = statistic length for all axes. Default is None, to use the entire axis.

• constant_values (scalar, tuple or mapping of Hashable to tuple, default: 0) â Used in âconstantâ. The values to set the padded values for each axis. {dim_1: (before_1, after_1), ... dim_N: (before_N, after_N)} unique pad constants along each dimension. ((before, after),) yields same before and after constants for each dimension. (constant,) or constant is a shortcut for before = after = constant for all dimensions. Default is 0.

• end_values (scalar, tuple or mapping of Hashable to tuple, default: 0) â Used in âlinear_rampâ. The values used for the ending value of the linear_ramp and that will form the edge of the padded array. {dim_1: (before_1, after_1), ... dim_N: (before_N, after_N)} unique end values along each dimension. ((before, after),) yields same before and after end values for each axis. (constant,) or constant is a shortcut for before = after = constant for all axes. Default is 0.

• reflect_type ({"even", "odd", None}, optional) â Used in âreflectâ, and âsymmetricâ. The âevenâ style is the default with an unaltered reflection around the edge value. For the âoddâ style, the extended part of the array is created by subtracting the reflected values from two times the edge value.

• keep_attrs (bool or None, optional) â If True, the attributes (attrs) will be copied from the original object to the new one. If False, the new object will be returned without attributes.

Returns:

Notes

For mode="constant" and constant_values=None, integer types will be promoted to float and padded with np.nan.

Padding coordinates will drop their corresponding index (if any) and will reset default indexes for dimension coordinates.

Examples

>>> arr = xr.DataArray([5, 6, 7], coords=[("x", [0, 1, 2])])
<xarray.DataArray (x: 6)> Size: 48B
array([0, 5, 6, 7, 0, 0])
Coordinates:
* x        (x) float64 48B nan 0.0 1.0 2.0 nan nan
>>> da = xr.DataArray(
...     [[0, 1, 2, 3], [10, 11, 12, 13]],
...     dims=["x", "y"],
...     coords={"x": [0, 1], "y": [10, 20, 30, 40], "z": ("x", [100, 200])},
... )
<xarray.DataArray (x: 4, y: 4)> Size: 128B
array([[nan, nan, nan, nan],
[ 0.,  1.,  2.,  3.],
[10., 11., 12., 13.],
[nan, nan, nan, nan]])
Coordinates:
* x        (x) float64 32B nan 0.0 1.0 nan
* y        (y) int64 32B 10 20 30 40
z        (x) float64 32B nan 100.0 200.0 nan

Careful, constant_values are coerced to the data type of the array which may lead to a loss of precision: