Skip to content

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

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

Closed
simonjayhawkins opened this issue Jan 6, 2020 · 1 comment · Fixed by #30838
Closed

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

simonjayhawkins opened this issue Jan 6, 2020 · 1 comment · Fixed by #30838
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@simonjayhawkins
Copy link
Member

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

@simonjayhawkins simonjayhawkins added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jan 6, 2020
@tanmay1618
Copy link
Contributor

tanmay1618 commented Jan 9, 2020

I have submitted a PR request for this.
#30838

tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
tanmay1618 added a commit to tanmay1618/pandas that referenced this issue Jan 9, 2020
@simonjayhawkins simonjayhawkins added this to the 1.0 milestone Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants