Skip to content

Commit

Permalink
BUG: Fixed GH16112 except for dia_format matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
kristopheryahoo committed Apr 30, 2017
1 parent 075eca1 commit 9f4c4e5
Show file tree
Hide file tree
Showing 2 changed files with 46 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
42 changes: 42 additions & 0 deletions pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,48 @@ def test_from_to_scipy_object(spmatrix, fill_value):
assert sdf.to_coo().dtype == res_dtype


def test_from_scipy_object_fillna(spmatrix):
columns = list('cd')
index = list('ab')
tm.skip_if_no_package('scipy', max_version='0.19.0')

# Explicitly convert one zero to np.nan
arr = np.eye(2)
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, index=index, columns=columns).fillna(-1.0)

# Returning frame should fill all nan values with -1.0
expected = pd.SparseDataFrame({"c": {"a": 1.0, "b": np.nan},
"d": {"a": np.nan, "b": 1.0}}).fillna(-1.0)
expected_bsr = pd.SparseDataFrame({"c": {"a": 1.0, "b": np.nan},
"d": {"a": 0.0, "b": 1.0}}).fillna(-1.0)

from scipy.sparse.bsr import bsr_matrix
from scipy.sparse.dia import dia_matrix
if spmatrix == bsr_matrix:
# A SparseDataFrame from a bsr matrix does not fill 0s
# Therefore, only the explicit nan value needs to be filled with -1
tm.assert_frame_equal(sdf.to_dense(), expected_bsr.to_dense())
elif spmatrix == dia_matrix:
# the dia matrix has a bug of a different nature,
# so is currently passed in this test suite
pass
else:
# The internal representations can differ.
# This test is here to ensure that all nan values are filled,
# regardless of origin.
tm.assert_frame_equal(sdf.to_dense(), expected.to_dense())


class TestSparseDataFrameArithmetic(tm.TestCase):

def test_numeric_op_scalar(self):
Expand Down

0 comments on commit 9f4c4e5

Please sign in to comment.