Skip to content

Commit

Permalink
BUG: to_sparse was dropping MultiIndex column names, #11600
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeke authored and jreback committed Nov 19, 2015
1 parent 4ffc3ef commit 207e0ce
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,4 @@ Bug Fixes
- Bug in ``Series.quantile`` with empty list results has ``Index`` with ``object`` dtype (:issue:`11588`)
- Bug in ``pd.merge`` results in empty ``Int64Index`` rather than ``Index(dtype=object)`` when the merge result is empty (:issue:`11588`)
- Bug in ``remove_unused_categories`` when having ``NaN`` values (:issue:`11599`).
- Bug in ``DataFrame.to_sparse()`` loses column names for MultiIndexes (:issue:`11600`)
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ def to_sparse(self, fill_value=None, kind='block'):
y : SparseDataFrame
"""
from pandas.core.sparse import SparseDataFrame
return SparseDataFrame(self._series, index=self.index,
return SparseDataFrame(self._series, index=self.index, columns=self.columns,
default_kind=kind,
default_fill_value=fill_value)

Expand Down
2 changes: 1 addition & 1 deletion pandas/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def to_dense(self):
df : DataFrame
"""
data = dict((k, v.to_dense()) for k, v in compat.iteritems(self))
return DataFrame(data, index=self.index)
return DataFrame(data, index=self.index,columns=self.columns)

def astype(self, dtype):
raise NotImplementedError
Expand Down
20 changes: 19 additions & 1 deletion pandas/sparse/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pandas as pd
dec = np.testing.dec

from pandas.util.testing import (assert_almost_equal, assert_series_equal,
from pandas.util.testing import (assert_almost_equal, assert_series_equal, assert_index_equal,
assert_frame_equal, assert_panel_equal, assertRaisesRegexp,
assert_numpy_array_equal, assert_attr_equal)
from numpy.testing import assert_equal
Expand Down Expand Up @@ -770,6 +770,24 @@ def test_combine_first(self):
assert_sp_series_equal(result, result2)
assert_sp_series_equal(result, expected)

class TestSparseHandlingMultiIndexes(tm.TestCase):

def setUp(self):
miindex = pd.MultiIndex.from_product([["x","y"], ["10","20"]],names=['row-foo', 'row-bar'])
micol = pd.MultiIndex.from_product([['a','b','c'], ["1","2"]],names=['col-foo', 'col-bar'])
dense_multiindex_frame = pd.DataFrame(index=miindex, columns=micol).sortlevel().sortlevel(axis=1)
self.dense_multiindex_frame = dense_multiindex_frame.fillna(value=3.14)

def test_to_sparse_preserve_multiindex_names_columns(self):
sparse_multiindex_frame = self.dense_multiindex_frame.to_sparse().copy()
assert_index_equal(sparse_multiindex_frame.columns,self.dense_multiindex_frame.columns)

def test_round_trip_preserve_multiindex_names(self):
sparse_multiindex_frame = self.dense_multiindex_frame.to_sparse()
round_trip_multiindex_frame = sparse_multiindex_frame.to_dense()
assert_frame_equal(self.dense_multiindex_frame,round_trip_multiindex_frame,
check_column_type=True,check_names=True)


class TestSparseSeriesScipyInteraction(tm.TestCase):
# Issue 8048: add SparseSeries coo methods
Expand Down

0 comments on commit 207e0ce

Please sign in to comment.