1111from .._coo .core import COO
1212from .._sparse_array import SparseArray
1313from .._utils import (
14+ _zero_of_dtype ,
1415 can_store ,
1516 check_compressed_axes ,
16- check_zero_fill_value ,
17+ check_fill_value ,
1718 equivalent ,
1819 normalize_axis ,
1920)
@@ -137,7 +138,7 @@ def __init__(
137138 shape = None ,
138139 compressed_axes = None ,
139140 prune = False ,
140- fill_value = 0 ,
141+ fill_value = None ,
141142 idx_dtype = None ,
142143 ):
143144 from .._common import _is_scipy_sparse_obj
@@ -176,8 +177,11 @@ def __init__(
176177
177178 self .shape = shape
178179
180+ if fill_value is None :
181+ fill_value = _zero_of_dtype (self .data .dtype )
182+
179183 self ._compressed_axes = tuple (compressed_axes ) if isinstance (compressed_axes , Iterable ) else None
180- self .fill_value = fill_value
184+ self .fill_value = self . data . dtype . type ( fill_value )
181185
182186 if prune :
183187 self ._prune ()
@@ -194,7 +198,7 @@ def copy(self, deep=True):
194198 return _copy .deepcopy (self ) if deep else _copy .copy (self )
195199
196200 @classmethod
197- def from_numpy (cls , x , compressed_axes = None , fill_value = 0 , idx_dtype = None ):
201+ def from_numpy (cls , x , compressed_axes = None , fill_value = None , idx_dtype = None ):
198202 coo = COO .from_numpy (x , fill_value = fill_value , idx_dtype = idx_dtype )
199203 return cls .from_coo (coo , compressed_axes , idx_dtype )
200204
@@ -204,12 +208,12 @@ def from_coo(cls, x, compressed_axes=None, idx_dtype=None):
204208 return cls (arg , shape = shape , compressed_axes = compressed_axes , fill_value = fill_value )
205209
206210 @classmethod
207- def from_scipy_sparse (cls , x ):
211+ def from_scipy_sparse (cls , x , / , * , fill_value = None ):
208212 if x .format == "csc" :
209- return cls ((x .data , x .indices , x .indptr ), shape = x .shape , compressed_axes = (1 ,))
213+ return cls ((x .data , x .indices , x .indptr ), shape = x .shape , compressed_axes = (1 ,), fill_value = fill_value )
210214
211215 x = x .asformat ("csr" )
212- return cls ((x .data , x .indices , x .indptr ), shape = x .shape , compressed_axes = (0 ,))
216+ return cls ((x .data , x .indices , x .indptr ), shape = x .shape , compressed_axes = (0 ,), fill_value = fill_value )
213217
214218 @classmethod
215219 def from_iter (cls , x , shape = None , compressed_axes = None , fill_value = None , idx_dtype = None ):
@@ -471,13 +475,20 @@ def todok(self):
471475
472476 return DOK .from_coo (self .tocoo ()) # probably a temporary solution
473477
474- def to_scipy_sparse (self ):
478+ def to_scipy_sparse (self , accept_fv = None ):
475479 """
476480 Converts this :obj:`GCXS` object into a :obj:`scipy.sparse.csr_matrix` or `scipy.sparse.csc_matrix`.
481+
482+ Parameters
483+ ----------
484+ accept_fv : scalar or list of scalar, optional
485+ The list of accepted fill-values. The default accepts only zero.
486+
477487 Returns
478488 -------
479489 :obj:`scipy.sparse.csr_matrix` or `scipy.sparse.csc_matrix`
480490 The converted Scipy sparse matrix.
491+
481492 Raises
482493 ------
483494 ValueError
@@ -487,8 +498,7 @@ def to_scipy_sparse(self):
487498 """
488499 import scipy .sparse
489500
490- check_zero_fill_value (self )
491-
501+ check_fill_value (self , accept_fv = accept_fv )
492502 if self .ndim != 2 :
493503 raise ValueError ("Can only convert a 2-dimensional array to a Scipy sparse matrix." )
494504
@@ -873,9 +883,9 @@ def __init__(self, arg, shape=None, compressed_axes=class_compressed_axes, prune
873883 super ().__init__ (arg , shape = shape , compressed_axes = compressed_axes , fill_value = fill_value )
874884
875885 @classmethod
876- def from_scipy_sparse (cls , x ):
886+ def from_scipy_sparse (cls , x , / , * , fill_value = None ):
877887 x = x .asformat ("csr" , copy = False )
878- return cls ((x .data , x .indices , x .indptr ), shape = x .shape )
888+ return cls ((x .data , x .indices , x .indptr ), shape = x .shape , fill_value = fill_value )
879889
880890 def transpose (self , axes : None = None , copy : bool = False ) -> Union ["CSC" , "CSR" ]:
881891 axes = normalize_axis (axes , self .ndim )
@@ -905,9 +915,9 @@ def __init__(self, arg, shape=None, compressed_axes=class_compressed_axes, prune
905915 super ().__init__ (arg , shape = shape , compressed_axes = compressed_axes , fill_value = fill_value )
906916
907917 @classmethod
908- def from_scipy_sparse (cls , x ):
918+ def from_scipy_sparse (cls , x , / , * , fill_value = None ):
909919 x = x .asformat ("csc" , copy = False )
910- return cls ((x .data , x .indices , x .indptr ), shape = x .shape )
920+ return cls ((x .data , x .indices , x .indptr ), shape = x .shape , fill_value = fill_value )
911921
912922 def transpose (self , axes : None = None , copy : bool = False ) -> Union ["CSC" , "CSR" ]:
913923 axes = normalize_axis (axes , self .ndim )
0 commit comments