xarray.Variable.copy

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

Returns a copy of this object.

If deep=True, the data array is loaded into memory and copied onto the new object. Dimensions, attributes and encodings are always copied.

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 is 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.

Returns

object – New object with dimensions, attributes, encodings, and optionally data copied from original.

Return type

Variable

Examples

Shallow copy versus deep copy

>>> var = xr.Variable(data=[1, 2, 3], dims="x")
>>> var.copy()
<xarray.Variable (x: 3)>
array([1, 2, 3])
>>> var_0 = var.copy(deep=False)
>>> var_0[0] = 7
>>> var_0
<xarray.Variable (x: 3)>
array([7, 2, 3])
>>> var
<xarray.Variable (x: 3)>
array([7, 2, 3])

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

>>> var.copy(data=[0.1, 0.2, 0.3])
<xarray.Variable (x: 3)>
array([0.1, 0.2, 0.3])
>>> var
<xarray.Variable (x: 3)>
array([7, 2, 3])