Skip to content

PERF: slicing #52183

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 5 commits into from
Mar 29, 2023
Merged

PERF: slicing #52183

merged 5 commits into from
Mar 29, 2023

Conversation

jbrockmendel
Copy link
Member

import pandas as pd
ser = pd.Series(range(300_000))
df = ser.to_frame()

%timeit ser[:30]
14.6 µs ± 474 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)  # <- main
12.6 µs ± 421 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)  # <- PR

%timeit df[:30]
16.2 µs ± 1.23 µs per loop (mean ± std. dev. of 7 runs, 100,000 loops each)  # <- main
11.9 µs ± 184 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)   # <- PR

@@ -6009,10 +6009,10 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
stable across pandas releases.
"""
if isinstance(other, NDFrame):
for name in other.attrs:
Copy link
Member

Choose a reason for hiding this comment

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

Are these changes supposed to be in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. __finalize__ adds up.

Copy link
Member

Choose a reason for hiding this comment

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

It looks like .attrs and .flags just call ._attrs and ._flags respectively? Did the attribute call significantly impact the slicing benchmark?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not seeing much difference, will revert.

Copy link
Member Author

Choose a reason for hiding this comment

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

reverted+greenish

@mroeschke mroeschke added Indexing Related to indexing on series/frames, not to indexes themselves Performance Memory or execution speed performance labels Mar 27, 2023
@mroeschke mroeschke added this to the 2.1 milestone Mar 28, 2023
@mroeschke mroeschke merged commit 1e03419 into pandas-dev:main Mar 29, 2023
@mroeschke
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the perf-slice-2 branch March 29, 2023 19:57
@alippai
Copy link
Contributor

alippai commented Mar 31, 2023

Interesting, why is the df faster than the series?

@jbrockmendel
Copy link
Member Author

Interesting, why is the df faster than the series?

In [4]: %prun -s cumtime for n in range(10**4): ser[:30]
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.038    0.000    0.436    0.000 series.py:971(__getitem__)
    10000    0.019    0.000    0.336    0.000 generic.py:4153(_getitem_slice)
    10000    0.029    0.000    0.269    0.000 series.py:964(_slice)
    10000    0.053    0.000    0.111    0.000 managers.py:1990(get_slice)
    10000    0.049    0.000    0.069    0.000 generic.py:6020(__finalize__)
    10000    0.020    0.000    0.058    0.000 series.py:371(__init__)
    [ommitted stuff that is roughly the same in both]

In [5]: %prun -s cumtime for n in range(10**4): df[:30]
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.028    0.000    0.386    0.000 frame.py:3736(__getitem__)
    10000    0.018    0.000    0.314    0.000 generic.py:4153(_getitem_slice)
    10000    0.039    0.000    0.252    0.000 generic.py:4172(_slice)
    10000    0.039    0.000    0.078    0.000 {method 'get_slice' of 'pandas._libs.internals.BlockManager' objects}
    10000    0.018    0.000    0.056    0.000 frame.py:651(__init__)
    10000    0.034    0.000    0.050    0.000 generic.py:6020(__finalize__)
    [ommitted stuff that is roughly the same in both]

Further investigation welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants