Skip to content

daft.functions.over#

over #

over(expr: Expression, window: Window) -> Expression

Apply the expression as a window function.

Parameters:

Name Type Description Default
expr Expression

The expression to apply as a window function.

required
window Window

The window specification (created using daft.Window) defining partitioning, ordering, and framing.

required

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> import daft
>>> from daft.functions import sum
>>>
>>> df = daft.from_pydict(
...     {
...         "group": ["A", "A", "A", "B", "B", "B"],
...         "date": ["2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04", "2020-01-05", "2020-01-06"],
...         "value": [1, 2, 3, 4, 5, 6],
...     }
... )
>>> window_spec = daft.Window().partition_by("group").order_by("date")
>>> df = df.with_column("grouped_sum", sum(df["value"]).over(window_spec))
>>> df.sort(["group", "date"]).show()
╭────────┬────────────┬───────┬─────────────╮
│ group  ┆ date       ┆ value ┆ grouped_sum │
│ ---    ┆ ---        ┆ ---   ┆ ---         │
│ String ┆ String     ┆ Int64 ┆ Int64       │
╞════════╪════════════╪═══════╪═════════════╡
│ A      ┆ 2020-01-01 ┆ 1     ┆ 6           │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ A      ┆ 2020-01-02 ┆ 2     ┆ 6           │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ A      ┆ 2020-01-03 ┆ 3     ┆ 6           │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ B      ┆ 2020-01-04 ┆ 4     ┆ 15          │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ B      ┆ 2020-01-05 ┆ 5     ┆ 15          │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ B      ┆ 2020-01-06 ┆ 6     ┆ 15          │
╰────────┴────────────┴───────┴─────────────╯
(Showing first 6 of 6 rows)

Returns:

Name Type Description
Expression Expression

The result of applying this expression as a window function.

Source code in daft/functions/window.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def over(expr: Expression, window: Window) -> Expression:
    """Apply the expression as a window function.

    Args:
        expr: The expression to apply as a window function.
        window: The window specification (created using ``daft.Window``)
            defining partitioning, ordering, and framing.

    Examples:
        >>> import daft
        >>> from daft.functions import sum
        >>>
        >>> df = daft.from_pydict(
        ...     {
        ...         "group": ["A", "A", "A", "B", "B", "B"],
        ...         "date": ["2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04", "2020-01-05", "2020-01-06"],
        ...         "value": [1, 2, 3, 4, 5, 6],
        ...     }
        ... )
        >>> window_spec = daft.Window().partition_by("group").order_by("date")
        >>> df = df.with_column("grouped_sum", sum(df["value"]).over(window_spec))
        >>> df.sort(["group", "date"]).show()
        ╭────────┬────────────┬───────┬─────────────╮
        │ group  ┆ date       ┆ value ┆ grouped_sum │
        │ ---    ┆ ---        ┆ ---   ┆ ---         │
        │ String ┆ String     ┆ Int64 ┆ Int64       │
        ╞════════╪════════════╪═══════╪═════════════╡
        │ A      ┆ 2020-01-01 ┆ 1     ┆ 6           │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ A      ┆ 2020-01-02 ┆ 2     ┆ 6           │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ A      ┆ 2020-01-03 ┆ 3     ┆ 6           │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ B      ┆ 2020-01-04 ┆ 4     ┆ 15          │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ B      ┆ 2020-01-05 ┆ 5     ┆ 15          │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ B      ┆ 2020-01-06 ┆ 6     ┆ 15          │
        ╰────────┴────────────┴───────┴─────────────╯
        <BLANKLINE>
        (Showing first 6 of 6 rows)

    Returns:
        Expression: The result of applying this expression as a window function.
    """
    return Expression._from_pyexpr(expr._expr.over(window._spec))