๐Ÿพ Xarray is now 10 years old! ๐ŸŽ‰

xarray.Dataset.copy

Contents

xarray.Dataset.copy#

Dataset.copy(deep=False, data=None)[source]#

Returns a copy of this dataset.

If deep=True, a deep copy is made of each of the component variables. Otherwise, a shallow copy of each of the component variable is made, so that the underlying memory region of the new dataset is the same as in the original dataset.

Use data to create a new object with the same structure as original but entirely new data.

Parameters:
  • deep (bool, default: False) โ€“ Whether each component variable is loaded into memory and copied onto the new object. Default is False.

  • data (dict-like or None, optional) โ€“ Data to use in the new object. Each item in data must have same shape as corresponding data variable in original. When data is used, deep is ignored for the data variables and only used for coords.

Returns:

object (Dataset) โ€“ New object with dimensions, attributes, coordinates, name, encoding, and optionally data copied from original.

Examples

Shallow copy versus deep copy

>>> da = xr.DataArray(np.random.randn(2, 3))
>>> ds = xr.Dataset(
...     {"foo": da, "bar": ("x", [-1, 2])},
...     coords={"x": ["one", "two"]},
... )
>>> ds.copy()
<xarray.Dataset> Size: 88B
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
  * x        (x) <U3 24B 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) float64 48B 1.764 0.4002 0.9787 2.241 1.868 -0.9773
    bar      (x) int64 16B -1 2
>>> ds_0 = ds.copy(deep=False)
>>> ds_0["foo"][0, 0] = 7
>>> ds_0
<xarray.Dataset> Size: 88B
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
  * x        (x) <U3 24B 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) float64 48B 7.0 0.4002 0.9787 2.241 1.868 -0.9773
    bar      (x) int64 16B -1 2
>>> ds
<xarray.Dataset> Size: 88B
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
  * x        (x) <U3 24B 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) float64 48B 7.0 0.4002 0.9787 2.241 1.868 -0.9773
    bar      (x) int64 16B -1 2

Changing the data using the data argument maintains the structure of the original object, but with the new data. Original object is unaffected.

>>> ds.copy(data={"foo": np.arange(6).reshape(2, 3), "bar": ["a", "b"]})
<xarray.Dataset> Size: 80B
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
  * x        (x) <U3 24B 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) int64 48B 0 1 2 3 4 5
    bar      (x) <U1 8B 'a' 'b'
>>> ds
<xarray.Dataset> Size: 88B
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
  * x        (x) <U3 24B 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) float64 48B 7.0 0.4002 0.9787 2.241 1.868 -0.9773
    bar      (x) int64 16B -1 2