@@ -1245,6 +1245,47 @@ def test_from_to_scipy_object(spmatrix, fill_value):
12451245 assert sdf .to_coo ().dtype == res_dtype
12461246
12471247
1248+ def test_from_scipy_object_fillna (spmatrix ):
1249+ columns = list ('cd' )
1250+ index = list ('ab' )
1251+ tm .skip_if_no_package ('scipy' , max_version = '0.19.0' )
1252+
1253+ # Explicitly convert one zero to np.nan
1254+ arr = np .array ([[2.0 , 0.0 ], [np .nan , 1.0 ]])
1255+ try :
1256+ spm = spmatrix (arr )
1257+ assert spm .dtype == arr .dtype
1258+ except (TypeError , AssertionError ):
1259+ # If conversion to sparse fails for this spmatrix type and arr.dtype,
1260+ # then the combination is not currently supported in NumPy, so we
1261+ # can just skip testing it thoroughly
1262+ return
1263+
1264+ sdf = pd .SparseDataFrame (spm , index = index , columns = columns ).fillna (- 1.0 )
1265+
1266+ # Returning frame should fill all nan values with -1.0
1267+ expected = pd .SparseDataFrame ({"c" : {"a" : 2.0 , "b" : np .nan },
1268+ "d" : {"a" : np .nan , "b" : 1.0 }}).fillna (- 1.0 )
1269+ expected_bsr = pd .SparseDataFrame ({"c" : {"a" : 2.0 , "b" : np .nan },
1270+ "d" : {"a" : 0.0 , "b" : 1.0 }}).fillna (- 1.0 )
1271+
1272+ from scipy .sparse .bsr import bsr_matrix
1273+ from scipy .sparse .dia import dia_matrix
1274+ if spmatrix == bsr_matrix :
1275+ # A SparseDataFrame from a bsr matrix does not fill 0s
1276+ # Therefore, only the explicit nan value needs to be filled with -1
1277+ tm .assert_frame_equal (sdf .to_dense (), expected_bsr .to_dense ())
1278+ elif spmatrix == dia_matrix :
1279+ # the dia matrix has a bug of a different nature,
1280+ # so is currently passed in this test suite
1281+ pass
1282+ else :
1283+ # The internal representations can differ.
1284+ # This test is here to ensure that all nan values are filled,
1285+ # regardless of origin.
1286+ tm .assert_frame_equal (sdf .to_dense (), expected .to_dense ())
1287+
1288+
12481289class TestSparseDataFrameArithmetic (tm .TestCase ):
12491290
12501291 def test_numeric_op_scalar (self ):
0 commit comments