-
Notifications
You must be signed in to change notification settings - Fork 603
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
feat: Way to calculate cumulative product over window #10542
Comments
Thanks for raising this @PtrBld ! So it's a bit odd, but most SQL engines don't implement a Anyway, for a temporary workaround (only for DuckDB) that is a bit awkward, but does work, you can do something like this: [nav] In [1]: import ibis
...: ibis.options.interactive = True
...: t = ibis.memtable(
...: {
...: "id": [1, 2, 3, 4, 5, 6],
...: "grouper": ["a", "a", "a", "b", "b", "c"],
...: "values": [3, 2, 1, 2, 3, 2],
...: }
...: )
[ins] In [2]: t
Out[2]:
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┓
┃ id ┃ grouper ┃ values ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━┩
│ int64 │ string │ int64 │
├───────┼─────────┼────────┤
│ 1 │ a │ 3 │
│ 2 │ a │ 2 │
│ 3 │ a │ 1 │
│ 4 │ b │ 2 │
│ 5 │ b │ 3 │
│ 6 │ c │ 2 │
└───────┴─────────┴────────┘
[ins] In [3]: import ibis.expr.datatypes as dt
[ins] In [4]: @ibis.udf.agg.builtin
...: def product(x) -> dt.float64:
...: ...
...:
[ins] In [5]: t.mutate(cumprod=product(t.values).over(ibis.cumulative_window(order_by
...: ="id", group_by="grouper"))).order_by("id")
Out[5]:
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━┓
┃ id ┃ grouper ┃ values ┃ cumprod ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━┩
│ int64 │ string │ int64 │ float64 │
├───────┼─────────┼────────┼─────────┤
│ 1 │ a │ 3 │ 3.0 │
│ 2 │ a │ 2 │ 6.0 │
│ 3 │ a │ 1 │ 6.0 │
│ 4 │ b │ 2 │ 2.0 │
│ 5 │ b │ 3 │ 6.0 │
│ 6 │ c │ 2 │ 2.0 │
└───────┴─────────┴────────┴─────────┘ |
Thanks @gforsyth for your quick response! I already knew it was kind of a odd request, but I think there is value in maintaining similar API capabilities to pandas. But I probably agree that it is not really maintainable especially with the range of available backends. For the moment I have solved the issue in a similar fashion to your suggestion. Works for now... |
Is your feature request related to a problem?
I am trying to calculate the cumulative product over a window and can not figure out how I would do this in ibis.
In pandas you would have this: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.cumprod.html
What is the motivation behind your request?
No response
Describe the solution you'd like
Similar to the cumsum functionality:
cumulative_col=.col.cumsum().over(ibis.window(order_by="date", group_by=["grouper"]))
I would expect something similar for products:
cumulative_col=.col.cumprod().over(ibis.window(order_by="date", group_by=["grouper"]))
What version of ibis are you running?
9.5.0
What backend(s) are you using, if any?
DuckDB
Code of Conduct
The text was updated successfully, but these errors were encountered: