Skip to content

Commit 391f46a

Browse files
wholmgrenjreback
authored andcommitted
BUG: allow for empty SparseSeries SparsePanel constructors (GH9272)
1 parent d349adc commit 391f46a

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

doc/source/whatsnew/v0.16.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,6 @@ Bug Fixes
217217
- Fixes issue with ``index_col=False`` when ``usecols`` is also specified in ``read_csv``. (:issue:`9082`)
218218
- Bug where ``wide_to_long`` would modify the input stubnames list (:issue:`9204`)
219219
- Bug in to_sql not storing float64 values using double precision. (:issue:`9009`)
220+
221+
222+
- ``SparseSeries`` and ``SparsePanel`` now accept zero argument constructors (same as their non-sparse counterparts) (:issue:`9272`).

pandas/sparse/panel.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ class SparsePanel(Panel):
6565
_typ = 'panel'
6666
_subtyp = 'sparse_panel'
6767

68-
def __init__(self, frames, items=None, major_axis=None, minor_axis=None,
68+
def __init__(self, frames=None, items=None, major_axis=None, minor_axis=None,
6969
default_fill_value=np.nan, default_kind='block',
7070
copy=False):
71+
72+
if frames is None:
73+
frames = {}
74+
7175
if isinstance(frames, np.ndarray):
7276
new_frames = {}
7377
for item, vals in zip(items, frames):

pandas/sparse/series.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SparseSeries(Series):
103103
"""
104104
_subtyp = 'sparse_series'
105105

106-
def __init__(self, data, index=None, sparse_index=None, kind='block',
106+
def __init__(self, data=None, index=None, sparse_index=None, kind='block',
107107
fill_value=None, name=None, dtype=None, copy=False,
108108
fastpath=False):
109109

@@ -115,6 +115,9 @@ def __init__(self, data, index=None, sparse_index=None, kind='block',
115115
if copy:
116116
data = data.copy()
117117
else:
118+
119+
if data is None:
120+
data = []
118121

119122
is_sparse_array = isinstance(data, SparseArray)
120123
if fill_value is None:

pandas/sparse/tests/test_sparse.py

+13
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ def test_constructor_nonnan(self):
280280
arr = [0, 0, 0, nan, nan]
281281
sp_series = SparseSeries(arr, fill_value=0)
282282
assert_equal(sp_series.values.values, arr)
283+
284+
# GH 9272
285+
def test_constructor_empty(self):
286+
sp = SparseSeries()
287+
self.assertEqual(len(sp.index), 0)
283288

284289
def test_copy_astype(self):
285290
cop = self.bseries.astype(np.float64)
@@ -862,6 +867,7 @@ def test_constructor_ndarray(self):
862867
ValueError, "^Column length", SparseDataFrame, self.frame.values,
863868
columns=self.frame.columns[:-1])
864869

870+
# GH 9272
865871
def test_constructor_empty(self):
866872
sp = SparseDataFrame()
867873
self.assertEqual(len(sp.index), 0)
@@ -1605,6 +1611,13 @@ def test_constructor(self):
16051611
with tm.assertRaisesRegexp(TypeError,
16061612
"input must be a dict, a 'list' was passed"):
16071613
SparsePanel(['a', 'b', 'c'])
1614+
1615+
# GH 9272
1616+
def test_constructor_empty(self):
1617+
sp = SparsePanel()
1618+
self.assertEqual(len(sp.items), 0)
1619+
self.assertEqual(len(sp.major_axis), 0)
1620+
self.assertEqual(len(sp.minor_axis), 0)
16081621

16091622
def test_from_dict(self):
16101623
fd = SparsePanel.from_dict(self.data_dict)

0 commit comments

Comments
 (0)