Skip to content

daft.functions.last_value#

last_value #

last_value(expr: Expression, ignore_nulls: bool = False) -> Expression

Returns the last value in the window frame.

Must be used with over() to specify the window partition, order, and frame. When ignore_nulls=True, skips null values and returns the last non-null value.

Parameters:

Name Type Description Default
expr Expression

The input expression.

required
ignore_nulls bool

whether to ignore null values. Defaults to False.

False

Returns:

Name Type Description
Expression Expression

The last value in the window frame.

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
>>> import daft
>>> from daft.functions import last_value
>>>
>>> df = daft.from_pydict(
...     {
...         "category": ["A", "A", "A", "A", "B", "B", "B", "B"],
...         "time": [1, 2, 3, 4, 1, 2, 3, 4],
...         "value": [1, None, None, 4, 10, None, 30, None],
...     }
... )
>>>
>>> # Forward fill using last_value: look back for the latest preceding non-null value
>>> window = (
...     daft.Window()
...     .partition_by("category")
...     .order_by("time")
...     .rows_between(daft.Window.unbounded_preceding, daft.Window.current_row)
... )
>>> df = df.with_column("ffill", last_value(df["value"], ignore_nulls=True).over(window))
>>> df.sort(["category", "time"]).show()
╭──────────┬───────┬───────┬───────╮
│ category ┆ time  ┆ value ┆ ffill │
│ ---      ┆ ---   ┆ ---   ┆ ---   │
│ String   ┆ Int64 ┆ Int64 ┆ Int64 │
╞══════════╪═══════╪═══════╪═══════╡
│ A        ┆ 1     ┆ 1     ┆ 1     │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ A        ┆ 2     ┆ None  ┆ 1     │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ A        ┆ 3     ┆ None  ┆ 1     │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ A        ┆ 4     ┆ 4     ┆ 4     │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ B        ┆ 1     ┆ 10    ┆ 10    │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ B        ┆ 2     ┆ None  ┆ 10    │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ B        ┆ 3     ┆ 30    ┆ 30    │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ B        ┆ 4     ┆ None  ┆ 30    │
╰──────────┴───────┴───────┴───────╯
(Showing first 8 rows)
Source code in daft/functions/window.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
def last_value(expr: Expression, ignore_nulls: bool = False) -> Expression:
    """Returns the last value in the window frame.

    Must be used with ``over()`` to specify the window partition, order, and frame.
    When ``ignore_nulls=True``, skips null values and returns the last non-null value.

    Args:
        expr (Expression): The input expression.
        ignore_nulls: whether to ignore null values. Defaults to False.

    Returns:
        Expression: The last value in the window frame.

    Examples:
        >>> import daft
        >>> from daft.functions import last_value
        >>>
        >>> df = daft.from_pydict(
        ...     {
        ...         "category": ["A", "A", "A", "A", "B", "B", "B", "B"],
        ...         "time": [1, 2, 3, 4, 1, 2, 3, 4],
        ...         "value": [1, None, None, 4, 10, None, 30, None],
        ...     }
        ... )
        >>>
        >>> # Forward fill using last_value: look back for the latest preceding non-null value
        >>> window = (
        ...     daft.Window()
        ...     .partition_by("category")
        ...     .order_by("time")
        ...     .rows_between(daft.Window.unbounded_preceding, daft.Window.current_row)
        ... )
        >>> df = df.with_column("ffill", last_value(df["value"], ignore_nulls=True).over(window))
        >>> df.sort(["category", "time"]).show()
        ╭──────────┬───────┬───────┬───────╮
        │ category ┆ time  ┆ value ┆ ffill │
        │ ---      ┆ ---   ┆ ---   ┆ ---   │
        │ String   ┆ Int64 ┆ Int64 ┆ Int64 │
        ╞══════════╪═══════╪═══════╪═══════╡
        │ A        ┆ 1     ┆ 1     ┆ 1     │
        ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ A        ┆ 2     ┆ None  ┆ 1     │
        ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ A        ┆ 3     ┆ None  ┆ 1     │
        ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ A        ┆ 4     ┆ 4     ┆ 4     │
        ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ B        ┆ 1     ┆ 10    ┆ 10    │
        ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ B        ┆ 2     ┆ None  ┆ 10    │
        ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ B        ┆ 3     ┆ 30    ┆ 30    │
        ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ B        ┆ 4     ┆ None  ┆ 30    │
        ╰──────────┴───────┴───────┴───────╯
        <BLANKLINE>
        (Showing first 8 rows)
    """
    return Expression._from_pyexpr(expr._expr.last_value(ignore_nulls))