@@ -209,11 +209,14 @@ def from_coo(cls, x, compressed_axes=None, idx_dtype=None):
209209
210210 @classmethod
211211 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 )
217220
218221 @classmethod
219222 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
903906 @classmethod
904907 def from_scipy_sparse (cls , x , / , * , fill_value = None ):
905908 x = x .asformat ("csr" , copy = False )
909+ if not x .has_canonical_format :
910+ x .eliminate_zeros ()
911+ x .sum_duplicates ()
912+
906913 return cls ((x .data , x .indices , x .indptr ), shape = x .shape , fill_value = fill_value )
907914
908915 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
935942 @classmethod
936943 def from_scipy_sparse (cls , x , / , * , fill_value = None ):
937944 x = x .asformat ("csc" , copy = False )
945+ if not x .has_canonical_format :
946+ x .eliminate_zeros ()
947+ x .sum_duplicates ()
948+
938949 return cls ((x .data , x .indices , x .indptr ), shape = x .shape , fill_value = fill_value )
939950
940951 def transpose (self , axes : None = None , copy : bool = False ) -> Union ["CSC" , "CSR" ]:
0 commit comments