xarray.Dataset.copy

Dataset.copy(deep: bool = False, data: collections.abc.Mapping = None) → T

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
deepbool, optional

Whether each component variable is loaded into memory and copied onto the new object. Default is False.

datadict-like, 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
objectDataset

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>
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
* x        (x) <U3 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) float64 -0.8079 0.3897 -1.862 -0.6091 -1.051 -0.3003
    bar      (x) int64 -1 2
>>> ds_0 = ds.copy(deep=False)
>>> ds_0['foo'][0, 0] = 7
>>> ds_0
<xarray.Dataset>
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
* x        (x) <U3 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) float64 7.0 0.3897 -1.862 -0.6091 -1.051 -0.3003
    bar      (x) int64 -1 2
>>> ds
<xarray.Dataset>
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
* x        (x) <U3 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) float64 7.0 0.3897 -1.862 -0.6091 -1.051 -0.3003
    bar      (x) int64 -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>
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
* x        (x) <U3 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) int64 0 1 2 3 4 5
    bar      (x) <U1 'a' 'b'
>>> ds
<xarray.Dataset>
Dimensions:  (dim_0: 2, dim_1: 3, x: 2)
Coordinates:
* x        (x) <U3 'one' 'two'
Dimensions without coordinates: dim_0, dim_1
Data variables:
    foo      (dim_0, dim_1) float64 7.0 0.3897 -1.862 -0.6091 -1.051 -0.3003
    bar      (x) int64 -1 2