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(&f) ⇒ 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(&f)
  Utils.wrap_expr(_rbexpr.name_map(f))
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

#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

#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