Skip to content

Commit 0c5f154

Browse files
committed
BUG: Fix issue with SparseArray
1 parent e5ec4ee commit 0c5f154

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

pandas/core/internals.py

+5
Original file line numberDiff line numberDiff line change
@@ -2756,6 +2756,11 @@ class SparseBlock(NonConsolidatableMixIn, Block):
27562756
_holder = SparseArray
27572757
_concatenator = staticmethod(_concat._concat_sparse)
27582758

2759+
def __init__(self, values, placement, ndim=None, fastpath=False, **kwargs):
2760+
super(SparseBlock, self).__init__(self._holder(values),
2761+
placement, ndim=ndim,
2762+
fastpath=fastpath, **kwargs)
2763+
27592764
@property
27602765
def shape(self):
27612766
return (len(self.mgr_locs), self.sp_index.length)

pandas/core/reshape/reshape.py

-4
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,6 @@ def check_len(item, name):
841841
with_dummies = []
842842
else:
843843
with_dummies = [data.drop(columns_to_encode, axis=1)]
844-
if sparse:
845-
with_dummies = [SparseDataFrame(with_dummies[0],
846-
index=with_dummies.index,
847-
default_fill_value=0)]
848844

849845
for (col, pre, sep) in zip(columns_to_encode, prefix, prefix_sep):
850846

pandas/core/sparse/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pandas.core.dtypes.cast import maybe_upcast, find_common_type
1515
from pandas.core.dtypes.common import _ensure_platform_int, is_scipy_sparse
1616

17-
from pandas.core.common import _try_sort
17+
from pandas.core.common import _try_sort, is_sparse
1818
from pandas.compat.numpy import function as nv
1919
from pandas.core.index import Index, MultiIndex, _ensure_index
2020
from pandas.core.series import Series
@@ -306,7 +306,7 @@ def to_dense(self):
306306
-------
307307
df : DataFrame
308308
"""
309-
data = {k: v.to_dense() for k, v in compat.iteritems(self)}
309+
data = {k: v.to_dense() if is_sparse(v) else v for k, v in compat.iteritems(self)}
310310
return DataFrame(data, index=self.index, columns=self.columns)
311311

312312
def _apply_columns(self, func):

pandas/tests/sparse/test_array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def test_to_dense(self):
465465
with tm.assert_produces_warning(FutureWarning,
466466
check_stacklevel=False):
467467
SparseArray(vals).to_dense(fill=2)
468-
468+
469469
def test_getitem(self):
470470
def _checkit(i):
471471
assert_almost_equal(self.arr[i], self.arr.values[i])

pandas/tests/sparse/test_frame.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas as pd
1010
from distutils.version import LooseVersion
1111

12-
from pandas import Series, DataFrame, bdate_range, Panel
12+
from pandas import Series, DataFrame, bdate_range, Panel, get_dummies
1313
from pandas.core.dtypes.common import (
1414
is_bool_dtype,
1515
is_float_dtype,
@@ -323,7 +323,13 @@ def test_density(self):
323323
assert df.density == 0.75
324324

325325
def test_sparse_to_dense(self):
326-
pass
326+
# See gh-
327+
sdf = DataFrame.from_items([('GDP', [1, 2]),('Nation', ['AB', 'CD'])])
328+
sdf = get_dummies(sdf, columns=['Nation'], sparse=True)
329+
df = sdf.to_dense()
330+
331+
tm.assert_frame_equals(df, sdf)
332+
327333

328334
def test_sparse_series_ops(self):
329335
self._check_frame_ops(self.frame)

0 commit comments

Comments
 (0)