Skip to content

BUG: Made SparseDataFrame.fillna() fill all NaNs #16892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ Groupby/Resample/Rolling
Sparse
^^^^^^

- Bug in :func:`SparseDataFrame.fillna` not filling all NaNs when frame was instantiated from SciPy sparse matrix (:issue:`16112`)


Reshaping
Expand Down
13 changes: 5 additions & 8 deletions pandas/core/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,11 @@ def fillna(self, value, downcast=None):
if issubclass(self.dtype.type, np.floating):
value = float(value)

if self._null_fill_value:
return self._simple_new(self.sp_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)
new_values = np.where(isnull(self.sp_values), value, self.sp_values)
fill_value = value if self._null_fill_value else self.fill_value

return self._simple_new(new_values, self.sp_index,
fill_value=fill_value)

def sum(self, axis=0, *args, **kwargs):
"""
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,32 @@ def test_from_scipy_correct_ordering(spmatrix):
tm.assert_frame_equal(sdf.to_dense(), expected.to_dense())


def test_from_scipy_fillna(spmatrix):
# GH 16112
tm.skip_if_no_package('scipy')

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([[1, -1, -1],
[-1, 1, -1],
[-1, -1, 1.]])

tm.assert_numpy_array_equal(sdf.values, expected.values)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change this to assert_sparse_frame_equal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because they are not. Or I can if I may either densify them first, or use expected.fillna(). The expected frame, as constructed, contains SparseArrays of shape (3,) whereas sdf of shape (1,) (even after filling). The internals seem intricate, and I'd prefer not to touch them here. :S

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean. What exactly does the result look like here? IOW show the internal structure. If these are not equal then that is a bigger problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, SparseSeries {0: nan, 1: 1, 2: nan} can have the underlying SparseArray of shape (3,) ([nan, 1, nan], sparse block of length 3, start index 0) or of shape (1,) ([1], sparse block of length 1, start index 1).

I did manage something.



class TestSparseDataFrameArithmetic(object):

def test_numeric_op_scalar(self):
Expand Down