Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion pandas/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def __init__(self, data=None, index=None, columns=None,
columns = Index([])
else:
for c in columns:
sdict[c] = Series(np.nan, index=index)
sdict[c] = SparseSeries(np.nan, index=index,
kind=self.default_kind,
fill_value=self.default_fill_value)

self._series = sdict
self.columns = columns
Expand Down
24 changes: 17 additions & 7 deletions pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,23 @@ def __new__(cls, data, index=None, sparse_index=None, kind='block',
if index is None:
raise Exception('must pass index!')

values = np.empty(len(index))
values.fill(data)

# TODO: more efficient

values, sparse_index = make_sparse(values, kind=kind,
fill_value=fill_value)
length = len(index)

if data == fill_value or (np.isnan(data)
and np.isnan(fill_value)):
if kind == 'block':
sparse_index = BlockIndex(length, [], [])
else:
sparse_index = IntIndex(length, [])
values = np.array([])
else:
if kind == 'block':
locs, lens = ([0], [length]) if length else ([], [])
sparse_index = BlockIndex(length, locs, lens)
else:
sparse_index = IntIndex(length, index)
values = np.empty(length)
values.fill(data)

else:
# array-like
Expand Down
6 changes: 6 additions & 0 deletions pandas/sparse/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,12 @@ def test_constructor(self):
assert_almost_equal([0, 0, 0, 0, 1, 2, 3, 4, 5, 6],
self.zframe['A'].values)

# construct no data
sdf = SparseDataFrame(columns=np.arange(10), index=np.arange(10))
for col, series in sdf.iteritems():
self.assert_(isinstance(series, SparseSeries))


# construct from nested dict
data = {}
for c, s in self.frame.iteritems():
Expand Down