11
11
from .._coo .core import COO
12
12
from .._sparse_array import SparseArray
13
13
from .._utils import (
14
+ _zero_of_dtype ,
14
15
can_store ,
15
16
check_compressed_axes ,
16
- check_zero_fill_value ,
17
+ check_fill_value ,
17
18
equivalent ,
18
19
normalize_axis ,
19
20
)
@@ -137,7 +138,7 @@ def __init__(
137
138
shape = None ,
138
139
compressed_axes = None ,
139
140
prune = False ,
140
- fill_value = 0 ,
141
+ fill_value = None ,
141
142
idx_dtype = None ,
142
143
):
143
144
from .._common import _is_scipy_sparse_obj
@@ -176,8 +177,11 @@ def __init__(
176
177
177
178
self .shape = shape
178
179
180
+ if fill_value is None :
181
+ fill_value = _zero_of_dtype (self .data .dtype )
182
+
179
183
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 )
181
185
182
186
if prune :
183
187
self ._prune ()
@@ -194,7 +198,7 @@ def copy(self, deep=True):
194
198
return _copy .deepcopy (self ) if deep else _copy .copy (self )
195
199
196
200
@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 ):
198
202
coo = COO .from_numpy (x , fill_value = fill_value , idx_dtype = idx_dtype )
199
203
return cls .from_coo (coo , compressed_axes , idx_dtype )
200
204
@@ -204,12 +208,12 @@ def from_coo(cls, x, compressed_axes=None, idx_dtype=None):
204
208
return cls (arg , shape = shape , compressed_axes = compressed_axes , fill_value = fill_value )
205
209
206
210
@classmethod
207
- def from_scipy_sparse (cls , x ):
211
+ def from_scipy_sparse (cls , x , / , * , fill_value = None ):
208
212
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 )
210
214
211
215
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 )
213
217
214
218
@classmethod
215
219
def from_iter (cls , x , shape = None , compressed_axes = None , fill_value = None , idx_dtype = None ):
@@ -471,13 +475,20 @@ def todok(self):
471
475
472
476
return DOK .from_coo (self .tocoo ()) # probably a temporary solution
473
477
474
- def to_scipy_sparse (self ):
478
+ def to_scipy_sparse (self , accept_fv = None ):
475
479
"""
476
480
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
+
477
487
Returns
478
488
-------
479
489
:obj:`scipy.sparse.csr_matrix` or `scipy.sparse.csc_matrix`
480
490
The converted Scipy sparse matrix.
491
+
481
492
Raises
482
493
------
483
494
ValueError
@@ -487,8 +498,7 @@ def to_scipy_sparse(self):
487
498
"""
488
499
import scipy .sparse
489
500
490
- check_zero_fill_value (self )
491
-
501
+ check_fill_value (self , accept_fv = accept_fv )
492
502
if self .ndim != 2 :
493
503
raise ValueError ("Can only convert a 2-dimensional array to a Scipy sparse matrix." )
494
504
@@ -873,9 +883,9 @@ def __init__(self, arg, shape=None, compressed_axes=class_compressed_axes, prune
873
883
super ().__init__ (arg , shape = shape , compressed_axes = compressed_axes , fill_value = fill_value )
874
884
875
885
@classmethod
876
- def from_scipy_sparse (cls , x ):
886
+ def from_scipy_sparse (cls , x , / , * , fill_value = None ):
877
887
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 )
879
889
880
890
def transpose (self , axes : None = None , copy : bool = False ) -> Union ["CSC" , "CSR" ]:
881
891
axes = normalize_axis (axes , self .ndim )
@@ -905,9 +915,9 @@ def __init__(self, arg, shape=None, compressed_axes=class_compressed_axes, prune
905
915
super ().__init__ (arg , shape = shape , compressed_axes = compressed_axes , fill_value = fill_value )
906
916
907
917
@classmethod
908
- def from_scipy_sparse (cls , x ):
918
+ def from_scipy_sparse (cls , x , / , * , fill_value = None ):
909
919
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 )
911
921
912
922
def transpose (self , axes : None = None , copy : bool = False ) -> Union ["CSC" , "CSR" ]:
913
923
axes = normalize_axis (axes , self .ndim )
0 commit comments