Last active
October 18, 2021 21:43
-
-
Save dutc/ceb690bd62f4fd0747bdc6dd1f6ba20f to your computer and use it in GitHub Desktop.
sample `pandas` operations affected by function-call penalty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pandas import Series | |
from numpy.random import default_rng | |
from string import ascii_lowercase | |
rng = default_rng(0) | |
s = Series( | |
rng.integers(-10, 10, size=(size := 10_000)), | |
index=rng.choice([*ascii_lowercase], size=(size, length := 10)).view(f'<U{length}').ravel(), | |
) | |
def udf(col): | |
''' sample user-defined function ''' | |
return 0 | |
# will call `udf` once per unique group | |
s.groupby(s.index).aggregate(udf) | |
# will call `udf` once per three rows | |
s.rolling(3).aggregate(udf) | |
# will call `udf` once per row | |
s.expanding().aggregate(udf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment