Skip to content

BUG: .drop with MultiIndex does not drop indices from MultiIndexlevels. #45654

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
2 of 3 tasks
ahalev opened this issue Jan 27, 2022 · 3 comments
Closed
2 of 3 tasks

BUG: .drop with MultiIndex does not drop indices from MultiIndexlevels. #45654

ahalev opened this issue Jan 27, 2022 · 3 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@ahalev
Copy link

ahalev commented Jan 27, 2022

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

 
import pandas as pd
>>> df = pd.DataFrame(index=pd.MultiIndex.from_product([[1,2], [3, 4]]), columns=['a', 'b'])
>>> df
       a    b
1 3  NaN  NaN
  4  NaN  NaN
2 3  NaN  NaN
  4  NaN  NaN

>>> df_2 = df.drop((1,))
>>> df_2
       a    b
2 3  NaN  NaN
  4  NaN  NaN
>>> df_2.index
MultiIndex([(2, 3),
            (2, 4)],
           )
>>> df_2.index.levels[0]
Int64Index([1, 2], dtype='int64')

Issue Description

When dropping rows from a DataFrame with a Pandas MultiIndex, it correctly drops the corresponding indices from the overall MultiIndex (see above) but the corresponding keys are not dropped from MultiIndex.levels, as they should be.

Expected Behavior

In the example above, df_2.index.levels[0] should be Int64Index([2], dtype='int64').

Installed Versions

NSTALLED VERSIONS

commit : 66e3805
python : 3.7.12.final.0
python-bits : 64
OS : Darwin
OS-release : 21.1.0
Version : Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:01 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T6000
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.3.5
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 60.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.31.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@ahalev ahalev added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 27, 2022
@purna135
Copy link
Contributor

HI, I would like to work on this issue.

@phofl
Copy link
Member

phofl commented Jan 27, 2022

This works as it should. They are kept on purpose.

We had a few issues about this in the past, they should be on the issue tracker

@phofl phofl closed this as completed Jan 27, 2022
@phofl phofl added this to the No action milestone Jan 27, 2022
@phofl
Copy link
Member

phofl commented Jan 27, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants