|
85 | 85 | "array_empty", |
86 | 86 | "array_except", |
87 | 87 | "array_extract", |
| 88 | + "array_filter", |
88 | 89 | "array_has", |
89 | 90 | "array_has_all", |
90 | 91 | "array_has_any", |
|
217 | 218 | "list_empty", |
218 | 219 | "list_except", |
219 | 220 | "list_extract", |
| 221 | + "list_filter", |
220 | 222 | "list_has", |
221 | 223 | "list_has_all", |
222 | 224 | "list_has_any", |
@@ -632,6 +634,47 @@ def list_any_match(array: Expr, predicate: Expr | Callable[..., Any]) -> Expr: |
632 | 634 | return array_any_match(array, predicate) |
633 | 635 |
|
634 | 636 |
|
| 637 | +def array_filter(array: Expr, predicate: Expr | Callable[..., Any]) -> Expr: |
| 638 | + """Keep the elements of ``array`` for which ``predicate`` is ``True``. |
| 639 | +
|
| 640 | + ``predicate`` may be a Python callable, converted to a lambda |
| 641 | + automatically, or an explicit lambda built with :py:func:`lambda_`. It must |
| 642 | + return a boolean expression. The result is a new array containing only the |
| 643 | + matching elements. |
| 644 | +
|
| 645 | + Examples: |
| 646 | + Using a Python callable: |
| 647 | +
|
| 648 | + >>> ctx = dfn.SessionContext() |
| 649 | + >>> df = ctx.from_pydict({"a": [[1, 2, 3, 4, 5]]}) |
| 650 | + >>> df.select( |
| 651 | + ... F.array_filter(col("a"), lambda v: v > 2).alias("f") |
| 652 | + ... ).collect_column("f")[0].as_py() |
| 653 | + [3, 4, 5] |
| 654 | +
|
| 655 | + Using an explicit lambda built with :py:func:`lambda_`: |
| 656 | +
|
| 657 | + >>> predicate = F.lambda_(["v"], F.lambda_var("v") > lit(2)) |
| 658 | + >>> df.select( |
| 659 | + ... F.array_filter(col("a"), predicate).alias("f") |
| 660 | + ... ).collect_column("f")[0].as_py() |
| 661 | + [3, 4, 5] |
| 662 | +
|
| 663 | + See Also: |
| 664 | + :py:func:`array_transform`, :py:func:`array_any_match`, :py:func:`lambda_`. |
| 665 | + """ |
| 666 | + return Expr(f.array_filter(array.expr, _to_lambda(predicate).expr)) |
| 667 | + |
| 668 | + |
| 669 | +def list_filter(array: Expr, predicate: Expr | Callable[..., Any]) -> Expr: |
| 670 | + """Keep the elements of a list for which a predicate is ``True``. |
| 671 | +
|
| 672 | + See Also: |
| 673 | + This is an alias for :py:func:`array_filter`. |
| 674 | + """ |
| 675 | + return array_filter(array, predicate) |
| 676 | + |
| 677 | + |
635 | 678 | def in_list(arg: Expr, values: list[Expr], negated: bool = False) -> Expr: |
636 | 679 | """Returns whether the argument is contained within the list ``values``. |
637 | 680 |
|
|
0 commit comments