@@ -105,28 +105,36 @@ def test_accessor_raises(self):
105105
106106 @pytest .mark .parametrize ("format" , ["csc" , "csr" , "coo" ])
107107 @pytest .mark .parametrize ("labels" , [None , list (string .ascii_letters [:10 ])])
108- @pytest .mark .parametrize ("dtype" , [" float64" , " int64" ])
108+ @pytest .mark .parametrize ("dtype" , [np . complex128 , np . float64 , np . int64 , bool ])
109109 def test_from_spmatrix (self , format , labels , dtype ):
110110 sp_sparse = pytest .importorskip ("scipy.sparse" )
111111
112- sp_dtype = SparseDtype (dtype , np . array ( 0 , dtype = dtype ). item () )
112+ sp_dtype = SparseDtype (dtype )
113113
114- mat = sp_sparse .eye (10 , format = format , dtype = dtype )
115- result = pd .DataFrame .sparse .from_spmatrix (mat , index = labels , columns = labels )
114+ sp_mat = sp_sparse .eye (10 , format = format , dtype = dtype )
115+ result = pd .DataFrame .sparse .from_spmatrix (sp_mat , index = labels , columns = labels )
116+ mat = np .eye (10 , dtype = dtype )
116117 expected = pd .DataFrame (
117- np .eye (10 , dtype = dtype ), index = labels , columns = labels
118+ np .ma .array (mat , mask = (mat == 0 )).filled (sp_dtype .fill_value ),
119+ index = labels ,
120+ columns = labels ,
118121 ).astype (sp_dtype )
119122 tm .assert_frame_equal (result , expected )
120123
121124 @pytest .mark .parametrize ("format" , ["csc" , "csr" , "coo" ])
122- def test_from_spmatrix_including_explicit_zero (self , format ):
125+ @pytest .mark .parametrize ("dtype" , [np .int64 , bool ])
126+ def test_from_spmatrix_including_explicit_zero (self , format , dtype ):
123127 sp_sparse = pytest .importorskip ("scipy.sparse" )
124128
125- mat = sp_sparse .random (10 , 2 , density = 0.5 , format = format )
126- mat .data [0 ] = 0
127- result = pd .DataFrame .sparse .from_spmatrix (mat )
128- dtype = SparseDtype ("float64" , 0.0 )
129- expected = pd .DataFrame (mat .todense ()).astype (dtype )
129+ sp_dtype = SparseDtype (dtype )
130+
131+ sp_mat = sp_sparse .random (10 , 2 , density = 0.5 , format = format , dtype = dtype )
132+ sp_mat .data [0 ] = 0
133+ result = pd .DataFrame .sparse .from_spmatrix (sp_mat )
134+ mat = sp_mat .toarray ()
135+ expected = pd .DataFrame (
136+ np .ma .array (mat , mask = (mat == 0 )).filled (sp_dtype .fill_value )
137+ ).astype (sp_dtype )
130138 tm .assert_frame_equal (result , expected )
131139
132140 @pytest .mark .parametrize (
@@ -136,41 +144,34 @@ def test_from_spmatrix_including_explicit_zero(self, format):
136144 def test_from_spmatrix_columns (self , columns ):
137145 sp_sparse = pytest .importorskip ("scipy.sparse" )
138146
139- dtype = SparseDtype ("float64" , 0.0 )
147+ sp_dtype = SparseDtype (np . float64 )
140148
141- mat = sp_sparse .random (10 , 2 , density = 0.5 )
142- result = pd .DataFrame .sparse .from_spmatrix (mat , columns = columns )
143- expected = pd .DataFrame (mat .toarray (), columns = columns ).astype (dtype )
149+ sp_mat = sp_sparse .random (10 , 2 , density = 0.5 )
150+ result = pd .DataFrame .sparse .from_spmatrix (sp_mat , columns = columns )
151+ mat = sp_mat .toarray ()
152+ expected = pd .DataFrame (
153+ np .ma .array (mat , mask = (mat == 0 )).filled (sp_dtype .fill_value ),
154+ columns = columns ,
155+ ).astype (sp_dtype )
144156 tm .assert_frame_equal (result , expected )
145157
146158 @pytest .mark .parametrize (
147- "colnames " , [("A" , "B" ), (1 , 2 ), (1 , pd .NA ), (0.1 , 0.2 ), ("x" , "x" ), (0 , 0 )]
159+ "columns " , [("A" , "B" ), (1 , 2 ), (1 , pd .NA ), (0.1 , 0.2 ), ("x" , "x" ), (0 , 0 )]
148160 )
149- def test_to_coo (self , colnames ):
161+ @pytest .mark .parametrize ("dtype" , [np .complex128 , np .float64 , np .int64 , bool ])
162+ def test_to_coo (self , columns , dtype ):
150163 sp_sparse = pytest .importorskip ("scipy.sparse" )
151164
152- df = pd .DataFrame (
153- {colnames [0 ]: [0 , 1 , 0 ], colnames [1 ]: [1 , 0 , 0 ]}, dtype = "Sparse[int64, 0]"
154- )
155- result = df .sparse .to_coo ()
156- expected = sp_sparse .coo_matrix (np .asarray (df ))
157- assert (result != expected ).nnz == 0
165+ sp_dtype = SparseDtype (dtype )
158166
159- @pytest .mark .parametrize ("fill_value" , [1 , np .nan ])
160- def test_to_coo_nonzero_fill_val_raises (self , fill_value ):
161- pytest .importorskip ("scipy" )
162- df = pd .DataFrame (
163- {
164- "A" : SparseArray (
165- [fill_value , fill_value , fill_value , 2 ], fill_value = fill_value
166- ),
167- "B" : SparseArray (
168- [fill_value , 2 , fill_value , fill_value ], fill_value = fill_value
169- ),
170- }
171- )
172- with pytest .raises (ValueError , match = "fill value must be 0" ):
173- df .sparse .to_coo ()
167+ expected = sp_sparse .random (10 , 2 , density = 0.5 , format = "coo" , dtype = dtype )
168+ mat = expected .toarray ()
169+ result = pd .DataFrame (
170+ np .ma .array (mat , mask = (mat == 0 )).filled (sp_dtype .fill_value ),
171+ columns = columns ,
172+ dtype = sp_dtype ,
173+ ).sparse .to_coo ()
174+ assert (result != expected ).nnz == 0
174175
175176 def test_to_coo_midx_categorical (self ):
176177 # GH#50996
0 commit comments