xarray.DataArray.pad (original) (raw)

DataArray.pad(pad_width=None, mode='constant', stat_length=None, constant_values=None, end_values=None, reflect_type=None, keep_attrs=None, **pad_width_kwargs)[source]#

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:

Returns:

padded (DataArray) – DataArray with the padded coordinates and data.

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])]) arr.pad(x=(1, 2), constant_values=0) <xarray.DataArray (x: 6)> Size: 48B array([0, 5, 6, 7, 0, 0]) Coordinates:

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])}, ... ) da.pad(x=1) <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:

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

da.pad(x=1, constant_values=1.23456789) <xarray.DataArray (x: 4, y: 4)> Size: 128B array([[ 1, 1, 1, 1], [ 0, 1, 2, 3], [10, 11, 12, 13], [ 1, 1, 1, 1]]) Coordinates: