cum_sum
cum_sum
calculates a cumulative sum in an update_by
table operation.
Syntax
cum_sum(cols: list[str]) -> UpdateByOperation
Parameters
Parameter | Type | Description |
---|---|---|
cols | list[str] | The column(s) to be operated on. These can include expressions to rename output columns (e.g., |
Returns
An UpdateByOperation
to be used in an update_by
table operation.
Examples
The following example performs an update_by
on the source
table using the cum_sum
operation. No grouping columns are given, so the cumulative sum is calculated for all rows in the table.
from deephaven.updateby import cum_sum
from deephaven import empty_table
source = empty_table(10).update(["Letter = (i % 2 == 0) ? `A` : `B`", "X = i"])
result = source.update_by(ops=cum_sum(cols=["SumX = X"]), by=[])
- result
- source
The following example builds off the previous by specifying Letter
as the grouping column. Thus, the cumulative sum of X
is calculated for each unique letter.
from deephaven.updateby import cum_sum
from deephaven import empty_table
source = empty_table(10).update(["Letter = (i % 2 == 0) ? `A` : `B`", "X = i"])
result = source.update_by(ops=cum_sum(cols=["SumX = X"]), by=["Letter"])
- result
- source
The following example builds off the previous by calculating the cumulative sum of two columns using the same UpdateByOperation.
from deephaven.updateby import cum_sum
from deephaven import empty_table
source = empty_table(10).update(
["Letter = (i % 2 == 0) ? `A` : `B`", "X = i", "Y = randomInt(1, 11)"]
)
result = source.update_by(ops=cum_sum(cols=["SumX = X", "SumY = Y"]), by=["Letter"])
- result
- source
The following example builds off the previous by specifying two grouping columns: Letter
and Truth
. Thus, each group is a unique combination of letter and boolean value in those two columns, respectively.
from deephaven.updateby import cum_sum
from deephaven import empty_table
source = empty_table(10).update(
[
"Letter = (i % 2 == 0) ? `A` : `B`",
"Truth = randomBool()",
"X = i",
"Y = randomInt(1, 11)",
]
)
result = source.update_by(
ops=cum_sum(cols=["SumX = X", "SumY = Y"]), by=["Letter", "Truth"]
)
- result
- source