Skip to content

Commit f760e70

Browse files
timsaucerclaude
andcommitted
Document bracket syntax shorthand in get_field docstring
Note that expr["field"] is a convenient alternative when the field name is a static string, and get_field is needed for dynamic expressions. Add a second doctest example showing the bracket syntax. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b627d30 commit f760e70

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

python/datafusion/functions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,6 +2701,10 @@ def arrow_metadata(expr: Expr, key: Expr | str | None = None) -> Expr:
27012701
def get_field(expr: Expr, name: Expr | str) -> Expr:
27022702
"""Extracts a field from a struct or map by name.
27032703
2704+
When the field name is a static string, the bracket operator
2705+
``expr["field"]`` is a convenient shorthand. Use ``get_field``
2706+
when the field name is a dynamic expression.
2707+
27042708
Examples:
27052709
>>> ctx = dfn.SessionContext()
27062710
>>> df = ctx.from_pydict({"a": [1], "b": [2]})
@@ -2715,6 +2719,14 @@ def get_field(expr: Expr, name: Expr | str) -> Expr:
27152719
... )
27162720
>>> result.collect_column("x_val")[0].as_py()
27172721
1
2722+
2723+
Equivalent using bracket syntax:
2724+
2725+
>>> result = df.select(
2726+
... dfn.col("s")["x"].alias("x_val")
2727+
... )
2728+
>>> result.collect_column("x_val")[0].as_py()
2729+
1
27182730
"""
27192731
if isinstance(name, str):
27202732
name = Expr.string_literal(name)

0 commit comments

Comments
 (0)