Skip to content

Commit

Permalink
BUG: Made SparseDataFrame.fillna() fill all NaNs
Browse files Browse the repository at this point in the history
  • Loading branch information
kristopheryahoo authored and keitakurita committed May 14, 2017
1 parent 9da7798 commit 9817a51
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pandas/core/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,13 @@ def fillna(self, value, downcast=None):
if issubclass(self.dtype.type, np.floating):
value = float(value)

new_values = self.sp_values.copy()
new_values[isnull(new_values)] = value

if self._null_fill_value:
return self._simple_new(self.sp_values, self.sp_index,
return self._simple_new(new_values, self.sp_index,
fill_value=value)
else:
new_values = self.sp_values.copy()
new_values[isnull(new_values)] = value
return self._simple_new(new_values, self.sp_index,
fill_value=self.fill_value)

Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,7 @@ def test_from_scipy_correct_ordering(spmatrix):
tm.skip_if_no_package('scipy')

arr = np.arange(1, 5).reshape(2, 2)

try:
spm = spmatrix(arr)
assert spm.dtype == arr.dtype
Expand All @@ -1267,6 +1268,33 @@ def test_from_scipy_correct_ordering(spmatrix):
tm.assert_frame_equal(sdf.to_dense(), expected.to_dense())


def test_from_scipy_object_fillna(spmatrix):
# GH 16112
tm.skip_if_no_package('scipy', max_version='0.19.0')

arr = np.eye(3)
arr[1:, 0] = np.nan

try:
spm = spmatrix(arr)
assert spm.dtype == arr.dtype
except (TypeError, AssertionError):
# If conversion to sparse fails for this spmatrix type and arr.dtype,
# then the combination is not currently supported in NumPy, so we
# can just skip testing it thoroughly
return

sdf = pd.SparseDataFrame(spm).fillna(-1.0)

# Returning frame should fill all nan values with -1.0
expected = pd.SparseDataFrame({0: {0: 1.0, 1: np.nan, 2: np.nan},
1: {0: np.nan, 1: 1.0, 2: np.nan},
2: {0: np.nan, 1: np.nan, 2: 1.0}}
).fillna(-1.0)

tm.assert_frame_equal(sdf.to_dense(), expected.to_dense())


class TestSparseDataFrameArithmetic(object):

def test_numeric_op_scalar(self):
Expand Down

0 comments on commit 9817a51

Please sign in to comment.