Skip to content

how do I compute the cumulative sum over a timeseries with Ibis? #6394

Answered by jcrist
lostmygithubaccount asked this question in Q&A
Discussion options

You must be logged in to vote

This can be done using a windowed aggregation using an UNBOUNDED PRECEDING window:

In [1]: import ibis, pandas as pd

In [2]: df = pd.DataFrame({"x": [1, 2, 3, 4, 5, 6, 7, 11]})

In [3]: t = ibis.memtable(df)

In [4]: ibis.options.interactive = True

In [5]: t.mutate(y=t.x.sum().over(rows=(None, 0)))  # y is the sum of x over an unbounded preceding window
Out[5]: 
┏━━━━━━━┳━━━━━━━┓
┃ xy     ┃
┡━━━━━━━╇━━━━━━━┩
│ int64int64 │
├───────┼───────┤
│     11 │
│     23 │
│     36 │
│     410 │
│     515 │
│     621 │
│     728 │
│    1139 │
└───────┴───────┘

In [6]: ibis.show_sql(t.mutate(y=t.x.sum().over(rows=(None, 0))))  # view the…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jcrist
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