xarray.full_like#

xarray.full_like(other, fill_value, dtype=None, *, chunks=None, chunked_array_type=None, from_array_kwargs=None)[source]#

Return a new object with the same shape and type as a given object.

Returned object will be chunked if if the given object is chunked, or if chunks or chunked_array_type are specified.

Parameters:
  • other (DataArray, Dataset or Variable) – The reference object in input

  • fill_value (scalar or dict-like) – Value to fill the new object with before returning it. If other is a Dataset, may also be a dict-like mapping data variables to fill values.

  • dtype (dtype or dict-like of dtype, optional) – dtype of the new array. If a dict-like, maps dtypes to variables. If omitted, it defaults to other.dtype.

  • chunks (int, "auto", tuple of int or mapping of Hashable to int, optional) – Chunk sizes along each dimension, e.g., 5, "auto", (5, 5) or {"x": 5, "y": 5}.

  • chunked_array_type (str, optional) – Which chunked array type to coerce the underlying data array to. Defaults to β€˜dask’ if installed, else whatever is registered via the ChunkManagerEnetryPoint system. Experimental API that should not be relied upon.

  • from_array_kwargs (dict, optional) – Additional keyword arguments passed on to the ChunkManagerEntrypoint.from_array method used to create chunked arrays, via whichever chunk manager is specified through the chunked_array_type kwarg. For example, with dask as the default chunked array type, this method would pass additional kwargs to dask.array.from_array(). Experimental API that should not be relied upon.

Returns:

out (same as object) – New object with the same shape and type as other, with the data filled with fill_value. Coords will be copied from other. If other is based on dask, the new one will be as well, and will be split in the same chunks.

Examples

>>> x = xr.DataArray(
...     np.arange(6).reshape(2, 3),
...     dims=["lat", "lon"],
...     coords={"lat": [1, 2], "lon": [0, 1, 2]},
... )
>>> x
<xarray.DataArray (lat: 2, lon: 3)>
array([[0, 1, 2],
       [3, 4, 5]])
Coordinates:
  * lat      (lat) int64 1 2
  * lon      (lon) int64 0 1 2
>>> xr.full_like(x, 1)
<xarray.DataArray (lat: 2, lon: 3)>
array([[1, 1, 1],
       [1, 1, 1]])
Coordinates:
  * lat      (lat) int64 1 2
  * lon      (lon) int64 0 1 2
>>> xr.full_like(x, 0.5)
<xarray.DataArray (lat: 2, lon: 3)>
array([[0, 0, 0],
       [0, 0, 0]])
Coordinates:
  * lat      (lat) int64 1 2
  * lon      (lon) int64 0 1 2
>>> xr.full_like(x, 0.5, dtype=np.double)
<xarray.DataArray (lat: 2, lon: 3)>
array([[0.5, 0.5, 0.5],
       [0.5, 0.5, 0.5]])
Coordinates:
  * lat      (lat) int64 1 2
  * lon      (lon) int64 0 1 2
>>> xr.full_like(x, np.nan, dtype=np.double)
<xarray.DataArray (lat: 2, lon: 3)>
array([[nan, nan, nan],
       [nan, nan, nan]])
Coordinates:
  * lat      (lat) int64 1 2
  * lon      (lon) int64 0 1 2
>>> ds = xr.Dataset(
...     {"a": ("x", [3, 5, 2]), "b": ("x", [9, 1, 0])}, coords={"x": [2, 4, 6]}
... )
>>> ds
<xarray.Dataset>
Dimensions:  (x: 3)
Coordinates:
  * x        (x) int64 2 4 6
Data variables:
    a        (x) int64 3 5 2
    b        (x) int64 9 1 0
>>> xr.full_like(ds, fill_value={"a": 1, "b": 2})
<xarray.Dataset>
Dimensions:  (x: 3)
Coordinates:
  * x        (x) int64 2 4 6
Data variables:
    a        (x) int64 1 1 1
    b        (x) int64 2 2 2
>>> xr.full_like(ds, fill_value={"a": 1, "b": 2}, dtype={"a": bool, "b": float})
<xarray.Dataset>
Dimensions:  (x: 3)
Coordinates:
  * x        (x) int64 2 4 6
Data variables:
    a        (x) bool True True True
    b        (x) float64 2.0 2.0 2.0

See also

zeros_like, ones_like