BUG: MultiIndex intersection with sort=False does not preserve order · Issue #31325 · pandas-dev/pandas (original) (raw)
import pandas as pd
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] tuples = list(zip(*arrays)) idx = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
left = idx[2::-1] print(left) #> MultiIndex([('baz', 'one'), #> ('bar', 'two'), #> ('bar', 'one')], #> names=['first', 'second']) right = idx[:5] print(right) #> MultiIndex([('bar', 'one'), #> ('bar', 'two'), #> ('baz', 'one'), #> ('baz', 'two'), #> ('foo', 'one')], #> names=['first', 'second'])
expected same order as left
intersect = left.intersection(right, sort=False) print(intersect) #> MultiIndex([('bar', 'two'), #> ('baz', 'one'), #> ('bar', 'one')], #> names=['first', 'second'])
Created on 2020-01-25 by the reprexpy package
Problem description
The intersection of 2 MultiIndex
with sort=False
does not preserve the order, whereas other logical operators on Index and MultiIndex preserve order when sort=False
.
Expected Output
The intersection of 2 MultiIndex
with sort=False
should return the common elements in the order of the left side.
Output of pd.show_versions()
INSTALLED VERSIONS
commit : ca3bfcc
python : 3.7.6.final.0
python-bits : 64
OS : Darwin
OS-release : 19.2.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.0.0rc0+212.gca3bfcc54
numpy : 1.17.5
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.1
setuptools : 45.1.0.post20200119
Cython : 0.29.14
pytest : 5.3.4
hypothesis : 5.3.0
sphinx : 2.3.1
blosc : None
feather : None
xlsxwriter : 1.2.7
lxml.etree : 4.4.2
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.11.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : 1.3.1
fastparquet : 0.3.2
gcsfs : None
lxml.etree : 4.4.2
matplotlib : 3.1.2
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.1
pandas_gbq : None
pyarrow : 0.15.1
pytables : None
pytest : 5.3.4
pyxlsb : None
s3fs : 0.4.0
scipy : 1.4.1
sqlalchemy : 1.3.13
tables : 3.6.1
tabulate : 0.8.6
xarray : 0.14.1
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.7
numba : 0.47.0