# xarray.Dataset.copy

#### Dataset.copy(deep=False, data=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L1117-L1214)

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`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – Whether each component variable is loaded into memory and copied onto
    the new object. Default is False.
  * **data** ([dict-like](https://docs.python.org/3/glossary.html#term-mapping) or [`None`](https://docs.python.org/3/builtins/constants.html#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`](xarray.Dataset.html.md#xarray.Dataset)) – New object with dimensions, attributes, coordinates, name, encoding,
  and optionally data copied from original.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('6063476e-f48e-4810-8c68-c0c0cc65a05a','4f09f166-bc34-4798-a00a-af8af99e93c5','fa07dc94-8071-4935-993d-4227aa3cb36f','../lite/tree/../notebooks/index.html?path=44206948_00ac_4124_9954_5018157fba30.ipynb','None')">Try it in your browser!</button></div>

Shallow copy versus deep copy

```pycon
>>> 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
```

```pycon
>>> 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
```

```pycon
>>> 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.

```pycon
>>> 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'
```

```pycon
>>> 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
```

<div id="fa07dc94-8071-4935-993d-4227aa3cb36f" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('6063476e-f48e-4810-8c68-c0c0cc65a05a','fa07dc94-8071-4935-993d-4227aa3cb36f')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('6063476e-f48e-4810-8c68-c0c0cc65a05a','fa07dc94-8071-4935-993d-4227aa3cb36f')">Open In Tab</button></div><div id="4f09f166-bc34-4798-a00a-af8af99e93c5" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`pandas.DataFrame.copy`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html#pandas.DataFrame.copy)
