Skip to content

Commit a7b29d2

Browse files
committed
Added a copy method for SparseNdarrays.
1 parent a0a7ccd commit a7b29d2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/delayedarray/SparseNdarray.py

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Callable, List, Optional, Sequence, Tuple, Union
33
from collections import namedtuple
44
import numpy
5+
import copy
56

67
from ._isometric import translate_ufunc_to_op_simple, translate_ufunc_to_op_with_args, ISOMETRIC_OP_WITH_ARGS, _choose_operator, _infer_along_with_args
78
from ._subset import _spawn_indices, _getitem_subset_preserves_dimensions, _getitem_subset_discards_dimensions, _repr_subset
@@ -777,6 +778,30 @@ def T(self) -> "SparseNdarray":
777778
return _transpose_SparseNdarray(self, axes)
778779

779780

781+
# Other stuff
782+
def __copy__(self) -> "SparseNdarray":
783+
"""
784+
Returns:
785+
A deep copy of this object.
786+
"""
787+
return SparseNdarray(
788+
shape=self._shape,
789+
contents=copy.deepcopy(self._contents),
790+
index_dtype=self._index_dtype,
791+
dtype=self._dtype,
792+
is_masked=self._is_masked,
793+
check=False
794+
)
795+
796+
797+
def copy(self) -> "SparseNdarray":
798+
"""
799+
Returns:
800+
A deep copy of this object.
801+
"""
802+
return self.__copy__()
803+
804+
780805
#########################################################
781806
#########################################################
782807

tests/test_SparseNdarray.py

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ def test_SparseNdarray_check(mask_rate):
126126
assert delayedarray.is_masked(y) == (mask_rate > 0)
127127
assert delayedarray.is_masked(y) == y.is_masked
128128

129+
clone = copy.copy(y)
130+
clone.contents[0] = "FOOBAR"
131+
assert y.contents[0] != "FOOBAR"
132+
129133
with pytest.raises(ValueError, match="match the extent"):
130134
y = delayedarray.SparseNdarray((10, 15, 1), contents)
131135

0 commit comments

Comments
 (0)