Skip to content

BUG: (GH3997) Fix for dropna=False in stack #3999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2013
Merged
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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ pandas 0.11.1
- csv parsers would loop infinitely if ``iterator=True`` but no ``chunksize`` was
specified (:issue:`3967`), python parser failing with ``chunksize=1``
- Fix index name not propogating when using ``shift``
- Fixed dropna=False being ignored with multi-index stack (:issue:`3997`)

.. _Gh3616: https://github.com/pydata/pandas/issues/3616

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def stack(frame, level=-1, dropna=True):
level = frame.columns._get_level_number(level)

if isinstance(frame.columns, MultiIndex):
return _stack_multi_columns(frame, level=level, dropna=True)
return _stack_multi_columns(frame, level=level, dropna=dropna)
elif isinstance(frame.index, MultiIndex):
new_levels = list(frame.index.levels)
new_levels.append(frame.columns)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,19 @@ def test_stack_multiple_bug(self):
xp.columns.name = 'Params'
assert_frame_equal(rs, xp)

def test_stack_dropna(self):
# GH #3997
df = pd.DataFrame({'A': ['a1', 'a2'],
'B': ['b1', 'b2'],
'C': [1, 1]})
df = df.set_index(['A', 'B'])

stacked = df.unstack().stack(dropna=False)
self.assertTrue(len(stacked) > len(stacked.dropna()))

stacked = df.unstack().stack(dropna=True)
assert_frame_equal(stacked, stacked.dropna())

def test_unstack_multiple_hierarchical(self):
df = DataFrame(index=[[0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 1, 1],
Expand Down