Skip to content

ENH: Support groupby.ewm operations #37878

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 43 commits into from
Nov 18, 2020

Conversation

mroeschke
Copy link
Member

@mroeschke mroeschke commented Nov 15, 2020

Additionally enables a Numba engine for groupby(...).ewm(...).mean(...)

In [1]: df = pd.DataFrame({"A": range(10_000), "B": range(10_000)})

In [2]: gb_ewm = df.groupby("A").ewm(com=1.0)

--cache time first
In [3]: %timeit -r 1 -n 1 gb_ewm.mean(engine='numba')
1.02 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

In [4]: %timeit gb_ewm.mean(engine='numba')
578 ms ± 28.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [5]: %timeit df.groupby('A').apply(lambda x: x.ewm(com=1.0).mean())
3.43 s ± 208 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [6]: %timeit gb_ewm.mean(engine='cython')
4.19 s ± 204 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

@pep8speaks
Copy link

pep8speaks commented Nov 15, 2020

Hello @mroeschke! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-11-17 21:58:52 UTC

@jreback jreback added Enhancement Window rolling, ewma, expanding labels Nov 15, 2020
@mroeschke mroeschke added this to the 1.2 milestone Nov 15, 2020
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

initial scan looks good, can you add some asv's for this

@jreback
Copy link
Contributor

jreback commented Nov 15, 2020

can you show a timeit result of the new groupby.ewm vs groupby.apply(.....ewm) for posterity in the top of this PR

@mroeschke
Copy link
Member Author

Posted the timings. I think there is a slight slowdown using the cython engine with the convenience wrapper since we have to reconstruct the EWM object in the apply loop.

@jreback
Copy link
Contributor

jreback commented Nov 15, 2020

what is the timing for master (before this PR for the groupby apply)

i would have expected the numba one to be way after here

@mroeschke
Copy link
Member Author

Updated the snippet above; the numba timing including a caching run which skewed the benchmark.

The groupby apply timing is similar on master.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

question and request for tests

],
names=["A", None],
),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you also compare to groupby('A').apply(lambda x: getattr(x.ewm(com=1.0), method)) ?

tm.assert_frame_equal(result, expected)

expected = df.groupby("A").apply(lambda x: getattr(x.ewm(com=1.0), method)())
# There may be a bug in the above statement; not returning the correct index
Copy link
Contributor

Choose a reason for hiding this comment

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

hah i think its expected actually :->

Copy link
Contributor

Choose a reason for hiding this comment

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

or if you think its a bug, then open an issue :->

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah right ewm acts as a transform so the index should be the same.

@jreback jreback merged commit e0547d1 into pandas-dev:master Nov 18, 2020
@jreback
Copy link
Contributor

jreback commented Nov 18, 2020

thanks @mroeschke really nice

@mroeschke mroeschke deleted the feature/groupby_ewma branch November 18, 2020 01:27
@mroeschke mroeschke mentioned this pull request Nov 18, 2020
32 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: support .groupby().ewm() directly
3 participants