File tree Expand file tree Collapse file tree 2 files changed +23
-5
lines changed
Expand file tree Collapse file tree 2 files changed +23
-5
lines changed Original file line number Diff line number Diff line change @@ -912,15 +912,20 @@ def is_copy(self):
912912
913913 def ensure_copy (self ):
914914 """
915- Ensure that the table owns its data; copy arrays when necessary
915+ Ensure that the table owns its data; copy arrays when necessary.
916916 """
917- if self .X .base is not None :
917+ def is_view (x ):
918+ # Sparse matrices don't have views like numpy arrays. Since indexing on
919+ # them creates copies in constructor we can skip this check here.
920+ return not sp .issparse (x ) and x .base is not None
921+
922+ if is_view (self .X ):
918923 self .X = self .X .copy ()
919- if self ._Y . base is not None :
924+ if is_view ( self ._Y ) :
920925 self ._Y = self ._Y .copy ()
921- if self .metas . base is not None :
926+ if is_view ( self .metas ) :
922927 self .metas = self .metas .copy ()
923- if self .W . base is not None :
928+ if is_view ( self .W ) :
924929 self .W = self .W .copy ()
925930
926931 def copy (self ):
Original file line number Diff line number Diff line change @@ -649,6 +649,19 @@ def test_copy(self):
649649 self .assertFalse (np .all (t .Y == copy .Y ))
650650 self .assertFalse (np .all (t .metas == copy .metas ))
651651
652+ def test_copy_sparse (self ):
653+ t = data .Table ('iris' )
654+ t .X = csr_matrix (t .X )
655+ copy = t .copy ()
656+
657+ self .assertEqual ((t .X != copy .X ).nnz , 0 ) # sparse matrices match by content
658+ np .testing .assert_equal (t .Y , copy .Y )
659+ np .testing .assert_equal (t .metas , copy .metas )
660+
661+ self .assertNotEqual (id (t .X ), id (copy .X ))
662+ self .assertNotEqual (id (t ._Y ), id (copy ._Y ))
663+ self .assertNotEqual (id (t .metas ), id (copy .metas ))
664+
652665 def test_concatenate (self ):
653666 d1 = data .Domain ([data .ContinuousVariable ('a1' )])
654667 t1 = data .Table .from_numpy (d1 , [[1 ],
You can’t perform that action at this time.
0 commit comments