You are viewing documentation about an older version (1.18.0). View latest version

snowflake.snowpark.DataFrameAnalyticsFunctions.compute_lead¶

DataFrameAnalyticsFunctions.compute_lead(cols: ~typing.List[~typing.Union[str, ~snowflake.snowpark.column.Column]], leads: ~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 lead columns to the specified columns of the DataFrame by grouping and ordering criteria.

Parameters:
  • cols – List of column names or Column objects to calculate lead features.

  • leads – List of positive integers specifying periods to lead 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>LEAD<lead>’. This function takes three arguments: ‘input_col’ (str) for the column name, ‘operation’ (str) for the applied operation, and ‘value’ (int) for the lead value, and returns a formatted string for the column name.

Returns:

A Snowflake DataFrame with additional columns corresponding to each specified lead 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_lead(
...     cols=["SALESAMOUNT"],
...     leads=[1, 2],
...     order_by=["ORDERDATE"],
...     group_by=["PRODUCTKEY"]
... )
>>> res.show()
--------------------------------------------------------------------------------------------
|"ORDERDATE"  |"PRODUCTKEY"  |"SALESAMOUNT"  |"SALESAMOUNT_LEAD_1"  |"SALESAMOUNT_LEAD_2"  |
--------------------------------------------------------------------------------------------
|2023-01-04   |102           |250            |NULL                  |NULL                  |
|2023-01-01   |101           |200            |100                   |300                   |
|2023-01-02   |101           |100            |300                   |NULL                  |
|2023-01-03   |101           |300            |NULL                  |NULL                  |
--------------------------------------------------------------------------------------------
Copy