Skip to content

SparseSeries.unstack() raises error #16614

Closed
@kernc

Description

@kernc

Code Sample, a copy-pastable example if possible

>>> s = pd.Series([1, 2], index=pd.MultiIndex.from_tuples([(0, 0), (1, 2)]))
>>> s
0  0    1
1  2    2
dtype: int64

>>> s.unstack()
     0    2
0  1.0  NaN
1  NaN  2.0

>>> s.to_sparse()
0  0    1
1  2    2
dtype: int64
BlockIndex
Block locations: array([0], dtype=int32)
Block lengths: array([2], dtype=int32

>>> s.to_sparse().unstack()
---------------------------------------------------------------------------
TypeError: expected dimension <= 1 data

Problem description

Unstacking a SparseSeries — i.e. one with a two-level index that should neatly expand into a SparseDataFrame — doesn't work, errors.

Expected Output

>>> s.to_sparse().unstack()
     0    2
0  1.0  NaN
1  NaN  2.0

>>> s.to_sparse().unstack().__class__
pandas.core.sparse.frame.SparseDataFrame

Output of pd.show_versions()

0.21.0.dev+91.g2aafce4

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, ExplodeSparseSparse Data Type

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions