Skip to content

BUG: Nonintuitive behaviour when indexing with a constant boolean function #35479

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

Closed
2 of 3 tasks
onnoeberhard opened this issue Jul 30, 2020 · 2 comments
Closed
2 of 3 tasks

Comments

@onnoeberhard
Copy link
Contributor

  • 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

import pandas as pd
series = pd.Series([1, 2, 3])
x = series[lambda s: s > 2]    # Indexing with a boolean function, works like a charm
y = series[lambda s: True]     # Indexing with a constant boolean function --> results in KeyError: True

Problem description

I would expect y to be equal to series in this example, because the callable function passed to __getitem__ returns True for all items in the Series. The same error arises when using a DataFrame. I know in this example, it seems like the function would be useless, because it would just return series, but in my use case the function that is passed to __getitem__ is actually a 'filter' function argument, and it would be very convenient to just turn off the filter by passing lambda df: True as the filter function.

A quick workaround I found useful is:

import numpy as np
y = series[lambda s: np.full(len(s), True)]

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.0-1030-aws
Version : #32~18.04.1-Ubuntu SMP Tue Jun 30 23:04:16 UTC 2020
machine : x86_64
processor :
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.0
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.1.1
Cython : 0.29.21
pytest : 5.4.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.2.9
lxml.etree : None
html5lib : None
pymysql : 0.9.3
psycopg2 : 2.8.5 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : 4.9.1
bottleneck : None
fsspec : 0.7.4
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.4
pandas_gbq : None
pyarrow : 0.17.1
pytables : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.4.1
sqlalchemy : 1.3.13
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.2.0
xlwt : None
numba : 0.50.1

@onnoeberhard onnoeberhard added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 30, 2020
@dsaxton
Copy link
Member

dsaxton commented Jul 30, 2020

The lambda function takes the whole Series as input (it's not a "mapping" operation that returns a Series of True values) and needs to return a valid indexer. In this case the scalar True is not a valid index so an error is raised. It seems you're expecting it to behave like

series[lambda s: s.map(lambda x: True)]

but this isn't the same thing.

@dsaxton dsaxton added Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 30, 2020
@simonjayhawkins
Copy link
Member

closing as answered. Thanks @onnoeberhard for the report. Thanks @dsaxton for the reply.

@simonjayhawkins simonjayhawkins added this to the No action milestone Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants