lt_toolbox.TrajFrame.add_variable#

TrajFrame.add_variable(name: str, values: list | None = None, expr: Expr | None = None, list_expr: bool = False) Self[source]#

Add a new column variable to the TrajFrame.

The variable can either be defined using a polars expression or a list of values.

Parameters:
  • name (str) – New column variable name to be added to TrajFrame.

  • values (list, default: None) – values of new variable to be added to the TrajFrame.

  • expr (Expression, default: None) – Expression used to determine values of new variable. The expression must use only columns contained in the TrajFrame.

  • list_expr (bool, default: False) – Use list expression to determine values of new variable. The default value is False.

Returns:

TrajFrame is returned with the new column variable appended to the TrajFrame.

Return type:

TrajFrame

Examples

Add a new column variable {sigma0} to the TrajFrame containing the potential density values recorded along particle trajectories.

Method 1: Using a list of values with an eager TrajFrame, trajectories. Here, we first import the Gibbs Seawater Toolbox package, gsw.

>>> import gsw
>>> sigma0 = gsw.density.sigma0(SA=trajectories.data['sal'], CT=trajectories.data['temp']).tolist()
>>> trajectories.add_variable(name='sigma0', values=sigma0).

Method 2: Using a polars expression with a lazy TrajFrame, trajectories. Importantly, this method is much more performant approach to create a new variable than using a list of values since the expression is only evaluated when the TrajFrame is collected by leveraging the eager/lazy_list_ops extensions of the polars API.

>>> trajectories.add_variable(name='sigma0', expr=gsw.density.sigma0(SA=pl.col('sal'), CT=pl.col('temp')), list_expr=True)