aboutsummaryrefslogtreecommitdiff
path: root/venv/lib/python3.8/site-packages/narwhals/expr_struct.py
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2025-07-31 17:27:24 -0400
committersotech117 <michael_foiani@brown.edu>2025-07-31 17:27:24 -0400
commit5bf22fc7e3c392c8bd44315ca2d06d7dca7d084e (patch)
tree8dacb0f195df1c0788d36dd0064f6bbaa3143ede /venv/lib/python3.8/site-packages/narwhals/expr_struct.py
parentb832d364da8c2efe09e3f75828caf73c50d01ce3 (diff)
add code for analysis of data
Diffstat (limited to 'venv/lib/python3.8/site-packages/narwhals/expr_struct.py')
-rw-r--r--venv/lib/python3.8/site-packages/narwhals/expr_struct.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/venv/lib/python3.8/site-packages/narwhals/expr_struct.py b/venv/lib/python3.8/site-packages/narwhals/expr_struct.py
new file mode 100644
index 0000000..f09425f
--- /dev/null
+++ b/venv/lib/python3.8/site-packages/narwhals/expr_struct.py
@@ -0,0 +1,48 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING, Generic, TypeVar
+
+if TYPE_CHECKING:
+ from narwhals.expr import Expr
+
+ExprT = TypeVar("ExprT", bound="Expr")
+
+
+class ExprStructNamespace(Generic[ExprT]):
+ def __init__(self, expr: ExprT) -> None:
+ self._expr = expr
+
+ def field(self, name: str) -> ExprT:
+ r"""Retrieve a Struct field as a new expression.
+
+ Arguments:
+ name: Name of the struct field to retrieve.
+
+ Returns:
+ A new expression.
+
+ Examples:
+ >>> import polars as pl
+ >>> import narwhals as nw
+ >>> df_native = pl.DataFrame(
+ ... {"user": [{"id": "0", "name": "john"}, {"id": "1", "name": "jane"}]}
+ ... )
+ >>> df = nw.from_native(df_native)
+ >>> df.with_columns(name=nw.col("user").struct.field("name"))
+ ┌───────────────────────┐
+ | Narwhals DataFrame |
+ |-----------------------|
+ |shape: (2, 2) |
+ |┌──────────────┬──────┐|
+ |│ user ┆ name │|
+ |│ --- ┆ --- │|
+ |│ struct[2] ┆ str │|
+ |╞══════════════╪══════╡|
+ |│ {"0","john"} ┆ john │|
+ |│ {"1","jane"} ┆ jane │|
+ |└──────────────┴──────┘|
+ └───────────────────────┘
+ """
+ return self._expr._with_elementwise_op(
+ lambda plx: self._expr._to_compliant_expr(plx).struct.field(name)
+ )