# xarray.Dataset.pad

#### Dataset.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]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L9192-L9388)

Pad this dataset 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:**
  * **pad_width** ([mapping](https://docs.python.org/3/glossary.html#term-mapping) of [hashable](../user-guide/terminology.html.md#term-name) to [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [`int`](https://docs.python.org/3/builtins/functions.html#int)) – Mapping with the form of {dim: (pad_before, pad_after)}
    describing the number of values padded along each dimension.
    {dim: pad} is a shortcut for pad_before = pad_after = pad
  * **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`](https://docs.python.org/3/builtins/functions.html#int), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) or [mapping](https://docs.python.org/3/glossary.html#term-mapping) of [hashable](../user-guide/terminology.html.md#term-name) to [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#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](../user-guide/terminology.html.md#term-scalar), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple), [mapping](https://docs.python.org/3/glossary.html#term-mapping) of [dimension name](../user-guide/terminology.html.md#term-name) to [scalar](../user-guide/terminology.html.md#term-scalar) or [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple), or             mapping of [variable name](../user-guide/terminology.html.md#term-name) to [scalar](../user-guide/terminology.html.md#term-scalar), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) or `to mapping` of [dimension name](../user-guide/terminology.html.md#term-name) to [scalar](../user-guide/terminology.html.md#term-scalar) or [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#None)) – Used in ‘constant’. The values to set the padded values for each data variable / axis.
    `{var_1: {dim_1: (before_1, after_1), ... dim_N: (before_N, after_N)}, ...
    var_M: (before, after)}` unique pad constants per data variable.
    `{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 `None`, pads with `np.nan`.
  * **end_values** ([scalar](../user-guide/terminology.html.md#term-scalar), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) or [mapping](https://docs.python.org/3/glossary.html#term-mapping) of [hashable](../user-guide/terminology.html.md#term-name) to [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#None)) – 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 None.
  * **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`](https://docs.python.org/3/builtins/functions.html#bool) or [`None`](https://docs.python.org/3/builtins/constants.html#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.
  * **\*\*pad_width_kwargs** – The keyword arguments form of `pad_width`.
    One of `pad_width` or `pad_width_kwargs` must be provided.
* **Returns:**
  **padded** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – Dataset with the padded coordinates and data.

#### SEE ALSO
[`Dataset.shift`](xarray.Dataset.shift.html.md#xarray.Dataset.shift), [`Dataset.roll`](xarray.Dataset.roll.html.md#xarray.Dataset.roll), [`Dataset.bfill`](xarray.Dataset.bfill.html.md#xarray.Dataset.bfill), [`Dataset.ffill`](xarray.Dataset.ffill.html.md#xarray.Dataset.ffill), [`numpy.pad`](https://numpy.org/doc/stable/reference/generated/numpy.pad.html#numpy.pad), [`dask.array.pad`](https://docs.dask.org/en/latest/generated/dask.array.pad.html#dask.array.pad)

### Notes

By default when `mode="constant"` and `constant_values=None`, integer types will be
promoted to `float` and padded with `np.nan`. To avoid type promotion
specify `constant_values=np.nan`

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

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('13a9f575-3a73-41a1-9c83-0cb945cd771f','51a86edc-d47c-490f-8d07-4d4405315c3f','40b18be7-6bbc-4601-9460-459e6ab9b338','../lite/tree/../notebooks/index.html?path=81125063_21ab_4e81_9fab_a6e6c15553bc.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> ds = xr.Dataset({"foo": ("x", range(5))})
>>> ds.pad(x=(1, 2))
<xarray.Dataset> Size: 64B
Dimensions:  (x: 8)
Dimensions without coordinates: x
Data variables:
    foo      (x) float64 64B nan 0.0 1.0 2.0 3.0 4.0 nan nan
```

<div id="40b18be7-6bbc-4601-9460-459e6ab9b338" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('13a9f575-3a73-41a1-9c83-0cb945cd771f','40b18be7-6bbc-4601-9460-459e6ab9b338')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('13a9f575-3a73-41a1-9c83-0cb945cd771f','40b18be7-6bbc-4601-9460-459e6ab9b338')">Open In Tab</button></div><div id="51a86edc-d47c-490f-8d07-4d4405315c3f" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
