datatable.cumprod()

Added in version 1.1.0

For each column from cols calculate cumulative product. The product of the missing values is calculated as one. In the presence of by(), the cumulative product is computed within each group.

Parameters

cols
FExpr

Input data for cumulative product calculation.

reverse
bool

If False, computation is done from top to bottom. If True, it is done from bottom to top.

return
FExpr

f-expression that converts input columns into the columns filled with the respective cumulative products.

except
TypeError

The exception is raised when one of the columns from cols has a non-numeric type.

Examples

Create a sample datatable frame:

from datatable import dt, f, by DT = dt.Frame({"A": [2, None, 5, -1, 0], "B": [None, None, None, None, None], "C": [5.4, 3, 2.2, 4.323, 3], "D": ['a', 'a', 'b', 'b', 'b']})
ABCD
int32voidfloat64str32
02NA5.4a
1NANA3a
25NA2.2b
3-1NA4.323b
40NA3b

Calculate cumulative product in a single column:

DT[:, dt.cumprod(f.A)]
A
int64
02
12
210
3-10
40

Calculate the cumulative product from bottom to top:

DT[:, dt.cumprod(f.A, reverse=True)]
A
int64
00
10
20
30
40

Calculate cumulative products in multiple columns:

DT[:, dt.cumprod(f[:-1])]
ABC
int64int64float64
0215.4
12116.2
210135.64
3-101154.072
401462.215

For a grouped frame calculate cumulative products within each group:

DT[:, dt.cumprod(f[:]), by('D')]
DABC
str32int64int64float64
0a215.4
1a2116.2
2b512.2
3b-519.5106
4b0128.5318