Skip to content

Commit eec7f57

Browse files
datapythonistajreback
authored andcommitted
BUG adding support for dense Series in the SparseDataFrame constructor, and providing useful error messages for other types (#19374) (#19377)
1 parent 1d7b075 commit eec7f57

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ Groupby/Resample/Rolling
512512
Sparse
513513
^^^^^^
514514

515-
-
515+
- Bug in which creating a ``SparseDataFrame`` from a dense ``Series`` or an unsupported type raised an uncontrolled exception (:issue:`19374`)
516516
-
517517
-
518518

pandas/core/sparse/frame.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ def __init__(self, data=None, index=None, columns=None, default_kind=None,
9595
dtype=dtype, copy=copy)
9696
elif isinstance(data, DataFrame):
9797
mgr = self._init_dict(data, data.index, data.columns, dtype=dtype)
98+
elif isinstance(data, Series):
99+
mgr = self._init_dict(data.to_frame(), data.index,
100+
columns=None, dtype=dtype)
98101
elif isinstance(data, BlockManager):
99102
mgr = self._init_mgr(data, axes=dict(index=index, columns=columns),
100103
dtype=dtype, copy=copy)
@@ -116,6 +119,10 @@ def __init__(self, data=None, index=None, columns=None, default_kind=None,
116119
mgr = to_manager(data, columns, index)
117120
if dtype is not None:
118121
mgr = mgr.astype(dtype)
122+
else:
123+
msg = ('SparseDataFrame called with unkown type "{data_type}" '
124+
'for data argument')
125+
raise TypeError(msg.format(data_type=type(data).__name__))
119126

120127
generic.NDFrame.__init__(self, mgr)
121128

pandas/tests/sparse/frame/test_frame.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,29 @@ def test_constructor_from_series(self):
199199
# without sparse value raises error
200200
# df2 = SparseDataFrame([x2_sparse, y])
201201

202+
def test_constructor_from_dense_series(self):
203+
# GH 19393
204+
# series with name
205+
x = Series(np.random.randn(10000), name='a')
206+
result = SparseDataFrame(x)
207+
expected = x.to_frame().to_sparse()
208+
tm.assert_sp_frame_equal(result, expected)
209+
210+
# series with no name
211+
x = Series(np.random.randn(10000))
212+
result = SparseDataFrame(x)
213+
expected = x.to_frame().to_sparse()
214+
tm.assert_sp_frame_equal(result, expected)
215+
216+
def test_constructor_from_unknown_type(self):
217+
# GH 19393
218+
class Unknown:
219+
pass
220+
with pytest.raises(TypeError,
221+
message='SparseDataFrame called with unkown type '
222+
'"Unknown" for data argument'):
223+
SparseDataFrame(Unknown())
224+
202225
def test_constructor_preserve_attr(self):
203226
# GH 13866
204227
arr = pd.SparseArray([1, 0, 3, 0], dtype=np.int64, fill_value=0)

0 commit comments

Comments
 (0)