Skip to content

Commit 9aae408

Browse files
Added test to check if issue pandas-dev#16112 is fixed
1 parent 075eca1 commit 9aae408

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pandas/tests/sparse/test_frame.py

+29
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ def test_from_to_scipy_object(spmatrix, fill_value):
12361236
tm.assert_sp_frame_equal(sdf_obj, expected)
12371237
tm.assert_frame_equal(sdf_obj.to_dense(), expected.to_dense())
12381238

1239+
12391240
# Assert spmatrices equal
12401241
assert dict(sdf.to_coo().todok()) == dict(spm.todok())
12411242

@@ -1245,6 +1246,34 @@ def test_from_to_scipy_object(spmatrix, fill_value):
12451246
assert sdf.to_coo().dtype == res_dtype
12461247

12471248

1249+
def test_from_scipy_object_fillna(spmatrix):
1250+
columns = list('cd')
1251+
index = list('ab')
1252+
tm.skip_if_no_package('scipy', max_version='0.19.0')
1253+
1254+
# Explicitly convert one zero to np.nan
1255+
arr = np.eye(2)
1256+
arr[1, 0] = np.nan
1257+
try:
1258+
spm = spmatrix(arr)
1259+
assert spm.dtype == arr.dtype
1260+
except (TypeError, AssertionError):
1261+
# If conversion to sparse fails for this spmatrix type and arr.dtype,
1262+
# then the combination is not currently supported in NumPy, so we
1263+
# can just skip testing it thoroughly
1264+
return
1265+
1266+
sdf = pd.SparseDataFrame(spm, index=index, columns=columns).fillna(-1.0)
1267+
1268+
# Returning frame should fill all nan values with -1
1269+
expected = pd.SparseDataFrame({"c": {"a": 1.0, "b": -1.0}, "d": {"a": -1.0, "b": 1.0}})
1270+
1271+
# Assert frame is as expected
1272+
sdf_obj = sdf.astype(object)
1273+
tm.assert_sp_frame_equal(sdf_obj, expected)
1274+
tm.assert_frame_equal(sdf_obj.to_dense(), expected.to_dense())
1275+
1276+
12481277
class TestSparseDataFrameArithmetic(tm.TestCase):
12491278

12501279
def test_numeric_op_scalar(self):

0 commit comments

Comments
 (0)