Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in documentation of DataFrame.drop #25473

Closed
MaxVanDeursen opened this issue Feb 28, 2019 · 0 comments · Fixed by #25474
Closed

Error in documentation of DataFrame.drop #25473

MaxVanDeursen opened this issue Feb 28, 2019 · 0 comments · Fixed by #25474
Labels
Milestone

Comments

@MaxVanDeursen
Copy link
Contributor

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()

commit: None python: 3.7.2.final.0 python-bits: 64 OS: Linux OS-release: 4.15.0-45-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants