Skip to content

Expr.Struct (StructOps) ​

Namespace: Polars.CSharp

Operations on Struct columns. Access via Expr.Struct.

Properties & Indexers ​

PropertyTypeDescription
this[long index]ExprRetrieve a field by its index.
this[string name]ExprRetrieve one of the fields of this Struct as a new expr.
this[string[] name]ExprRetrieve a field from the struct by name.

Instance Methods ​

Drop ​

Drop one or more fields from the struct.

csharp
public Expr Drop(IEnumerable<string> names, bool strict = true)

Parameters

NameTypeDescription
namesIEnumerable<string>Names of the fields to drop.
strictboolIf True, raise an error if any of the specified fields do not exist in the struct.

Field ​

Retrieve a field from the struct by name.

Retrieve a field by its index.

csharp
public Expr Field(params string[] name)
public Expr Field(long index)

Parameters

NameTypeDescription
namestring[]The name of the field.
indexlong-

Example

csharp
var df = DataFrame.FromColumns(new
{
    id = new[] { 1, 2 },
    product = new[] 
    { 
        new { Name = "Laptop", Specs = new { Ram = 16, SSD = 512 } },
        new { Name = "Mouse",  Specs = new { Ram = 0,  SSD = 0   } }
    }
});

df.Select(
    Pl.Col("id"),
    Pl.Col("product").Struct.Field("Name").Alias("prod_name"),
    // Nested Access
    Pl.Col("product").Struct.Field("Specs").Struct.Field("Ram").Alias("ram_gb")
).Show();
/* Output:
shape: (2, 3)
┌─────┬───────────┬────────┐
│ id  ┆ prod_name ┆ ram_gb │
│ --- ┆ ---       ┆ ---    │
│ i32 ┆ str       ┆ i32    │
╞═════╪═══════════╪════════╡
│ 1   ┆ Laptop    ┆ 16     │
│ 2   ┆ Mouse     ┆ 0      │
└─────┴───────────┴────────┘
*/

JsonEncode ​

Convert the struct column into a JSON string column. Useful for debugging or exporting to systems that support JSON strings.

csharp
public Expr JsonEncode()

RenameFields ​

Rename the fields of the struct.

csharp
public Expr RenameFields(params string[] names)

Parameters

NameTypeDescription
namesstring[]The new names for the fields.

Example

csharp
df.Select(
    Pl.Col("product").Struct.RenameFields(["NewName", "NewSpecs"])
);

Unnest ​

Expand the struct into its individual fields.Alias for Expr.Struct.field("*").

csharp
public Expr Unnest()

WithFields ​

Add or overwrite fields of this struct.This is similar to with_columns on DataFrame.

csharp
public Expr WithFields(params IntoExprColumn[] fields)

Parameters

NameTypeDescription
fieldsIntoExprColumn[]-

Released under the MIT License. Powered by the Polars Rust core & Apache Arrow.