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
(self, deep: bool = False, data: DataVars | None = None)
| 1032 | return replaced |
| 1033 | |
| 1034 | def copy(self, deep: bool = False, data: DataVars | None = None) -> Self: |
| 1035 | """Returns a copy of this dataset. |
| 1036 | |
| 1037 | If `deep=True`, a deep copy is made of each of the component variables. |
| 1038 | Otherwise, a shallow copy of each of the component variable is made, so |
| 1039 | that the underlying memory region of the new dataset is the same as in |
| 1040 | the original dataset. |
| 1041 | |
| 1042 | Use `data` to create a new object with the same structure as |
| 1043 | original but entirely new data. |
| 1044 | |
| 1045 | Parameters |
| 1046 | ---------- |
| 1047 | deep : bool, default: False |
| 1048 | Whether each component variable is loaded into memory and copied onto |
| 1049 | the new object. Default is False. |
| 1050 | data : dict-like or None, optional |
| 1051 | Data to use in the new object. Each item in `data` must have same |
| 1052 | shape as corresponding data variable in original. When `data` is |
| 1053 | used, `deep` is ignored for the data variables and only used for |
| 1054 | coords. |
| 1055 | |
| 1056 | Returns |
| 1057 | ------- |
| 1058 | object : Dataset |
| 1059 | New object with dimensions, attributes, coordinates, name, encoding, |
| 1060 | and optionally data copied from original. |
| 1061 | |
| 1062 | Examples |
| 1063 | -------- |
| 1064 | Shallow copy versus deep copy |
| 1065 | |
| 1066 | >>> da = xr.DataArray(np.random.randn(2, 3)) |
| 1067 | >>> ds = xr.Dataset( |
| 1068 | ... {"foo": da, "bar": ("x", [-1, 2])}, |
| 1069 | ... coords={"x": ["one", "two"]}, |
| 1070 | ... ) |
| 1071 | >>> ds.copy() |
| 1072 | <xarray.Dataset> Size: 88B |
| 1073 | Dimensions: (dim_0: 2, dim_1: 3, x: 2) |
| 1074 | Coordinates: |
| 1075 | * x (x) <U3 24B 'one' 'two' |
| 1076 | Dimensions without coordinates: dim_0, dim_1 |
| 1077 | Data variables: |
| 1078 | foo (dim_0, dim_1) float64 48B 1.764 0.4002 0.9787 2.241 1.868 -0.9773 |
| 1079 | bar (x) int64 16B -1 2 |
| 1080 | |
| 1081 | >>> ds_0 = ds.copy(deep=False) |
| 1082 | >>> ds_0["foo"][0, 0] = 7 |
| 1083 | >>> ds_0 |
| 1084 | <xarray.Dataset> Size: 88B |
| 1085 | Dimensions: (dim_0: 2, dim_1: 3, x: 2) |
| 1086 | Coordinates: |
| 1087 | * x (x) <U3 24B 'one' 'two' |
| 1088 | Dimensions without coordinates: dim_0, dim_1 |
| 1089 | Data variables: |
| 1090 | foo (dim_0, dim_1) float64 48B 7.0 0.4002 0.9787 2.241 1.868 -0.9773 |
| 1091 | bar (x) int64 16B -1 2 |