Skip to content

Dropna=False is ignored for MultiIndex stack. #3997

Closed
@miketkelly

Description

@miketkelly

>>> df = pd.DataFrame({'A': ['a1', 'a2'], 'B': ['b1', 'b2'], 'C': [1, 1]})
>>> df
    A   B  C
0  a1  b1  1
1  a2  b2  1

# Unstack introduces some NaNs
>>> df.set_index(['A', 'B']).unstack()
     C    
B   b1  b2
A         
a1   1 NaN
a2 NaN   1

# Stack with dropna=False should keep the NaNs, but doesn't.
>>> df.set_index(['A', 'B']).unstack().stack(dropna=False)
       C
A  B    
a1 b1  1
a2 b2  1

# This is what I expected to get
>>> df.set_index(['A', 'B']).unstack().stack(dropna=False)
        C
A  B     
a1 b1   1
   b2 NaN
a2 b1 NaN
   b2   1

The problem seems to be with reshape.py, line 495 with forces dropna=True.

if isinstance(frame.columns, MultiIndex):
    return _stack_multi_columns(frame, level=level, dropna=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions