Skip to main content
Version: Python

rolling_std_time

rolling_std_time creates a time-based windowed sample standard deviation operator to be used in an update_by table operation. Data is windowed by reverse and forward time intervals relative to the current row, and the sample standard deviation of values within the window is calculated.

Sample standard deviation is calculated as the square root of the Bessel-corrected sample variance, which can be shown to be an unbiased estimator of population variance under some conditions. However, sample standard deviation is a biased estimator of population standard deviation.

Syntax

rolling_std_time(
ts_col: str,
cols: list[str],
rev_time: Union[int, str],
fwd_time: Union[int, str],
) -> UpdateByOperation

Parameters

ParameterTypeDescription
ts_colstr

The name of the column containing timestamps.

colslist[str]

The column(s) to be operated on. These can include expressions to rename the output (e.g., NewCol = Col). If None the rolling sample standard deviation is calculated for all applicable columns.

rev_timeUnion[int,str]

The look-behind window size. This can be expressed as an integer in nanoseconds or a string duration, e.g., "PT00:00:00.001" or "PTnHnMnS", where H is hour, M is minute, and S is second.

fwd_timeUnion[int,str]

The look-forward window size. This can be expressed as an integer in nanoseconds or a string duration, e.g., "PT00:00:00.001" or "PTnHnMnS", where H is hour, M is minute, and S is second.

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 three rolling_std_time operations. Each operation gives varying rev_time and fwd_time values to show how they affect the output. The windows for each operation are as follows:

  • op_before: The window starts five seconds before the current row, and ends one second before the current row.
  • op_after: The window starts one second after the current row, and ends five seconds after of the current row.
  • op_middle: The window starts three seconds before the current row, and ends three seconds after the current row.
from deephaven.updateby import rolling_std_time
from deephaven.time import to_j_instant
from deephaven import empty_table

base_time = to_j_instant("2023-01-01T00:00:00 ET")

source = empty_table(10).update(
["Timestamp = base_time + i * SECOND", "Letter = (i % 2 == 0) ? `A` : `B`", "X = i"]
)

op_before = rolling_std_time(
ts_col="Timestamp",
cols=["WindowBeforeX = X"],
rev_time=int(5e9),
fwd_time=int(-1e9),
)
op_after = rolling_std_time(
ts_col="Timestamp", cols=["WindowAfterX = X"], rev_time="PT-1S", fwd_time="PT5S"
)
op_middle = rolling_std_time(
ts_col="Timestamp", cols=["WindowMiddleX = X"], rev_time="PT3S", fwd_time="PT3S"
)

result = source.update_by(ops=[op_before, op_after, op_middle], by=["Letter"])