snowflake.snowpark.DataFrameAnalyticsFunctions.compute_lag¶
- DataFrameAnalyticsFunctions.compute_lag(cols: ~typing.List[~typing.Union[str, ~snowflake.snowpark.column.Column]], lags: ~typing.List[int], order_by: ~typing.List[str], group_by: ~typing.List[str], col_formatter: ~typing.Callable[[str, str, int], str] = <function DataFrameAnalyticsFunctions._default_col_formatter>) DataFrame [source]¶
Creates lag columns to the specified columns of the DataFrame by grouping and ordering criteria.
- Parameters:
cols – List of column names or Column objects to calculate lag features.
lags – List of positive integers specifying periods to lag by.
order_by – A list of column names that specify the order in which rows are processed.
group_by – A list of column names on which the DataFrame is partitioned for separate window calculations.
col_formatter – An optional function for formatting output column names, defaulting to the format ‘<input_col>LAG<lag>’. This function takes three arguments: ‘input_col’ (str) for the column name, ‘operation’ (str) for the applied operation, and ‘value’ (int) for lag value, and returns a formatted string for the column name.
- Returns:
A Snowflake DataFrame with additional columns corresponding to each specified lag period.
Example
>>> sample_data = [ ... ["2023-01-01", 101, 200], ... ["2023-01-02", 101, 100], ... ["2023-01-03", 101, 300], ... ["2023-01-04", 102, 250], ... ] >>> df = session.create_dataframe(sample_data).to_df( ... "ORDERDATE", "PRODUCTKEY", "SALESAMOUNT" ... ) >>> res = df.analytics.compute_lag( ... cols=["SALESAMOUNT"], ... lags=[1, 2], ... order_by=["ORDERDATE"], ... group_by=["PRODUCTKEY"], ... ) >>> res.show() ------------------------------------------------------------------------------------------ |"ORDERDATE" |"PRODUCTKEY" |"SALESAMOUNT" |"SALESAMOUNT_LAG_1" |"SALESAMOUNT_LAG_2" | ------------------------------------------------------------------------------------------ |2023-01-04 |102 |250 |NULL |NULL | |2023-01-01 |101 |200 |NULL |NULL | |2023-01-02 |101 |100 |200 |NULL | |2023-01-03 |101 |300 |100 |200 | ------------------------------------------------------------------------------------------