Skip to content

DataFrame.unstack() with list of levels ignores fill_value #30740

Closed
@simonjayhawkins

Description

@simonjayhawkins

Code Sample, a copy-pastable example if possible

>>> import pandas as pd
>>> df = (
...     pd.DataFrame(
...         {
...             "name": ["Alice", "Bob"],
...             "score": [9.5, 8],
...             "employed": [False, True],
...             "kids": [0, 0],
...             "gender": ["female", "male"],
...         }
...     )
...     .set_index(["name", "employed", "kids", "gender"])
...     .unstack(["gender"], fill_value=0)
... )
>>> df.unstack(["employed", "kids"], fill_value=0)
          score
gender   female        male
employed  False True  False True
kids          0     0     0     0
name
Alice       9.5   NaN   0.0   NaN
Bob         NaN   0.0   NaN   8.0

Problem description

when unstacking with a list of levels on a DataFrame that already has a columns MultiIndex, fill_value is ignored.

Expected Output

>>> df.unstack("employed", fill_value=0).unstack("kids", fill_value=0)
          score
gender   female        male
employed  False True  False True
kids          0     0     0     0
name
Alice       9.5   0.0   0.0   0.0
Bob         0.0   0.0   0.0   8.0
>>>

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 4206fd4
python : 3.7.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : None.None

pandas : 0.26.0.dev0+1622.g4206fd42c
numpy : 1.17.2
pytz : 2019.3
dateutil : 2.8.0
pip : 19.3.1
setuptools : 41.6.0.post20191030
Cython : 0.29.13
pytest : 5.2.2
hypothesis : 4.36.2
sphinx : 2.2.1
blosc : None
feather : None
xlsxwriter : 1.2.2
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.9.0
pandas_datareader: None
bs4 : 4.7.1
bottleneck : 1.2.1
fastparquet : 0.3.2
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : 2.7.0
odfpy : None
openpyxl : 3.0.0
pandas_gbq : None
pyarrow : 0.15.1
pytables : None
pytest : 5.2.2
s3fs : 0.3.4
scipy : 1.3.1
sqlalchemy : 1.3.10
tables : 3.5.1
tabulate : None
xarray : 0.13.0
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.2
numba : 0.46.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions