Skip to content

PERF: is_bool_indexer #41861

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

Merged
merged 4 commits into from
Jun 8, 2021
Merged

PERF: is_bool_indexer #41861

merged 4 commits into from
Jun 8, 2021

Conversation

jbrockmendel
Copy link
Member

Besides avoiding an array allocation, thus avoids the np.asarray call that was one of the main offenders in #41632


for item in obj:
if not util.is_bool_object(item):
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any concern with the benchmarks being somewhat misleading due to short circuiting? At least in the Python space similar functions don't seem to benefit much from this

In [6]: %timeit all(isinstance(x, bool) for x in obj)
6.7 µs ± 136 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [7]: %timeit np.array(obj).dtype.kind == "b"
5.98 µs ± 121 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any concern with the benchmarks being somewhat misleading due to short circuiting?

Only one of the 4 cases posted short-circuits. Am I misunderstanding the question?

At least in the Python space similar functions don't seem to benefit much from this

I was surprised by this too, found that doing it in cython was much more performant after talking with @seberg yesterday about how numpy does this inference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, the difference is big...

NumPy should maybe try to be a bit faster, but that will just make the gap a bit smaller (e.g. by assuming that usually all elements are the same type, I guess a true casting support np.asarray(..., dtype=bool, casting="equiv") would fill the gap, but no idea how likely that would be). Unlike NumPy, you already know what you want here (bools) and what you got (a 1-d list).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah my bad on short circuiting - misread it. Thanks for comments

@WillAyd WillAyd added the Performance Memory or execution speed performance label Jun 8, 2021
@jreback jreback added this to the 1.3 milestone Jun 8, 2021
@jreback jreback merged commit 017ff7c into pandas-dev:master Jun 8, 2021
@jreback
Copy link
Contributor

jreback commented Jun 8, 2021

thanks this looks good

@jbrockmendel jbrockmendel deleted the ref-infer branch June 8, 2021 20:48
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants