You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importpandasaspdimportnumpyasnpmi=pd.MultiIndex.from_product([[1,2,3], ["a", "b"]])
ser=pd.Series([1,np.nan]*3, index=mi)
# b in index, as expectedprint("b"inser.index.levels[1])
# b still in index even though all cells with (anything, b) have been droppedser0=ser.dropna()
print("b"inser0.index.levels[1])
# resetting and setting index produces expected behavior after dropnaser1=ser.dropna().reset_index().set_index(["level_0", "level_1"])
print("b"inser1.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
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
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 havevalue
in levelx
.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
The text was updated successfully, but these errors were encountered: