diff --git a/pandas/sparse/frame.py b/pandas/sparse/frame.py index c38936b55696f..58d3a90e9b7db 100644 --- a/pandas/sparse/frame.py +++ b/pandas/sparse/frame.py @@ -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 diff --git a/pandas/sparse/series.py b/pandas/sparse/series.py index 8be9e2b5c7d75..ae9bda78d2c61 100644 --- a/pandas/sparse/series.py +++ b/pandas/sparse/series.py @@ -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 diff --git a/pandas/sparse/tests/test_sparse.py b/pandas/sparse/tests/test_sparse.py index 87ffa0822aedf..076122b68f476 100644 --- a/pandas/sparse/tests/test_sparse.py +++ b/pandas/sparse/tests/test_sparse.py @@ -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():