Class: Polars::NameExpr

Inherits:
Object
  • Object
show all
Defined in:
lib/polars/name_expr.rb

Overview

Namespace for expressions that operate on expression names.

Instance Method Summary collapse

Instance Method Details

#keepExpr

Note:

Due to implementation constraints, this method can only be called as the last expression in a chain.

Keep the original root name of the expression.

Examples:

Prevent errors due to potential duplicate column names.

df = Polars::DataFrame.new(
  {
    "a" => [1, 2],
    "b" => [3, 4]
  }
)
df.select((Polars.lit(10) / Polars.all).name.keep)
# =>
# shape: (2, 2)
# ┌──────┬──────────┐
# │ a    ┆ b        │
# │ ---  ┆ ---      │
# │ f64  ┆ f64      │
# ╞══════╪══════════╡
# │ 10.0 ┆ 3.333333 │
# │ 5.0  ┆ 2.5      │
# └──────┴──────────┘

Undo an alias operation.

df.with_columns((Polars.col("a") * 9).alias("c").name.keep)
# =>
# shape: (2, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 9   ┆ 3   │
# │ 18  ┆ 4   │
# └─────┴─────┘

Returns:



51
52
53
# File 'lib/polars/name_expr.rb', line 51

def keep
  Utils.wrap_expr(_rbexpr.name_keep)
end

#map(&function) ⇒ Expr

Rename the output of an expression by mapping a function over the root name.

Examples:

Remove a common suffix and convert to lower case.

df = Polars::DataFrame.new(
  {
    "A_reverse" => [3, 2, 1],
    "B_reverse" => ["z", "y", "x"]
  }
)
df.with_columns(
  Polars.all.reverse.name.map { |c| c.delete_suffix("_reverse").downcase }
)
# =>
# shape: (3, 4)
# ┌───────────┬───────────┬─────┬─────┐
# │ A_reverse ┆ B_reverse ┆ a   ┆ b   │
# │ ---       ┆ ---       ┆ --- ┆ --- │
# │ i64       ┆ str       ┆ i64 ┆ str │
# ╞═══════════╪═══════════╪═════╪═════╡
# │ 3         ┆ z         ┆ 1   ┆ x   │
# │ 2         ┆ y         ┆ 2   ┆ y   │
# │ 1         ┆ x         ┆ 3   ┆ z   │
# └───────────┴───────────┴─────┴─────┘

Returns:



80
81
82
# File 'lib/polars/name_expr.rb', line 80

def map(&function)
  Utils.wrap_expr(_rbexpr.name_map(function))
end

#map_fields(&function) ⇒ Expr

Note:

This only takes effect for struct columns.

Rename fields of a struct by mapping a function over the field name(s).

Examples:

df = Polars::DataFrame.new({"x" => {"a" => 1, "b" => 2}})
df.select(Polars.col("x").name.map_fields  { |x| x.upcase }).schema
# => Polars::Schema({"x"=>Polars::Struct({"A"=>Polars::Int64, "B"=>Polars::Int64})})

Returns:



209
210
211
# File 'lib/polars/name_expr.rb', line 209

def map_fields(&function)
  Utils.wrap_expr(_rbexpr.name_map_fields(function))
end

#prefix(prefix) ⇒ Expr

Add a prefix to the root column name of the expression.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, 3],
    "b" => ["x", "y", "z"]
  }
)
df.with_columns(Polars.all.reverse.name.prefix("reverse_"))
# =>
# shape: (3, 4)
# ┌─────┬─────┬───────────┬───────────┐
# │ a   ┆ b   ┆ reverse_a ┆ reverse_b │
# │ --- ┆ --- ┆ ---       ┆ ---       │
# │ i64 ┆ str ┆ i64       ┆ str       │
# ╞═════╪═════╪═══════════╪═══════════╡
# │ 1   ┆ x   ┆ 3         ┆ z         │
# │ 2   ┆ y   ┆ 2         ┆ y         │
# │ 3   ┆ z   ┆ 1         ┆ x         │
# └─────┴─────┴───────────┴───────────┘

Parameters:

  • prefix (Object)

    Prefix to add to the root column name.

Returns:



110
111
112
# File 'lib/polars/name_expr.rb', line 110

def prefix(prefix)
  Utils.wrap_expr(_rbexpr.name_prefix(prefix))
end

#prefix_fields(prefix) ⇒ Expr

Note:

This only takes effect for struct columns.

Add a prefix to all field names of a struct.

Examples:

df = Polars::DataFrame.new({"x" => {"a" => 1, "b" => 2}})
df.select(Polars.col("x").name.prefix_fields("prefix_")).schema
# => Polars::Schema({"x"=>Polars::Struct({"prefix_a"=>Polars::Int64, "prefix_b"=>Polars::Int64})})

Parameters:

  • prefix (String)

    Prefix to add to the field name.

Returns:



227
228
229
# File 'lib/polars/name_expr.rb', line 227

def prefix_fields(prefix)
  Utils.wrap_expr(_rbexpr.name_prefix_fields(prefix))
end

#replace(pattern, value, literal: false) ⇒ Expr

Replace matching regex/literal substring in the name with a new value.

Examples:

df = Polars::DataFrame.new(
  {
    "n_foo" => [1, 2, 3],
    "n_bar" => ["x", "y", "z"]
  }
)
df.select(Polars.all.name.replace("^n_", "col_"))
# =>
# shape: (3, 2)
# ┌─────────┬─────────┐
# │ col_foo ┆ col_bar │
# │ ---     ┆ ---     │
# │ i64     ┆ str     │
# ╞═════════╪═════════╡
# │ 1       ┆ x       │
# │ 2       ┆ y       │
# │ 3       ┆ z       │
# └─────────┴─────────┘

Parameters:

  • pattern (String)

    A valid regular expression pattern, compatible with the regex crate.

  • value (String)

    String that will replace the matched substring.

  • literal (Boolean) (defaults to: false)

    Treat pattern as a literal string, not a regex.

Returns:



261
262
263
# File 'lib/polars/name_expr.rb', line 261

def replace(pattern, value, literal: false)
  Utils.wrap_expr(_rbexpr.name_replace(pattern, value, literal))
end

#suffix(suffix) ⇒ Expr

Add a suffix to the root column name of the expression.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, 3],
    "b" => ["x", "y", "z"]
  }
)
df.with_columns(Polars.all.reverse.name.suffix("_reverse"))
# =>
# shape: (3, 4)
# ┌─────┬─────┬───────────┬───────────┐
# │ a   ┆ b   ┆ a_reverse ┆ b_reverse │
# │ --- ┆ --- ┆ ---       ┆ ---       │
# │ i64 ┆ str ┆ i64       ┆ str       │
# ╞═════╪═════╪═══════════╪═══════════╡
# │ 1   ┆ x   ┆ 3         ┆ z         │
# │ 2   ┆ y   ┆ 2         ┆ y         │
# │ 3   ┆ z   ┆ 1         ┆ x         │
# └─────┴─────┴───────────┴───────────┘

Parameters:

  • suffix (Object)

    Suffix to add to the root column name.

Returns:



140
141
142
# File 'lib/polars/name_expr.rb', line 140

def suffix(suffix)
  Utils.wrap_expr(_rbexpr.name_suffix(suffix))
end

#suffix_fields(suffix) ⇒ Expr

Note:

This only takes effect for struct columns.

Add a suffix to all field names of a struct.

Examples:

df = Polars::DataFrame.new({"x" => {"a" => 1, "b" => 2}})
df.select(Polars.col("x").name.suffix_fields("_suffix")).schema
# => Polars::Schema({"x"=>Polars::Struct({"a_suffix"=>Polars::Int64, "b_suffix"=>Polars::Int64})})

Parameters:

  • suffix (String)

    Suffix to add to the field name.

Returns:



279
280
281
# File 'lib/polars/name_expr.rb', line 279

def suffix_fields(suffix)
  Utils.wrap_expr(_rbexpr.name_suffix_fields(suffix))
end

#to_lowercaseExpr

Make the root column name lowercase.

Examples:

df = Polars::DataFrame.new(
  {
    "ColX" => [1, 2, 3],
    "ColY" => ["x", "y", "z"],
  }
)
df.with_columns(Polars.all.name.to_lowercase)
# =>
# shape: (3, 4)
# ┌──────┬──────┬──────┬──────┐
# │ ColX ┆ ColY ┆ colx ┆ coly │
# │ ---  ┆ ---  ┆ ---  ┆ ---  │
# │ i64  ┆ str  ┆ i64  ┆ str  │
# ╞══════╪══════╪══════╪══════╡
# │ 1    ┆ x    ┆ 1    ┆ x    │
# │ 2    ┆ y    ┆ 2    ┆ y    │
# │ 3    ┆ z    ┆ 3    ┆ z    │
# └──────┴──────┴──────┴──────┘

Returns:



167
168
169
# File 'lib/polars/name_expr.rb', line 167

def to_lowercase
  Utils.wrap_expr(_rbexpr.name_to_lowercase)
end

#to_uppercaseExpr

Make the root column name uppercase.

Examples:

df = Polars::DataFrame.new(
  {
    "ColX" => [1, 2, 3],
    "ColY" => ["x", "y", "z"]
  }
)
df.with_columns(Polars.all.name.to_uppercase)
# =>
# shape: (3, 4)
# ┌──────┬──────┬──────┬──────┐
# │ ColX ┆ ColY ┆ COLX ┆ COLY │
# │ ---  ┆ ---  ┆ ---  ┆ ---  │
# │ i64  ┆ str  ┆ i64  ┆ str  │
# ╞══════╪══════╪══════╪══════╡
# │ 1    ┆ x    ┆ 1    ┆ x    │
# │ 2    ┆ y    ┆ 2    ┆ y    │
# │ 3    ┆ z    ┆ 3    ┆ z    │
# └──────┴──────┴──────┴──────┘

Returns:



194
195
196
# File 'lib/polars/name_expr.rb', line 194

def to_uppercase
  Utils.wrap_expr(_rbexpr.name_to_uppercase)
end