Skip to content

Instantly share code, notes, and snippets.

@dutc
Last active October 18, 2021 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dutc/ceb690bd62f4fd0747bdc6dd1f6ba20f to your computer and use it in GitHub Desktop.
Save dutc/ceb690bd62f4fd0747bdc6dd1f6ba20f to your computer and use it in GitHub Desktop.
sample `pandas` operations affected by function-call penalty
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