Skip to content

BUG:df.drop(columns=["col1"]) KeyError "['col1'] not found in axis" #40160

@asuleyman

Description

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


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

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

Problem description

When trying to drop a non existing column in pandas dataframe it throw a KeyError stating that "['B1'] not found in axis" It is better if the wording was more specific like : should be "['B1'] not found in columns" because I specified that I want to delete columns. The same thing is applicable for rows drop. Here you can see the result error:

KeyError Traceback (most recent call last)
in
----> 1 df.drop(columns=['B1', 'C'])

D:\Python\My_Anaconda\lib\site-packages\pandas\core\frame.py in drop(self, labels, axis, index, columns, level, inplace, errors)
4303 weight 1.0 0.8
4304 """
-> 4305 return super().drop(
4306 labels=labels,
4307 axis=axis,

D:\Python\My_Anaconda\lib\site-packages\pandas\core\generic.py in drop(self, labels, axis, index, columns, level, inplace, errors)
4150 for axis, labels in axes.items():
4151 if labels is not None:
-> 4152 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
4153
4154 if inplace:

D:\Python\My_Anaconda\lib\site-packages\pandas\core\generic.py in _drop_axis(self, labels, axis, level, errors)
4185 new_axis = axis.drop(labels, level=level, errors=errors)
4186 else:
-> 4187 new_axis = axis.drop(labels, errors=errors)
4188 result = self.reindex(**{axis_name: new_axis})
4189

D:\Python\My_Anaconda\lib\site-packages\pandas\core\indexes\base.py in drop(self, labels, errors)
5589 if mask.any():
5590 if errors != "ignore":
-> 5591 raise KeyError(f"{labels[mask]} not found in axis")
5592 indexer = indexer[~mask]
5593 return self.delete(indexer)

KeyError: "['B1'] not found in axis"

OUTPUT EXPECTED:

"['B1'] not found in columns"

Output of 1.2.2

INSTALLED VERSIONS

commit : 7d32926
python : 3.8.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.2.2
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20210108
Cython : 0.29.22
pytest : 6.2.2
hypothesis : None
sphinx : 3.5.1
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.21.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.7
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.2
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.23
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.52.0
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closing CandidateMay be closeable, needs more eyeballsEnhancementError ReportingIncorrect or improved errors from pandasIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions