copy

UnitsAwareDataArray.copy(deep: bool = True, data: Any = None) Self

Returns a copy of this array.

If deep=True, a deep copy is made of the data array. Otherwise, a shallow copy is made, and the returned data array’s values are a new view of this data array’s values.

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

Parameters:
  • deep (bool, optional) – Whether the data array and its coordinates are loaded into memory and copied onto the new object. Default is True.

  • data (array_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:

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

Return type:

DataArray

Examples

Shallow versus deep copy

>>> array = xr.DataArray([1, 2, 3], dims="x", coords={"x": ["a", "b", "c"]})
>>> array.copy()
<xarray.DataArray (x: 3)> Size: 24B
array([1, 2, 3])
Coordinates:
  * x        (x) <U1 12B 'a' 'b' 'c'
>>> array_0 = array.copy(deep=False)
>>> array_0[0] = 7
>>> array_0
<xarray.DataArray (x: 3)> Size: 24B
array([7, 2, 3])
Coordinates:
  * x        (x) <U1 12B 'a' 'b' 'c'
>>> array
<xarray.DataArray (x: 3)> Size: 24B
array([7, 2, 3])
Coordinates:
  * x        (x) <U1 12B '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)> Size: 24B
array([0.1, 0.2, 0.3])
Coordinates:
  * x        (x) <U1 12B 'a' 'b' 'c'
>>> array
<xarray.DataArray (x: 3)> Size: 24B
array([7, 2, 3])
Coordinates:
  * x        (x) <U1 12B 'a' 'b' 'c'

See also

pandas.DataFrame.copy