Skip to content

BUG: multiindex level values do not update when rows are removed #41362

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
ndwarshuis opened this issue May 7, 2021 · 2 comments
Closed
2 of 3 tasks

BUG: multiindex level values do not update when rows are removed #41362

ndwarshuis opened this issue May 7, 2021 · 2 comments
Labels
Duplicate Report Duplicate issue or pull request MultiIndex

Comments

@ndwarshuis
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

import pandas as pd
import numpy as np

mi = pd.MultiIndex.from_product([[1,2,3], ["a", "b"]])
ser = pd.Series([1,np.nan]*3, index = mi)

# b in index, as expected
print("b" in ser.index.levels[1])

# b still in index even though all cells with (anything, b) have been dropped
ser0 = ser.dropna()
print("b" in ser0.index.levels[1])

# resetting and setting index produces expected behavior after dropna
ser1 = ser.dropna().reset_index().set_index(["level_0", "level_1"])
print("b" in ser1.index.levels[1])

Problem description

I'm not sure if this is a bug or if there is simply a function that does what I expect that I can't find.

When dropping NaN's from a series/dataframe with a multiindex (or just slicing it arbitrarily for that matter), removing all rows that correspond to one value in the index doesn't actually remove it from the index. In that case, tests like value in series.index.levels[x] will return true even though there are no rows that have value in level x.

Furthermore, this is inconsistent in comparison to series/dataframes with a non-multiindex; in these cases the index values are updated as the rows change.

Expected Output

It is expected that the values in each level of the multiindex reflect what is currently in the dataframe/series. The workaround (the last two lines in the code above) produce the desired behavior.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.9.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.33-1-lts-native
Version : #1 SMP PREEMPT Sun, 02 May 2021 04:17:56 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 49.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.1
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : 1.4.6
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@ndwarshuis ndwarshuis added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 7, 2021
@phofl
Copy link
Member

phofl commented May 7, 2021

Hi, thanks for your report. This is expected, see #36227

@phofl phofl closed this as completed May 7, 2021
@phofl phofl added the Duplicate Report Duplicate issue or pull request label May 7, 2021
@phofl phofl added this to the No action milestone May 7, 2021
@phofl phofl added MultiIndex and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 7, 2021
@ndwarshuis
Copy link
Author

Thank you, I figured it was for performance reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request MultiIndex
Projects
None yet
Development

No branches or pull requests

2 participants