@@ -1245,6 +1245,53 @@ 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+ import sys
1273+ from scipy .sparse .bsr import bsr_matrix
1274+ from scipy .sparse .dia import dia_matrix
1275+ from scipy .sparse .dok import dok_matrix
1276+ if spmatrix == bsr_matrix :
1277+ # A SparseDataFrame from a bsr matrix does not fill 0s
1278+ # Therefore, only the explicit nan value needs to be filled with -1
1279+ tm .assert_frame_equal (sdf .to_dense (), expected_bsr .to_dense ())
1280+ elif spmatrix == dia_matrix :
1281+ # the dia matrix has a bug of a different nature,
1282+ # so is currently passed in this test suite
1283+ pass
1284+ elif spmatrix == dok_matrix and sys .version_info .major == 2 :
1285+ # the dok matrix in python2 has a bug of a different nature,
1286+ # so is currently passed in this test suite
1287+ pass
1288+ else :
1289+ # The internal representations can differ.
1290+ # This test is here to ensure that all nan values are filled,
1291+ # regardless of origin.
1292+ tm .assert_frame_equal (sdf .to_dense (), expected .to_dense ())
1293+
1294+
12481295class TestSparseDataFrameArithmetic (tm .TestCase ):
12491296
12501297 def test_numeric_op_scalar (self ):
0 commit comments