Skip to content

BUG: using df.drop(labels=x, level='level_name') does not update df.index.levels #36227

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
1 of 3 tasks
jeffliu-LL opened this issue Sep 8, 2020 · 3 comments · Fixed by #40953
Closed
1 of 3 tasks

Comments

@jeffliu-LL
Copy link

  • 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

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.Series(np.random.randn(8), index=index)
s2 = s.drop(labels='one', level='second') #drop the label 'one' from the second level
print(s2.index.levels) #the levels still include the label 'one' in the second level

outputs:
FrozenList([['bar', 'baz', 'foo', 'qux'], ['one', 'two']])
despite dropping one from the second level

Problem description

When calling df.drop(labels='one', level='second'), one would expect that the dropped label would no longer appear in df.index.levels since all rows with that label are dropped. However, the label persists in the df.index.levels despite not showing up in df.index.get_level_values('second'). I would expect that df.index.levels only include labels of entries currently in df, not ones that were previously there but dropped.

Expected Output

FrozenList([['bar', 'baz', 'foo', 'qux'], ['two']])

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.14.182-llgrid-10ms
Version : #1 SMP Fri May 29 11:57:47 EDT 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C
LANG : C.UTF-8
LOCALE : None.None

pandas : 1.1.0
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.3.1.post20200810
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.17.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.0
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@jeffliu-LL jeffliu-LL added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 8, 2020
@phofl
Copy link
Member

phofl commented Sep 8, 2020

Hi,

thanks for your report. This is actually explained in https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html#defined-levels

@phofl phofl added MultiIndex and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 8, 2020
@jeffliu-LL
Copy link
Author

Thanks for the quick response, and the linked documentation does explain the behavior.

I would suggest making a note of this in the documentation of pandas.DataFrame.drop. Currently, the documentation says "When using a multi-index, labels on different levels can be removed by specifying the level" which is not clear whether the dropped label remains in the multi-index or not.

@phofl
Copy link
Member

phofl commented Sep 8, 2020

Would you like to contribute a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants