nodejs-polars
    Preparing search index...

    Interface StructNamespace

    Struct functions for Lazy datatframes

    interface StructNamespace {
        field(name: string): pl.Expr;
        jsonEncode(): pl.Expr;
        nth(index: number): pl.Expr;
        renameFields(names: string[]): pl.Expr;
        withFields(fields: pl.Expr[]): pl.Expr;
    }
    Index

    Methods

    • Access a field by name

      Parameters

      • name: string

        name of the field

      Returns pl.Expr

      pl.DataFrame({
      objs: [
      { a: 1, b: 2.0, c: "abc" },
      { a: 10, b: 20.0, c: "def" },
      ],
      }).select(
      col("objs").struct.field("b"),
      col("objs").struct.field("c").as("last")
      );
      >>shape: (2, 2)
      ┌──────┬──────┐
      blast
      │ --- ┆ --- │
      f64str
      ╞══════╪══════╡
      2.0abc
      20.0def
      └──────┴──────┘
    • Convert this struct to a string column with json values.

      Returns pl.Expr

      pl.DataFrame( {"a": [{"a": [1, 2], "b": [45]}, {"a": [9, 1, 3], "b": null}]}
      ).withColumn(pl.col("a").struct.jsonEncode().alias("encoded"))
      shape: (2, 2)
      ┌──────────────────┬────────────────────────┐
      aencoded
      │ --- ┆ --- │
      struct[2] ┆ str
      ╞══════════════════╪════════════════════════╡
      │ {[1, 2],[45]} ┆ {"a":[1,2],"b":[45]} │
      │ {[9, 1, 3],null} ┆ {"a":[9,1,3],"b":null} │
      └──────────────────┴────────────────────────┘
    • Access a field by index (zero based index)

      Parameters

      • index: number

        index of the field (starts at 0)

      Returns pl.Expr

      pl.DataFrame({
      objs: [
      { a: 1, b: 2.0, c: "abc" },
      { a: 10, b: 20.0, c: "def" },
      ],
      }).select(
      col("objs").struct.nth(1),
      col("objs").struct.nth(2).as("last"),
      );
      >>shape: (2, 2)
      ┌──────┬──────┐
      blast
      │ --- ┆ --- │
      f64str
      ╞══════╪══════╡
      2.0abc
      20.0def
      └──────┴──────┘
    • Rename the fields of a struct

      Parameters

      • names: string[]

        new names of the fields

      Returns pl.Expr

      pl.DataFrame({
      objs: [
      { a: 1, b: 2.0, c: "abc" },
      { a: 10, b: 20.0, c: "def" },
      ]}).select(col("objs").struct.renameFields(["a", "b", "c"]));
      >>shape: (2, 1)
      ┌───────────────────┐
      objs
      │ --- │
      struct[3] │
      ╞═══════════════════╡
      │ {1.0,2.0,"abc"} │
      │ {10.0,20.0,"def"} │
      └───────────────────┘
    • Add/replace fields in a struct

      Parameters

      • fields: pl.Expr[]

        array of expressions for new fields

      Returns pl.Expr

      pl.DataFrame({
      objs: [
      { a: 1, b: 2.0, c: "abc" },
      { a: 10, b: 20.0, c: "def" },
      ],
      more: ["text1", "text2"],
      final: [100, null],
      }).select(
      col("objs").struct.withFields([
      pl.lit(null).alias("d"),
      pl.lit("text").alias("e"),
      ]),
      col("objs")
      .struct.withFields([col("more"), col("final")])
      .alias("new"),
      );
      shape: (2, 2)
      ┌───────────────────────────────┬────────────────────────────────┐
      objsnew
      │ --- ┆ --- │
      struct[5] ┆ struct[5] │
      ╞═══════════════════════════════╪════════════════════════════════╡
      │ {1.0,2.0,"abc",null,"text"} ┆ {1.0,2.0,"abc","text1",100.0} │
      │ {10.0,20.0,"def",null,"text"} ┆ {10.0,20.0,"def","text2",null} │
      └───────────────────────────────┴────────────────────────────────┘