From ba83733e0558b58f98ab9be66e19124467a0d720 Mon Sep 17 00:00:00 2001 From: Eric Hutchins Date: Fri, 16 Aug 2024 11:41:09 -0400 Subject: [PATCH 1/2] Update struct.md StructChunked::new is no longer available. I think from_series is the correct replacement --- docs/struct.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/struct.md b/docs/struct.md index 1336a2d..80ec89e 100644 --- a/docs/struct.md +++ b/docs/struct.md @@ -76,7 +76,7 @@ fn shift_struct(inputs: &[Series]) -> PolarsResult { }) .collect::>(); fields.push(field_0); - StructChunked::new(struct_.name(), &fields).map(|ca| ca.into_series()) + StructChunked::from_series(struct_.name(), &fields).map(|ca| ca.into_series()) } ``` From d90c0598fbb87d5133c944af6cae131c7a2462a2 Mon Sep 17 00:00:00 2001 From: Eric Hutchins Date: Fri, 16 Aug 2024 12:01:59 -0400 Subject: [PATCH 2/2] match shift_struct from expressions.rs --- docs/struct.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/struct.md b/docs/struct.md index 80ec89e..ecbbb71 100644 --- a/docs/struct.md +++ b/docs/struct.md @@ -23,12 +23,14 @@ def shift_struct(expr: IntoExpr) -> pl.Expr: On the Rust side, we need to start by activating the necessary feature - in `Cargo.toml`, please make this change: + ```diff -polars = { version = "0.42.0", default-features = false } +polars = { version = "0.42.0", features=["dtype-struct"], default-features = false } ``` Then, we need to get the schema right. + ```Rust fn shifted_struct(input_fields: &[Field]) -> PolarsResult { let field = &input_fields[0]; @@ -49,6 +51,7 @@ fn shifted_struct(input_fields: &[Field]) -> PolarsResult { } } ``` + In this case, I put the first field's name as the output struct's name, but it doesn't really matter what we put, as Polars doesn't allow us to rename expressions within plugins. You can always rename on the Python side if you really want to, but I'd suggest @@ -60,7 +63,7 @@ The function definition is going to follow a similar logic: #[polars_expr(output_type_func=shifted_struct)] fn shift_struct(inputs: &[Series]) -> PolarsResult { let struct_ = inputs[0].struct_()?; - let fields = struct_.fields(); + let fields = struct_.fields_as_series(); if fields.is_empty() { return Ok(inputs[0].clone()); } @@ -114,12 +117,15 @@ shape: (3, 2) The values look right - but is the schema? Let's take a look + ``` import pprint pprint.pprint(df.with_columns(abc_shifted=mp.shift_struct("abc")).schema) ``` + ``` OrderedDict([('abc', Struct({'a': Int64, 'b': Float64, 'c': String})), ('abc_shifted', Struct({'a': Float64, 'b': String, 'c': Int64}))]) ``` + Looks correct!