@@ -209,11 +209,14 @@ def from_coo(cls, x, compressed_axes=None, idx_dtype=None):
209
209
210
210
@classmethod
211
211
def from_scipy_sparse (cls , x , / , * , fill_value = None ):
212
- if x .format == "csc" :
213
- return cls ((x .data , x .indices , x .indptr ), shape = x .shape , compressed_axes = (1 ,), fill_value = fill_value )
214
-
215
- x = x .asformat ("csr" )
216
- return cls ((x .data , x .indices , x .indptr ), shape = x .shape , compressed_axes = (0 ,), fill_value = fill_value )
212
+ is_csc = x .format == "csc"
213
+ ca = (1 ,) if is_csc else (0 ,)
214
+ if not is_csc :
215
+ x = x .asformat ("csr" )
216
+ if not x .has_canonical_format :
217
+ x .eliminate_zeros ()
218
+ x .sum_duplicates ()
219
+ return cls ((x .data , x .indices , x .indptr ), shape = x .shape , compressed_axes = ca , fill_value = fill_value )
217
220
218
221
@classmethod
219
222
def from_iter (cls , x , shape = None , compressed_axes = None , fill_value = None , idx_dtype = None ):
@@ -903,6 +906,10 @@ def __init__(self, arg, shape=None, compressed_axes=class_compressed_axes, prune
903
906
@classmethod
904
907
def from_scipy_sparse (cls , x , / , * , fill_value = None ):
905
908
x = x .asformat ("csr" , copy = False )
909
+ if not x .has_canonical_format :
910
+ x .eliminate_zeros ()
911
+ x .sum_duplicates ()
912
+
906
913
return cls ((x .data , x .indices , x .indptr ), shape = x .shape , fill_value = fill_value )
907
914
908
915
def transpose (self , axes : None = None , copy : bool = False ) -> Union ["CSC" , "CSR" ]:
@@ -935,6 +942,10 @@ def __init__(self, arg, shape=None, compressed_axes=class_compressed_axes, prune
935
942
@classmethod
936
943
def from_scipy_sparse (cls , x , / , * , fill_value = None ):
937
944
x = x .asformat ("csc" , copy = False )
945
+ if not x .has_canonical_format :
946
+ x .eliminate_zeros ()
947
+ x .sum_duplicates ()
948
+
938
949
return cls ((x .data , x .indices , x .indptr ), shape = x .shape , fill_value = fill_value )
939
950
940
951
def transpose (self , axes : None = None , copy : bool = False ) -> Union ["CSC" , "CSR" ]:
0 commit comments