Skip to content

Commit 42b4c97

Browse files
JustinZhengBCgfyoung
authored andcommitted
BUG-16807-1 SparseFrame fills with default_fill_value if data is None (pandas-dev#24842)
Closes pandas-devgh-16807.
1 parent 0c193c6 commit 42b4c97

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Sparse
247247
^^^^^^
248248

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

253253

pandas/core/sparse/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ def __init__(self, data=None, index=None, columns=None, default_kind=None,
124124
columns = Index([])
125125
else:
126126
for c in columns:
127-
data[c] = SparseArray(np.nan, index=index,
128-
kind=self._default_kind,
127+
data[c] = SparseArray(self._default_fill_value,
128+
index=index, kind=self._default_kind,
129129
fill_value=self._default_fill_value)
130130
mgr = to_manager(data, columns, index)
131131
if dtype is not None:

pandas/tests/sparse/frame/test_frame.py

+21
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ def test_type_coercion_at_construction(self):
270270
default_fill_value=0)
271271
tm.assert_sp_frame_equal(result, expected)
272272

273+
def test_default_dtype(self):
274+
result = pd.SparseDataFrame(columns=list('ab'), index=range(2))
275+
expected = pd.SparseDataFrame([[np.nan, np.nan], [np.nan, np.nan]],
276+
columns=list('ab'), index=range(2))
277+
tm.assert_sp_frame_equal(result, expected)
278+
279+
def test_nan_data_with_int_dtype_raises_error(self):
280+
sdf = pd.SparseDataFrame([[np.nan, np.nan], [np.nan, np.nan]],
281+
columns=list('ab'), index=range(2))
282+
msg = "Cannot convert non-finite values"
283+
with pytest.raises(ValueError, match=msg):
284+
pd.SparseDataFrame(sdf, dtype=np.int64)
285+
273286
def test_dtypes(self):
274287
df = DataFrame(np.random.randn(10000, 4))
275288
df.loc[:9998] = np.nan
@@ -1263,6 +1276,14 @@ def test_notna(self):
12631276
'B': [True, False, True, True, False]})
12641277
tm.assert_frame_equal(res.to_dense(), exp)
12651278

1279+
def test_default_fill_value_with_no_data(self):
1280+
# GH 16807
1281+
expected = pd.SparseDataFrame([[1.0, 1.0], [1.0, 1.0]],
1282+
columns=list('ab'), index=range(2))
1283+
result = pd.SparseDataFrame(columns=list('ab'), index=range(2),
1284+
default_fill_value=1.0)
1285+
tm.assert_frame_equal(expected, result)
1286+
12661287

12671288
class TestSparseDataFrameArithmetic(object):
12681289

0 commit comments

Comments
 (0)