Skip to content

Commit

Permalink
Update variable.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Illviljan committed Oct 14, 2023
1 parent d9543f4 commit 587c9e7
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2760,7 +2760,7 @@ def concat(

return cls(first_var.dims, data, attrs)

def copy(self, deep: bool = True, data: ArrayLike | None = None):
def copy(self, deep: bool = True, data: T_DuckArray | ArrayLike | None = None):
"""Returns a copy of this object.
`deep` is ignored since data is stored in the form of
Expand All @@ -2784,15 +2784,25 @@ def copy(self, deep: bool = True, data: ArrayLike | None = None):
New object with dimensions, attributes, encodings, and optionally
data copied from original.
"""
if data is not None:

if data is None:
data_old = self._data

if isinstance(data_old, indexing.MemoryCachedArray):
# don't share caching between copies
ndata = indexing.MemoryCachedArray(data_old.array)
else:
ndata = data_old

if deep:
ndata = copy.deepcopy(ndata, None)

else:
ndata = as_compatible_data(data)
if self.shape != ndata.shape:
if self.shape != ndata.shape: # type: ignore[attr-defined]
raise ValueError(
f"Data shape {ndata.shape} must match shape of object {self.shape}"
f"Data shape {ndata.shape} must match shape of object {self.shape}" # type: ignore[attr-defined]
)
else:
# TODO: array api has no copy-function:
ndata = self._data.copy(deep=deep) # type: ignore[assignment]

attrs = copy.deepcopy(self._attrs) if deep else copy.copy(self._attrs)
encoding = copy.deepcopy(self._encoding) if deep else copy.copy(self._encoding)
Expand Down

0 comments on commit 587c9e7

Please sign in to comment.