Skip to content

Commit ea213c4

Browse files
authored
feat: Added struct namespace with field method. (#2146)
1 parent 1b5bbc9 commit ea213c4

31 files changed

+455
-1
lines changed

docs/api-reference/expr_struct.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `narwhals.Expr.struct`
2+
3+
::: narwhals.expr.ExprStructNamespace
4+
handler: python
5+
options:
6+
members:
7+
- field
8+
show_source: false
9+
show_bases: false

docs/api-reference/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [narwhals.Expr.list](expr_list.md)
99
- [narwhals.Expr.name](expr_name.md)
1010
- [narwhals.Expr.str](expr_str.md)
11+
- [narwhals.Expr.struct](expr_struct.md)
1112
- [narwhals.GroupBy](group_by.md)
1213
- [narwhals.LazyGroupBy](lazy_group_by.md)
1314
- [narwhals.LazyFrame](lazyframe.md)
@@ -17,6 +18,7 @@
1718
- [narwhals.Series.dt](series_dt.md)
1819
- [narwhals.Series.list](series_list.md)
1920
- [narwhals.Series.str](series_str.md)
21+
- [narwhals.Series.struct](series_struct.md)
2022
- [narwhals.dependencies](dependencies.md)
2123
- [narwhals.Implementation](implementation.md)
2224
- [narwhals.dtypes](dtypes.md)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `narwhals.Series.struct`
2+
3+
::: narwhals.series.SeriesStructNamespace
4+
handler: python
5+
options:
6+
members:
7+
- field
8+
show_source: false
9+
show_bases: false

mkdocs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ nav:
3434
- Supported Expr.list methods: api-completeness/expr_list.md
3535
- Supported Expr.name methods: api-completeness/expr_name.md
3636
- Supported Expr.str methods: api-completeness/expr_str.md
37+
- Supported Expr.struct methods: api-completeness/expr_struct.md
3738
- Supported Series methods: api-completeness/series.md
3839
- Supported Series.cat methods: api-completeness/series_cat.md
3940
- Supported Series.dt methods: api-completeness/series_dt.md
4041
- Supported Series.list methods: api-completeness/series_list.md
4142
- Supported Series.str methods: api-completeness/series_str.md
43+
- Supported Series.struct methods: api-completeness/series_struct.md
4244
- API Reference:
4345
- api-reference/index.md
4446
- api-reference/narwhals.md
@@ -49,6 +51,7 @@ nav:
4951
- api-reference/expr_list.md
5052
- api-reference/expr_name.md
5153
- api-reference/expr_str.md
54+
- api-reference/expr_struct.md
5255
- api-reference/group_by.md
5356
- api-reference/lazy_group_by.md
5457
- api-reference/lazyframe.md
@@ -58,6 +61,7 @@ nav:
5861
- api-reference/series_dt.md
5962
- api-reference/series_list.md
6063
- api-reference/series_str.md
64+
- api-reference/series_struct.md
6165
- api-reference/dependencies.md
6266
- api-reference/implementation.md
6367
- api-reference/dtypes.md

narwhals/_arrow/expr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from narwhals._arrow.expr_list import ArrowExprListNamespace
1515
from narwhals._arrow.expr_name import ArrowExprNameNamespace
1616
from narwhals._arrow.expr_str import ArrowExprStringNamespace
17+
from narwhals._arrow.expr_struct import ArrowExprStructNamespace
1718
from narwhals._arrow.series import ArrowSeries
1819
from narwhals._expression_parsing import ExprKind
1920
from narwhals._expression_parsing import evaluate_output_names_and_aliases
@@ -635,3 +636,7 @@ def name(self: Self) -> ArrowExprNameNamespace:
635636
@property
636637
def list(self: Self) -> ArrowExprListNamespace:
637638
return ArrowExprListNamespace(self)
639+
640+
@property
641+
def struct(self: Self) -> ArrowExprStructNamespace:
642+
return ArrowExprStructNamespace(self)

narwhals/_arrow/expr_struct.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from narwhals._expression_parsing import reuse_series_namespace_implementation
6+
7+
if TYPE_CHECKING:
8+
from typing_extensions import Self
9+
10+
from narwhals._arrow.expr import ArrowExpr
11+
12+
13+
class ArrowExprStructNamespace:
14+
def __init__(self: Self, expr: ArrowExpr) -> None:
15+
self._compliant_expr = expr
16+
17+
def field(self: Self, name: str) -> ArrowExpr:
18+
return reuse_series_namespace_implementation(
19+
self._compliant_expr,
20+
"struct",
21+
"field",
22+
name=name,
23+
).alias(name)

narwhals/_arrow/series.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from narwhals._arrow.series_dt import ArrowSeriesDateTimeNamespace
1717
from narwhals._arrow.series_list import ArrowSeriesListNamespace
1818
from narwhals._arrow.series_str import ArrowSeriesStringNamespace
19+
from narwhals._arrow.series_struct import ArrowSeriesStructNamespace
1920
from narwhals._arrow.utils import cast_for_truediv
2021
from narwhals._arrow.utils import chunked_array
2122
from narwhals._arrow.utils import extract_native
@@ -1213,3 +1214,7 @@ def str(self: Self) -> ArrowSeriesStringNamespace:
12131214
@property
12141215
def list(self: Self) -> ArrowSeriesListNamespace:
12151216
return ArrowSeriesListNamespace(self)
1217+
1218+
@property
1219+
def struct(self: Self) -> ArrowSeriesStructNamespace:
1220+
return ArrowSeriesStructNamespace(self)

narwhals/_arrow/series_struct.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pyarrow.compute as pc
6+
7+
if TYPE_CHECKING:
8+
from typing_extensions import Self
9+
10+
from narwhals._arrow.series import ArrowSeries
11+
12+
13+
class ArrowSeriesStructNamespace:
14+
def __init__(self: Self, series: ArrowSeries) -> None:
15+
self._compliant_series: ArrowSeries = series
16+
17+
def field(self: Self, name: str) -> ArrowSeries:
18+
return self._compliant_series._from_native_series(
19+
pc.struct_field(self._compliant_series._native_series, name),
20+
).alias(name)

narwhals/_dask/expr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,3 +652,4 @@ def name(self: Self) -> DaskExprNameNamespace:
652652

653653
cat = not_implemented() # pyright: ignore[reportAssignmentType]
654654
list = not_implemented() # pyright: ignore[reportAssignmentType]
655+
struct = not_implemented() # pyright: ignore[reportAssignmentType]

narwhals/_duckdb/expr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from narwhals._duckdb.expr_list import DuckDBExprListNamespace
1919
from narwhals._duckdb.expr_name import DuckDBExprNameNamespace
2020
from narwhals._duckdb.expr_str import DuckDBExprStringNamespace
21+
from narwhals._duckdb.expr_struct import DuckDBExprStructNamespace
2122
from narwhals._duckdb.utils import lit
2223
from narwhals._duckdb.utils import maybe_evaluate_expr
2324
from narwhals._duckdb.utils import narwhals_to_native_dtype
@@ -484,6 +485,10 @@ def name(self: Self) -> DuckDBExprNameNamespace:
484485
def list(self: Self) -> DuckDBExprListNamespace:
485486
return DuckDBExprListNamespace(self)
486487

488+
@property
489+
def struct(self: Self) -> DuckDBExprStructNamespace:
490+
return DuckDBExprStructNamespace(self)
491+
487492
arg_min = not_implemented()
488493
arg_max = not_implemented()
489494
arg_true = not_implemented()

0 commit comments

Comments
 (0)