Skip to content

Conversation

@lukemanley
Copy link
Member

@lukemanley lukemanley commented Jan 30, 2022

  • tests added / passed
  • Ensure all linting tests pass, see here for how to run them
  • whatsnew entry

groupby.transform broadcasts user-defined functions by passing a repeated array to np.concatenate. Passing the array once to np.tile and letting numpy repeat the array is quite a bit faster.

import numpy as np
import pandas as pd

N = 1_000_000

df = pd.DataFrame(
    data=np.random.rand(N, 3),
    index=np.random.randint(0, 5, N)
)

%timeit df.groupby(level=0).transform(lambda x: np.max(x, axis=0))

302 ms ± 8.18 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)     <- main
118 ms ± 2.02 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)   <- PR

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

Nice. Could you check if we have an asv for that case?

@lukemanley
Copy link
Member Author

@phofl - there are not many groupby transform asvs that hit this code path. I just added one for this case.

@mroeschke mroeschke added this to the 1.5 milestone Jan 30, 2022
@mroeschke mroeschke added Groupby Performance Memory or execution speed performance labels Jan 30, 2022
Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

thx

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

We like to target ASVs to be around 80ms, is that right? Just want to make sure, looks appropriate as-is.

@jreback
Copy link
Contributor

jreback commented Jan 30, 2022

We like to target ASVs to be around 80ms, is that right? Just want to make sure, looks appropriate as-is.

yeah generally < 100ms is good, but we have a lot that are not so can cleanup separately.

@jreback jreback merged commit f4c5797 into pandas-dev:main Jan 30, 2022
@jreback
Copy link
Contributor

jreback commented Jan 30, 2022

thanks @lukemanley

phofl pushed a commit to phofl/pandas that referenced this pull request Feb 14, 2022
@lukemanley lukemanley deleted the groupby-transform-broadcast-perf branch March 2, 2022 01:13
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Groupby Performance Memory or execution speed performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants