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
(self, deep: bool = True, data: Any = None)
| 1272 | return self._from_temp_dataset(ds) |
| 1273 | |
| 1274 | def copy(self, deep: bool = True, data: Any = None) -> Self: |
| 1275 | """Returns a copy of this array. |
| 1276 | |
| 1277 | If `deep=True`, a deep copy is made of the data array. |
| 1278 | Otherwise, a shallow copy is made, and the returned data array's |
| 1279 | values are a new view of this data array's values. |
| 1280 | |
| 1281 | Use `data` to create a new object with the same structure as |
| 1282 | original but entirely new data. |
| 1283 | |
| 1284 | Parameters |
| 1285 | ---------- |
| 1286 | deep : bool, optional |
| 1287 | Whether the data array and its coordinates are loaded into memory |
| 1288 | and copied onto the new object. Default is True. |
| 1289 | data : array_like, optional |
| 1290 | Data to use in the new object. Must have same shape as original. |
| 1291 | When `data` is used, `deep` is ignored for all data variables, |
| 1292 | and only used for coords. |
| 1293 | |
| 1294 | Returns |
| 1295 | ------- |
| 1296 | copy : DataArray |
| 1297 | New object with dimensions, attributes, coordinates, name, |
| 1298 | encoding, and optionally data copied from original. |
| 1299 | |
| 1300 | Examples |
| 1301 | -------- |
| 1302 | Shallow versus deep copy |
| 1303 | |
| 1304 | >>> array = xr.DataArray([1, 2, 3], dims="x", coords={"x": ["a", "b", "c"]}) |
| 1305 | >>> array.copy() |
| 1306 | <xarray.DataArray (x: 3)> Size: 24B |
| 1307 | array([1, 2, 3]) |
| 1308 | Coordinates: |
| 1309 | * x (x) <U1 12B 'a' 'b' 'c' |
| 1310 | >>> array_0 = array.copy(deep=False) |
| 1311 | >>> array_0[0] = 7 |
| 1312 | >>> array_0 |
| 1313 | <xarray.DataArray (x: 3)> Size: 24B |
| 1314 | array([7, 2, 3]) |
| 1315 | Coordinates: |
| 1316 | * x (x) <U1 12B 'a' 'b' 'c' |
| 1317 | >>> array |
| 1318 | <xarray.DataArray (x: 3)> Size: 24B |
| 1319 | array([7, 2, 3]) |
| 1320 | Coordinates: |
| 1321 | * x (x) <U1 12B 'a' 'b' 'c' |
| 1322 | |
| 1323 | Changing the data using the ``data`` argument maintains the |
| 1324 | structure of the original object, but with the new data. Original |
| 1325 | object is unaffected. |
| 1326 | |
| 1327 | >>> array.copy(data=[0.1, 0.2, 0.3]) |
| 1328 | <xarray.DataArray (x: 3)> Size: 24B |
| 1329 | array([0.1, 0.2, 0.3]) |
| 1330 | Coordinates: |
| 1331 | * x (x) <U1 12B 'a' 'b' 'c' |