Skip to content

Commit

Permalink
BUG-16807-1 SparseFrame fills with default_fill_value if data is None (
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinZhengBC authored and gfyoung committed Mar 3, 2019
1 parent 0c193c6 commit 42b4c97
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ Sparse
^^^^^^

- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`)
-
- Bug in :class:`SparseFrame` constructor where passing ``None`` as the data would cause ``default_fill_value`` to be ignored (:issue:`16807`)
-


Expand Down
4 changes: 2 additions & 2 deletions pandas/core/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def __init__(self, data=None, index=None, columns=None, default_kind=None,
columns = Index([])
else:
for c in columns:
data[c] = SparseArray(np.nan, index=index,
kind=self._default_kind,
data[c] = SparseArray(self._default_fill_value,
index=index, kind=self._default_kind,
fill_value=self._default_fill_value)
mgr = to_manager(data, columns, index)
if dtype is not None:
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/sparse/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,19 @@ def test_type_coercion_at_construction(self):
default_fill_value=0)
tm.assert_sp_frame_equal(result, expected)

def test_default_dtype(self):
result = pd.SparseDataFrame(columns=list('ab'), index=range(2))
expected = pd.SparseDataFrame([[np.nan, np.nan], [np.nan, np.nan]],
columns=list('ab'), index=range(2))
tm.assert_sp_frame_equal(result, expected)

def test_nan_data_with_int_dtype_raises_error(self):
sdf = pd.SparseDataFrame([[np.nan, np.nan], [np.nan, np.nan]],
columns=list('ab'), index=range(2))
msg = "Cannot convert non-finite values"
with pytest.raises(ValueError, match=msg):
pd.SparseDataFrame(sdf, dtype=np.int64)

def test_dtypes(self):
df = DataFrame(np.random.randn(10000, 4))
df.loc[:9998] = np.nan
Expand Down Expand Up @@ -1263,6 +1276,14 @@ def test_notna(self):
'B': [True, False, True, True, False]})
tm.assert_frame_equal(res.to_dense(), exp)

def test_default_fill_value_with_no_data(self):
# GH 16807
expected = pd.SparseDataFrame([[1.0, 1.0], [1.0, 1.0]],
columns=list('ab'), index=range(2))
result = pd.SparseDataFrame(columns=list('ab'), index=range(2),
default_fill_value=1.0)
tm.assert_frame_equal(expected, result)


class TestSparseDataFrameArithmetic(object):

Expand Down

0 comments on commit 42b4c97

Please sign in to comment.