Skip to content

BUG: TypeError when trying to find intersection between MultiIndex and array since 1.2.0 #39699

Open
@dchigarev

Description

@dchigarev
  • 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.


Code Sample, a copy-pastable example

import pandas

multi_index = pandas.MultiIndex.from_arrays([["a", "b"], ["c", "d"]])
try:
    result = multi_index.intersection(["some_col"])
except Exception as e:
    result = e
print(f"Intersection with a list: {repr(result)}")

result = multi_index.intersection(pandas.Index(["some_col"]))
print(f"Intersection with an Index: {result}")
Output for pandas 1.1.5
Intersection with a list: MultiIndex([], )
Intersection with an Index: MultiIndex([], )
Output for pandas 1.2.0+
Intersection with a list: TypeError('other must be a MultiIndex or a list of tuples')
Intersection with an Index: MultiIndex([], )

Problem description

It's expected to get an empty index as the result of the intersection, but getting a TypeError.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 3e89b4c4b1580aa890023fc550774e63d499da25
python           : 3.7.7.final.0
python-bits      : 64
OS               : Linux
OS-release       : 4.15.0-50-generic
Version          : #54-Ubuntu SMP Mon May 6 18:46:08 UTC 2019
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.2.0
numpy            : 1.19.0
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 20.1.1
setuptools       : 47.3.1.post20200622
Cython           : None
pytest           : 6.0.2
hypothesis       : None
sphinx           : None
blosc            : None
feather          : 0.4.1
xlsxwriter       : None
lxml.etree       : 4.5.1
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.11.2
IPython          : None
pandas_datareader: None
bs4              : 4.9.1
bottleneck       : None
fsspec           : 0.7.4
fastparquet      : None
gcsfs            : None
matplotlib       : 3.2.2
numexpr          : 2.7.1
odfpy            : None
openpyxl         : 3.0.4
pandas_gbq       : 0.13.2
pyarrow          : 1.0.1
pyxlsb           : None
s3fs             : 0.4.2
scipy            : 1.5.1
sqlalchemy       : 1.3.18
tables           : 3.6.1
tabulate         : None
xarray           : 0.15.1
xlrd             : 1.2.0
xlwt             : None
numba            : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMultiIndexsetopsunion, intersection, difference, symmetric_difference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions