xarray.DataArray.copy

DataArray.copy(deep=True, data=None)

Returns a copy of this array.

If deep=True, a deep copy is made of the data array. Otherwise, a shallow copy is made, so each variable in the new array’s dataset is also a variable in this array’s dataset.

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

Parameters
deepbool, optional

Whether the data array and its coordinates are loaded into memory and copied onto the new object. Default is True.

dataarray_like, optional

Data to use in the new object. Must have same shape as original. When data is used, deep is ignored for all data variables, and only used for coords.

Returns
objectDataArray

New object with dimensions, attributes, coordinates, name, encoding, and optionally data copied from original.

Examples

Shallow versus deep copy

>>> array = xr.DataArray([1, 2, 3], dims='x',
...                      coords={'x': ['a', 'b', 'c']})
>>> array.copy()
<xarray.DataArray (x: 3)>
array([1, 2, 3])
Coordinates:
* x        (x) <U1 'a' 'b' 'c'
>>> array_0 = array.copy(deep=False)
>>> array_0[0] = 7
>>> array_0
<xarray.DataArray (x: 3)>
array([7, 2, 3])
Coordinates:
* x        (x) <U1 'a' 'b' 'c'
>>> array
<xarray.DataArray (x: 3)>
array([7, 2, 3])
Coordinates:
* x        (x) <U1 'a' 'b' 'c'

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

>>> array.copy(data=[0.1, 0.2, 0.3])
<xarray.DataArray (x: 3)>
array([ 0.1,  0.2,  0.3])
Coordinates:
* x        (x) <U1 'a' 'b' 'c'
>>> array
<xarray.DataArray (x: 3)>
array([1, 2, 3])
Coordinates:
* x        (x) <U1 'a' 'b' 'c'