Skip to content

BUG: DataFrame.drop() fails when columns= is given as tuple #43978

Open
@JBGreisman

Description

@JBGreisman

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(12).reshape(3, 4),
                  columns=['A', 'B', 'C', 'D'])

print(df.drop(columns=list(["A", "B"])))     # <-- Works
print(df.drop(columns=np.array(["A", "B"]))) # <-- Works
print(df.drop(columns=tuple(["A", "B"])))    # <-- Fails with KeyError

Issue Description

The documentation says that the columns= argument of DataFrame.drop() can take a single label or list-like, but it fails when given a tuple with more than column name. This method seems to work when the exact same column labels are provided as a list or a np.ndarray.

Just an observation -- a tuple with len()==1 does seem to work successfully here.

Expected Behavior

For the above example, df.drop(columns=("A", "B")) should produce the same output as columns=["A", "B"] or columns=np.array(["A", "B"]), resulting in the following DataFrame:

    C   D
0   2   3
1   6   7
2  10  11

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.8.11.final.0
python-bits : 64
OS : Darwin
OS-release : 17.7.0
Version : Darwin Kernel Version 17.7.0: Fri Oct 30 13:34:27 PDT 2020; root:xnu-4570.71.82.8~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.0.1
setuptools : 58.0.4
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.28.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

Labels

BugNeeds DiscussionRequires discussion from core team before further actionNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions