Skip to content

Commit 68873b3

Browse files
authored
chore: document Expr.__getitem__ (get a field of a struct array) (#5344)
<img width="827" height="796" alt="Screenshot 2025-11-14 at 10 55 55 AM" src="https://github.com/user-attachments/assets/6985750f-6b2b-4351-9c92-1be6ff84f140" /> Signed-off-by: Daniel King <[email protected]>
1 parent ed8d7a9 commit 68873b3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

docs/api/python/expr.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,36 @@ the following expression represents the set of rows for which the `age` column l
3232
.. autofunction:: vortex.expr.literal
3333

3434
.. autoclass:: vortex.expr.Expr
35+
:members:
36+
37+
.. py:method:: __getitem__ (name, /)
38+
39+
Extract a field of a struct array.
40+
41+
:parameters:
42+
43+
- **name** (:class:`.str`) -- The name of the field.
44+
45+
:return type:
46+
47+
:class:`.vortex.Expr`
48+
49+
.. rubric:: Examples
50+
51+
>>> import vortex as vx
52+
>>> import vortex.expr as ve
53+
>>> import pyarrow as pa
54+
>>>
55+
>>> array = pa.array([
56+
... {"x": 1, "y": {"yy": "a"}},
57+
... {"x": 2, "y": {"yy": "b"}},
58+
... ])
59+
>>>
60+
>>> vx.io.write(vx.array(array), '/tmp/foo.vortex')
61+
>>> (vx.file.open('/tmp/foo.vortex')
62+
... .scan(expr=vx.expr.column("y")["yy"] == "a")
63+
... .read_all()
64+
... .to_pylist()
65+
... )
66+
[{'x': 1, 'y': {'yy': 'a'}}]
67+

vortex-python/src/expr/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ impl PyExpr {
159159
py_binary_operator(self_, Operator::Or, coerce_expr(right)?)
160160
}
161161

162+
// Special methods docstrings cannot be defined in Rust. Write a docstring in the corresponding
163+
// rST file. https://github.com/PyO3/pyo3/issues/4326
162164
fn __getitem__(self_: PyRef<'_, Self>, field: String) -> PyResult<PyExpr> {
163165
get_item(field, self_.clone())
164166
}
@@ -236,6 +238,10 @@ pub fn root() -> PyExpr {
236238
/// >>> ve.column("age")
237239
/// <vortex.Expr object at ...>
238240
/// ```
241+
///
242+
/// .. seealso::
243+
///
244+
/// Use :meth:`.vortex.expr.Expr.__getitem__` to retrieve a field of a struct array.
239245
#[pyfunction]
240246
pub fn column<'py>(name: &Bound<'py, PyString>) -> PyResult<Bound<'py, PyExpr>> {
241247
let py = name.py();

0 commit comments

Comments
 (0)