Skip to content

Is there a way to cumsum over a order by column #9198

Answered by cpcloud
carlsunderman asked this question in Q&A
Discussion options

You must be logged in to vote

You can use ibis.cumulative_window or specify the ordering key and bounds by hand:

In [22]: from ibis.interactive import *

In [23]: data = {
    ...:     'fruit' :['apple','apple','banana', 'orange','orange'],
    ...:     '_order': [1, 2, 3, 4, 5],
    ...:     'price': [10, 20, 30, 15, 25]
    ...: }

In [24]: t = ibis.memtable(data)

In [25]: expr = t.mutate(
    ...:     over_time_manual=_.price.sum().over(order_by=_._order, rows=(None, 0)),
    ...:     over_time_cumulative=_.price.sum().over(ibis.cumulative_window(order_by=_._order))
    ...: )

In [26]: expr
Out[26]:
┏━━━━━━━━┳━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┓
┃ fruit  ┃ _order ┃ price ┃ over_time_manual …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by cpcloud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants