Description
Code Sample, a copy-pastable example if possible
df = pd.DataFrame(np.arange(12).reshape(3,4), columns=['A', 'B', 'C', 'D'])
df.drop(columns=['A','not_occurring'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py", line 3697, in drop
errors=errors)
File "/opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py", line 3111, in drop
obj = obj._drop_axis(labels, axis, level=level, errors=errors)
File "/opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py", line 3143, in _drop_axis
new_axis = axis.drop(labels, errors=errors)
File "/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 4404, in drop
'{} not found in axis'.format(labels[mask]))
KeyError: "['not_occurring'] not found in axis"
Problem description
In the pandas documentation for DataFrame drop (https://pandas-docs.github.io/pandas-docs-travis/reference/api/pandas.DataFrame.drop.html#pandas.DataFrame.drop), the following is mentioned:
KeyError: If none of the labels are found in the selected axis
However, when looking at the provided code snippet, we see that even though there is a label which is found in the selected axis ('A'
), the KeyError is thrown.
Expected Output
df = pd.DataFrame(np.arange(12).reshape(3,4), columns=['A', 'B', 'C', 'D'])
df.drop(columns=['A','not_occurring'])
B C D
0 1 2 3
1 5 6 7
2 9 10 11
Suggested Fix
Although from this issue, it seems like the code is at fault, I would suggest to change the documentation to KeyError: If one of the labels are not found in the selected axis
. If the core team agrees with this fix, then I would be happy to provide a pull request that does this.
Output of pd.show_versions()
pandas: 0.25.0.dev0+162.gc9863865c.dirty
pytest: 4.2.1
pip: 19.0.1
setuptools: 40.8.0
Cython: 0.29.5
numpy: 1.15.4
scipy: 1.2.0
pyarrow: 0.11.1
xarray: 0.11.3
IPython: 7.2.0
sphinx: 1.8.4
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.9
feather: None
matplotlib: 3.0.2
openpyxl: 2.6.0
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml.etree: 4.3.1
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.2.18
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: 0.2.0
fastparquet: 0.2.1
pandas_gbq: None
pandas_datareader: None
gcsfs: None