BUG: Made SparseDataFrame.fillna() fill all NaNs · rs2/pandas@4bc01a1 (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -595,14 +595,12 @@ def fillna(self, value, downcast=None):
595 595 if issubclass(self.dtype.type, np.floating):
596 596 value = float(value)
597 597
598 -if self._null_fill_value:
599 -return self._simple_new(self.sp_values, self.sp_index,
600 -fill_value=value)
601 -else:
602 -new_values = self.sp_values.copy()
603 -new_values[isnull(new_values)] = value
604 -return self._simple_new(new_values, self.sp_index,
605 -fill_value=self.fill_value)
598 +new_values = self.sp_values.copy()
599 +new_values[isnull(new_values)] = value
600 +fill_value = value if isnull(self.fill_value) else self.fill_value
601 +
602 +return self._simple_new(new_values, self.sp_index,
603 +fill_value=fill_value)
606 604
607 605 def sum(self, axis=0, *args, **kwargs):
608 606 """
Original file line number Diff line number Diff line change
@@ -1252,6 +1252,7 @@ def test_from_scipy_correct_ordering(spmatrix):
1252 1252 tm.skip_if_no_package('scipy')
1253 1253
1254 1254 arr = np.arange(1, 5).reshape(2, 2)
1255 +
1255 1256 try:
1256 1257 spm = spmatrix(arr)
1257 1258 assert spm.dtype == arr.dtype
@@ -1267,6 +1268,33 @@ def test_from_scipy_correct_ordering(spmatrix):
1267 1268 tm.assert_frame_equal(sdf.to_dense(), expected.to_dense())
1268 1269
1269 1270
1271 +def test_from_scipy_object_fillna(spmatrix):
1272 +# GH 16112
1273 +tm.skip_if_no_package('scipy', max_version='0.19.0')
1274 +
1275 +arr = np.eye(3)
1276 +arr[1:, 0] = np.nan
1277 +
1278 +try:
1279 +spm = spmatrix(arr)
1280 +assert spm.dtype == arr.dtype
1281 +except (TypeError, AssertionError):
1282 +# If conversion to sparse fails for this spmatrix type and arr.dtype,
1283 +# then the combination is not currently supported in NumPy, so we
1284 +# can just skip testing it thoroughly
1285 +return
1286 +
1287 +sdf = pd.SparseDataFrame(spm).fillna(-1.0)
1288 +
1289 +# Returning frame should fill all nan values with -1.0
1290 +expected = pd.SparseDataFrame({0: {0: 1.0, 1: np.nan, 2: np.nan},
1291 +1: {0: np.nan, 1: 1.0, 2: np.nan},
1292 +2: {0: np.nan, 1: np.nan, 2: 1.0}}
1293 + ).fillna(-1.0)
1294 +
1295 +tm.assert_frame_equal(sdf.to_dense(), expected.to_dense())
1296 +
1297 +
1270 1298 class TestSparseDataFrameArithmetic(object):
1271 1299
1272 1300 def test_numeric_op_scalar(self):