Skip to content

Commit 42bd3f5

Browse files
committed
Merge pull request #3999 from mtkni/stack_dropna
BUG: (GH3997) Fix for dropna=False in stack
2 parents 01a8648 + 6a14fbf commit 42bd3f5

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ pandas 0.11.1
276276
- csv parsers would loop infinitely if ``iterator=True`` but no ``chunksize`` was
277277
specified (:issue:`3967`), python parser failing with ``chunksize=1``
278278
- Fix index name not propogating when using ``shift``
279+
- Fixed dropna=False being ignored with multi-index stack (:issue:`3997`)
279280

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

pandas/core/reshape.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ def stack(frame, level=-1, dropna=True):
492492
level = frame.columns._get_level_number(level)
493493

494494
if isinstance(frame.columns, MultiIndex):
495-
return _stack_multi_columns(frame, level=level, dropna=True)
495+
return _stack_multi_columns(frame, level=level, dropna=dropna)
496496
elif isinstance(frame.index, MultiIndex):
497497
new_levels = list(frame.index.levels)
498498
new_levels.append(frame.columns)

pandas/tests/test_multilevel.py

+13
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,19 @@ def test_stack_multiple_bug(self):
948948
xp.columns.name = 'Params'
949949
assert_frame_equal(rs, xp)
950950

951+
def test_stack_dropna(self):
952+
# GH #3997
953+
df = pd.DataFrame({'A': ['a1', 'a2'],
954+
'B': ['b1', 'b2'],
955+
'C': [1, 1]})
956+
df = df.set_index(['A', 'B'])
957+
958+
stacked = df.unstack().stack(dropna=False)
959+
self.assertTrue(len(stacked) > len(stacked.dropna()))
960+
961+
stacked = df.unstack().stack(dropna=True)
962+
assert_frame_equal(stacked, stacked.dropna())
963+
951964
def test_unstack_multiple_hierarchical(self):
952965
df = DataFrame(index=[[0, 0, 0, 0, 1, 1, 1, 1],
953966
[0, 0, 1, 1, 0, 0, 1, 1],

0 commit comments

Comments
 (0)