-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: slicing a MultiIndex does not preserve the sequence of the index since pandas 1.2.0rc0 #40978
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
Comments
The reordering is expected since you've got duplicates in the second level. But the exact result is a bit unexpected. I would have expected the same as in
see #31330 |
Ok, I get the logic behind it, that the resulting Yet, it is a little bit unsatisfying because it leaves me with the question, how to efficiently select items of a pandas object while preserving the original sequence. What I found fo that is something like s = pd.Series([1,2,3,4,5], index=[0,9,8,7,6])
s.iloc[np.sort(s.index.get_indexer([0,8,9]))] which results in
as desired. However, it has the Any better ideas for that? |
Intersection with sort=False is first thing which comes to mind. Loc should in theory select in the order your indexer is, so your incoming sequence has to be the same order somehow. |
Cool. That seems to do it. Thanks a lot, closing issue |
The ordering is wrong for duplicates like in your example, will fix this shortly but lets leave open till then |
Just for completeness: in order to slice a MultiIndex while preserving the order like I tried in my demo above df_sliced = df.loc[df.index.isin(all_items, level=1), :] does the trick. |
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.
Between 1.1.5 and 1.2.0rc2 the behavior of slicing MultiIndexes has changed. Up to 1.1.5 slicing a MultiIndex like
df.loc[(slice(None), somel_items), :]
preserved the sequence of the slicedDataFrame
. From 1.2.0rc0 the sequence is changed.Code Sample
Problem description
Running the sample code in 1.1.5 gives the following output:
whereas in 1.2.4 it gives
I am not sure if this is necessarily a problem, I stumbled across it because a test suite that used
pd.testing.assert_frame_equal()
failed due to this. So either the actual sequence should be preserved when slicing aDataFrame
orpd.testing.assert_frame_equal()
should not fail if the sequence is shuffled (but the index is correct).Expected Output
As discussed above.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 2cb9652
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-71-lowlatency
Version : #79-Ubuntu SMP PREEMPT Wed Mar 24 12:38:51 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8
pandas : 1.2.4 (resp. 1.1.5)
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 50.3.0.post20201006
Cython : 0.29.23
pytest : 6.2.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : None
tabulate : None
xarray : 0.17.0
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: