Skip to content

BUG: multiindex reindex doesn't work properly #35326

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
3 tasks
chloe-wang opened this issue Jul 17, 2020 · 1 comment
Closed
3 tasks

BUG: multiindex reindex doesn't work properly #35326

chloe-wang opened this issue Jul 17, 2020 · 1 comment
Labels
Bug Duplicate Report Duplicate issue or pull request MultiIndex

Comments

@chloe-wang
Copy link

chloe-wang commented Jul 17, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
a = pd.DataFrame([[1,2,3,4],[5,6,7,8]], index=[['a','a'], ['x', 'y']])
a.reindex(['x','z'], level=1)
#       0  1  2  3
#a x  1  2  3  4

Problem description

pandas doesn't work properly for multiindex reindexing. It drops the index that didn't appear in the original dataset directly.

Expected Output

we expect pandas will reindex the second level of the multiindex to ['x' , 'z']. But it drops 'z' directly.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-109-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.1.1
setuptools : 45.2.0.post20200210
Cython : 0.29.15
pytest : 5.3.5
hypothesis : 5.5.4
sphinx : 2.4.0
blosc : None
feather : None
xlsxwriter : 1.2.7
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.12.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.3.5
pyxlsb : None

@chloe-wang chloe-wang added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 17, 2020
@simonjayhawkins
Copy link
Member

Thanks @chloe-wang for the report. This is a duplicate of #25460 so closing. LMK if I'm missing something and I'll reopen.

This issue also occurs with the first level.

>>> a.reindex(["x", "z"], level=1)
     0  1  2  3
a x  1  2  3  4
>>>
>>> a.reindex(["a", "b"], level=0)
     0  1  2  3
a x  1  2  3  4
  y  5  6  7  8
>>>
>>> idx = pd.MultiIndex.from_product([["a"], ["x", "z"]])
>>> idx
MultiIndex([('a', 'x'),
            ('a', 'z')],
           )
>>>
>>> a.reindex(idx)
       0    1    2    3
a x  1.0  2.0  3.0  4.0
  z  NaN  NaN  NaN  NaN
>>>

does reindexing using an explicit MultIndex give the expected result?

@simonjayhawkins simonjayhawkins added Duplicate Report Duplicate issue or pull request MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request MultiIndex
Projects
None yet
Development

No branches or pull requests

2 participants