Skip to content

BUG: DataFrame.drop fails when there is a multiIndex without any level provided #36293

Closed
@galipremsagar

Description

@galipremsagar
  • 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

>>> x = pd.DataFrame({"a": range(10), "b": range(10, 20), "d": ["a", "v"] * 5}, index=pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
...                              ['speed', 'weight', 'length']],
...                      codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2, 1],
...                             [0, 1, 2, 0, 1, 2, 0, 1, 2, 1]]))
>>> x
               a   b  d
lama   speed   0  10  a
       weight  1  11  v
       length  2  12  a
cow    speed   3  13  v
       weight  4  14  a
       length  5  15  v
falcon speed   6  16  a
       weight  7  17  v
       length  8  18  a
cow    weight  9  19  v
>>> x.drop(index='cow')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/frame.py", line 4169, in drop
    errors=errors,
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/generic.py", line 3884, in drop
    obj = obj._drop_axis(labels, axis, level=level, errors=errors)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/generic.py", line 3933, in _drop_axis
    indexer = ~axis.isin(labels)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 3600, in isin
    values = MultiIndex.from_tuples(values, names=self.names)._values
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 501, in from_tuples
    arrays = list(lib.tuples_to_object_array(tuples).T)
  File "pandas/_libs/lib.pyx", line 2471, in pandas._libs.lib.tuples_to_object_array
TypeError: Expected tuple, got str

Problem description

I'd expected drop to drop all the rows with cow as an index.

Expected Output

>>> x.drop(index='cow')
               a   b  d
lama   speed   0  10  a
       weight  1  11  v
       length  2  12  a
falcon speed   6  16  a
       weight  7  17  v
       length  8  18  a

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2a7d332
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.1.2
numpy : 1.17.3
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.1.0
Cython : None
pytest : None
hypothesis : 5.29.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    MultiIndexReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions