Class: Polars::Expr

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

Overview

Expressions that can be used in various contexts.

Instance Method Summary collapse

Instance Method Details

#!=(other) ⇒ Expr

Not equal.

Returns:



116
117
118
# File 'lib/polars/expr.rb', line 116

def !=(other)
  wrap_expr(_rbexpr.neq(_to_expr(other)._rbexpr))
end

#%(other) ⇒ Expr

Returns the modulo.

Returns:



81
82
83
# File 'lib/polars/expr.rb', line 81

def %(other)
  wrap_expr(_rbexpr % _to_rbexpr(other))
end

#&(other) ⇒ Expr

Bitwise AND.

Returns:



32
33
34
# File 'lib/polars/expr.rb', line 32

def &(other)
  wrap_expr(_rbexpr._and(_to_rbexpr(other)))
end

#*(other) ⇒ Expr

Performs multiplication.

Returns:



60
61
62
# File 'lib/polars/expr.rb', line 60

def *(other)
  wrap_expr(_rbexpr * _to_rbexpr(other))
end

#**(power) ⇒ Expr

Raises to the power of exponent.

Returns:



88
89
90
# File 'lib/polars/expr.rb', line 88

def **(power)
  pow(power)
end

#+(other) ⇒ Expr

Performs addition.

Returns:



46
47
48
# File 'lib/polars/expr.rb', line 46

def +(other)
  wrap_expr(_rbexpr + _to_rbexpr(other))
end

#-(other) ⇒ Expr

Performs subtraction.

Returns:



53
54
55
# File 'lib/polars/expr.rb', line 53

def -(other)
  wrap_expr(_rbexpr - _to_rbexpr(other))
end

#-@Expr

Performs negation.

Returns:



137
138
139
# File 'lib/polars/expr.rb', line 137

def -@
  Utils.lit(0) - self
end

#/(other) ⇒ Expr

Performs division.

Returns:



67
68
69
# File 'lib/polars/expr.rb', line 67

def /(other)
  wrap_expr(_rbexpr / _to_rbexpr(other))
end

#<(other) ⇒ Expr

Less than.

Returns:



123
124
125
# File 'lib/polars/expr.rb', line 123

def <(other)
  wrap_expr(_rbexpr.lt(_to_expr(other)._rbexpr))
end

#<=(other) ⇒ Expr

Less than or equal.

Returns:



102
103
104
# File 'lib/polars/expr.rb', line 102

def <=(other)
  wrap_expr(_rbexpr.lt_eq(_to_expr(other)._rbexpr))
end

#==(other) ⇒ Expr

Equal.

Returns:



109
110
111
# File 'lib/polars/expr.rb', line 109

def ==(other)
  wrap_expr(_rbexpr.eq(_to_expr(other)._rbexpr))
end

#>(other) ⇒ Expr

Greater than.

Returns:



130
131
132
# File 'lib/polars/expr.rb', line 130

def >(other)
  wrap_expr(_rbexpr.gt(_to_expr(other)._rbexpr))
end

#>=(other) ⇒ Expr

Greater than or equal.

Returns:



95
96
97
# File 'lib/polars/expr.rb', line 95

def >=(other)
  wrap_expr(_rbexpr.gt_eq(_to_expr(other)._rbexpr))
end

#^(other) ⇒ Expr

Bitwise XOR.

Returns:



25
26
27
# File 'lib/polars/expr.rb', line 25

def ^(other)
  wrap_expr(_rbexpr._xor(_to_rbexpr(other)))
end

#_hash(seed = 0, seed_1 = nil, seed_2 = nil, seed_3 = nil) ⇒ Expr

Hash the elements in the selection.

The hash value is of type :u64.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, nil],
    "b" => ["x", nil, "z"]
  }
)
df.with_column(Polars.all._hash(10, 20, 30, 40))
# =>
# shape: (3, 2)
# ┌──────────────────────┬──────────────────────┐
# │ a                    ┆ b                    │
# │ ---                  ┆ ---                  │
# │ u64                  ┆ u64                  │
# ╞══════════════════════╪══════════════════════╡
# │ 4629889412789719550  ┆ 6959506404929392568  │
# ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 16386608652769605760 ┆ 11638928888656214026 │
# ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 11638928888656214026 ┆ 11040941213715918520 │
# └──────────────────────┴──────────────────────┘

Parameters:

  • seed (Integer) (defaults to: 0)

    Random seed parameter. Defaults to 0.

  • seed_1 (Integer) (defaults to: nil)

    Random seed parameter. Defaults to seed if not set.

  • seed_2 (Integer) (defaults to: nil)

    Random seed parameter. Defaults to seed if not set.

  • seed_3 (Integer) (defaults to: nil)

    Random seed parameter. Defaults to seed if not set.

Returns:



3048
3049
3050
3051
3052
3053
3054
# File 'lib/polars/expr.rb', line 3048

def _hash(seed = 0, seed_1 = nil, seed_2 = nil, seed_3 = nil)
  k0 = seed
  k1 = seed_1.nil? ? seed : seed_1
  k2 = seed_2.nil? ? seed : seed_2
  k3 = seed_3.nil? ? seed : seed_3
  wrap_expr(_rbexpr._hash(k0, k1, k2, k3))
end

#absExpr

Compute absolute values.

Examples:

df = Polars::DataFrame.new(
  {
    "A" => [-1.0, 0.0, 1.0, 2.0]
  }
)
df.select(Polars.col("A").abs)
# =>
# shape: (4, 1)
# ┌─────┐
# │ A   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.0 │
# ├╌╌╌╌╌┤
# │ 0.0 │
# ├╌╌╌╌╌┤
# │ 1.0 │
# ├╌╌╌╌╌┤
# │ 2.0 │
# └─────┘

Returns:



4015
4016
4017
# File 'lib/polars/expr.rb', line 4015

def abs
  wrap_expr(_rbexpr.abs)
end

#agg_groupsExpr

Get the group indexes of the group by operation.

Should be used in aggregation context only.

Examples:

df = Polars::DataFrame.new(
  {
    "group" => [
      "one",
      "one",
      "one",
      "two",
      "two",
      "two"
    ],
    "value" => [94, 95, 96, 97, 97, 99]
  }
)
df.groupby("group", maintain_order: true).agg(Polars.col("value").agg_groups)
# =>
# shape: (2, 2)
# ┌───────┬───────────┐
# │ group ┆ value     │
# │ ---   ┆ ---       │
# │ str   ┆ list[u32] │
# ╞═══════╪═══════════╡
# │ one   ┆ [0, 1, 2] │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ two   ┆ [3, 4, 5] │
# └───────┴───────────┘

Returns:



734
735
736
# File 'lib/polars/expr.rb', line 734

def agg_groups
  wrap_expr(_rbexpr.agg_groups)
end

#alias(name) ⇒ Expr

Rename the output of an expression.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, 3],
    "b" => ["a", "b", nil]
  }
)
df.select(
  [
    Polars.col("a").alias("bar"),
    Polars.col("b").alias("foo")
  ]
)
# =>
# shape: (3, 2)
# ┌─────┬──────┐
# │ bar ┆ foo  │
# │ --- ┆ ---  │
# │ i64 ┆ str  │
# ╞═════╪══════╡
# │ 1   ┆ a    │
# ├╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ 2   ┆ b    │
# ├╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ 3   ┆ null │
# └─────┴──────┘

Parameters:

  • name (String)

    New name.

Returns:



331
332
333
# File 'lib/polars/expr.rb', line 331

def alias(name)
  wrap_expr(_rbexpr._alias(name))
end

#allBoolean

Check if all boolean values in a Boolean column are true.

This method is an expression - not to be confused with Polars.all which is a function to select all columns.

Examples:

df = Polars::DataFrame.new(
  {"TT" => [true, true], "TF" => [true, false], "FF" => [false, false]}
)
df.select(Polars.col("*").all)
# =>
# shape: (1, 3)
# ┌──────┬───────┬───────┐
# │ TT   ┆ TF    ┆ FF    │
# │ ---  ┆ ---   ┆ ---   │
# │ bool ┆ bool  ┆ bool  │
# ╞══════╪═══════╪═══════╡
# │ true ┆ false ┆ false │
# └──────┴───────┴───────┘

Returns:



222
223
224
# File 'lib/polars/expr.rb', line 222

def all
  wrap_expr(_rbexpr.all)
end

#anyBoolean

Check if any boolean value in a Boolean column is true.

Examples:

df = Polars::DataFrame.new({"TF" => [true, false], "FF" => [false, false]})
df.select(Polars.all.any)
# =>
# shape: (1, 2)
# ┌──────┬───────┐
# │ TF   ┆ FF    │
# │ ---  ┆ ---   │
# │ bool ┆ bool  │
# ╞══════╪═══════╡
# │ true ┆ false │
# └──────┴───────┘

Returns:



197
198
199
# File 'lib/polars/expr.rb', line 197

def any
  wrap_expr(_rbexpr.any)
end

#append(other, upcast: true) ⇒ Expr

Append expressions.

This is done by adding the chunks of other to this Series.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [8, 9, 10],
    "b" => [nil, 4, 4]
  }
)
df.select(Polars.all.head(1).append(Polars.all.tail(1)))
# =>
# shape: (2, 2)
# ┌─────┬──────┐
# │ a   ┆ b    │
# │ --- ┆ ---  │
# │ i64 ┆ i64  │
# ╞═════╪══════╡
# │ 8   ┆ null │
# ├╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ 10  ┆ 4    │
# └─────┴──────┘

Parameters:

  • other (Expr)

    Expression to append.

  • upcast (Boolean) (defaults to: true)

    Cast both Series to the same supertype.

Returns:



849
850
851
852
# File 'lib/polars/expr.rb', line 849

def append(other, upcast: true)
  other = Utils.expr_to_lit_or_expr(other)
  wrap_expr(_rbexpr.append(other._rbexpr, upcast))
end

#arccosExpr

Compute the element-wise value for the inverse cosine.

Examples:

df = Polars::DataFrame.new({"a" => [0.0]})
df.select(Polars.col("a").arccos)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 1.570796 │
# └──────────┘

Returns:



4532
4533
4534
# File 'lib/polars/expr.rb', line 4532

def arccos
  wrap_expr(_rbexpr.arccos)
end

#arccoshExpr

Compute the element-wise value for the inverse hyperbolic cosine.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").arccosh)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 0.0 │
# └─────┘

Returns:



4652
4653
4654
# File 'lib/polars/expr.rb', line 4652

def arccosh
  wrap_expr(_rbexpr.arccosh)
end

#arcsinExpr

Compute the element-wise value for the inverse sine.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").arcsin)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 1.570796 │
# └──────────┘

Returns:



4512
4513
4514
# File 'lib/polars/expr.rb', line 4512

def arcsin
  wrap_expr(_rbexpr.arcsin)
end

#arcsinhExpr

Compute the element-wise value for the inverse hyperbolic sine.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").arcsinh)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 0.881374 │
# └──────────┘

Returns:



4632
4633
4634
# File 'lib/polars/expr.rb', line 4632

def arcsinh
  wrap_expr(_rbexpr.arcsinh)
end

#arctanExpr

Compute the element-wise value for the inverse tangent.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").arctan)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 0.785398 │
# └──────────┘

Returns:



4552
4553
4554
# File 'lib/polars/expr.rb', line 4552

def arctan
  wrap_expr(_rbexpr.arctan)
end

#arctanhExpr

Compute the element-wise value for the inverse hyperbolic tangent.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").arctanh)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ inf │
# └─────┘

Returns:



4672
4673
4674
# File 'lib/polars/expr.rb', line 4672

def arctanh
  wrap_expr(_rbexpr.arctanh)
end

#arg_maxExpr

Get the index of the maximal value.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [20, 10, 30]
  }
)
df.select(Polars.col("a").arg_max)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 2   │
# └─────┘

Returns:



1489
1490
1491
# File 'lib/polars/expr.rb', line 1489

def arg_max
  wrap_expr(_rbexpr.arg_max)
end

#arg_minExpr

Get the index of the minimal value.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [20, 10, 30]
  }
)
df.select(Polars.col("a").arg_min)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 1   │
# └─────┘

Returns:



1513
1514
1515
# File 'lib/polars/expr.rb', line 1513

def arg_min
  wrap_expr(_rbexpr.arg_min)
end

#arg_sort(reverse: false, nulls_last: false) ⇒ Expr

Get the index values that would sort this column.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [20, 10, 30]
  }
)
df.select(Polars.col("a").arg_sort)
# =>
# shape: (3, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 1   │
# ├╌╌╌╌╌┤
# │ 0   │
# ├╌╌╌╌╌┤
# │ 2   │
# └─────┘

Parameters:

  • reverse (Boolean) (defaults to: false)

    Sort in reverse (descending) order.

  • nulls_last (Boolean) (defaults to: false)

    Place null values last instead of first.

Returns:



1465
1466
1467
# File 'lib/polars/expr.rb', line 1465

def arg_sort(reverse: false, nulls_last: false)
  wrap_expr(_rbexpr.arg_sort(reverse, nulls_last))
end

#arg_uniqueExpr

Get index of first unique value.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [8, 9, 10],
    "b" => [nil, 4, 4]
  }
)
df.select(Polars.col("a").arg_unique)
# =>
# shape: (3, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 0   │
# ├╌╌╌╌╌┤
# │ 1   │
# ├╌╌╌╌╌┤
# │ 2   │
# └─────┘
df.select(Polars.col("b").arg_unique)
# =>
# shape: (2, 1)
# ┌─────┐
# │ b   │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 0   │
# ├╌╌╌╌╌┤
# │ 1   │
# └─────┘

Returns:



2192
2193
2194
# File 'lib/polars/expr.rb', line 2192

def arg_unique
  wrap_expr(_rbexpr.arg_unique)
end

#argsort(reverse: false, nulls_last: false) ⇒ expr

Get the index values that would sort this column.

Alias for #arg_sort.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [20, 10, 30]
  }
)
df.select(Polars.col("a").argsort)
# =>
# shape: (3, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 1   │
# ├╌╌╌╌╌┤
# │ 0   │
# ├╌╌╌╌╌┤
# │ 2   │
# └─────┘

Parameters:

  • reverse (Boolean) (defaults to: false)

    Sort in reverse (descending) order.

  • nulls_last (Boolean) (defaults to: false)

    Place null values last instead of first.

Returns:

  • (expr)


4050
4051
4052
# File 'lib/polars/expr.rb', line 4050

def argsort(reverse: false, nulls_last: false)
  arg_sort(reverse: reverse, nulls_last: nulls_last)
end

#arrListExpr

Create an object namespace of all list related methods.

Returns:



5208
5209
5210
# File 'lib/polars/expr.rb', line 5208

def arr
  ListExpr.new(self)
end

#backward_fill(limit: nil) ⇒ Expr

Fill missing values with the next to be seen values.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, nil],
    "b" => [4, nil, 6]
  }
)
df.select(Polars.all.backward_fill)
# =>
# shape: (3, 2)
# ┌──────┬─────┐
# │ a    ┆ b   │
# │ ---  ┆ --- │
# │ i64  ┆ i64 │
# ╞══════╪═════╡
# │ 1    ┆ 4   │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2    ┆ 6   │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┤
# │ null ┆ 6   │
# └──────┴─────┘

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of consecutive null values to backward fill.

Returns:



1887
1888
1889
# File 'lib/polars/expr.rb', line 1887

def backward_fill(limit: nil)
  wrap_expr(_rbexpr.backward_fill(limit))
end

#cast(dtype, strict: true) ⇒ Expr

Cast between data types.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, 3],
    "b" => ["4", "5", "6"]
  }
)
df.with_columns(
  [
    Polars.col("a").cast(:f64),
    Polars.col("b").cast(:i32)
  ]
)
# =>
# shape: (3, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ f64 ┆ i32 │
# ╞═════╪═════╡
# │ 1.0 ┆ 4   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2.0 ┆ 5   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 3.0 ┆ 6   │
# └─────┴─────┘

Parameters:

  • dtype (Symbol)

    DataType to cast to.

  • strict (Boolean) (defaults to: true)

    Throw an error if a cast could not be done. For instance, due to an overflow.

Returns:



1301
1302
1303
1304
# File 'lib/polars/expr.rb', line 1301

def cast(dtype, strict: true)
  dtype = Utils.rb_type_to_dtype(dtype)
  wrap_expr(_rbexpr.cast(dtype, strict))
end

#catCatExpr

Create an object namespace of all categorical related methods.

Returns:



5215
5216
5217
# File 'lib/polars/expr.rb', line 5215

def cat
  CatExpr.new(self)
end

#ceilExpr

Rounds up to the nearest integer value.

Only works on floating point Series.

Examples:

df = Polars::DataFrame.new({"a" => [0.3, 0.5, 1.0, 1.1]})
df.select(Polars.col("a").ceil)
# =>
# shape: (4, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.0 │
# ├╌╌╌╌╌┤
# │ 1.0 │
# ├╌╌╌╌╌┤
# │ 1.0 │
# ├╌╌╌╌╌┤
# │ 2.0 │
# └─────┘

Returns:



1174
1175
1176
# File 'lib/polars/expr.rb', line 1174

def ceil
  wrap_expr(_rbexpr.ceil)
end

#clip(min_val, max_val) ⇒ Expr

Clip (limit) the values in an array to a min and max boundary.

Only works for numerical types.

If you want to clip other dtypes, consider writing a "when, then, otherwise" expression. See when for more information.

Examples:

df = Polars::DataFrame.new({"foo" => [-50, 5, nil, 50]})
df.with_column(Polars.col("foo").clip(1, 10).alias("foo_clipped"))
# =>
# shape: (4, 2)
# ┌──────┬─────────────┐
# │ foo  ┆ foo_clipped │
# │ ---  ┆ ---         │
# │ i64  ┆ i64         │
# ╞══════╪═════════════╡
# │ -50  ┆ 1           │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 5    ┆ 5           │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ null ┆ null        │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 50   ┆ 10          │
# └──────┴─────────────┘

Parameters:

  • min_val (Numeric)

    Minimum value.

  • max_val (Numeric)

    Maximum value.

Returns:



4290
4291
4292
# File 'lib/polars/expr.rb', line 4290

def clip(min_val, max_val)
  wrap_expr(_rbexpr.clip(min_val, max_val))
end

#clip_max(max_val) ⇒ Expr

Clip (limit) the values in an array to a max boundary.

Only works for numerical types.

If you want to clip other dtypes, consider writing a "when, then, otherwise" expression. See when for more information.

Examples:

df = Polars::DataFrame.new({"foo" => [-50, 5, nil, 50]})
df.with_column(Polars.col("foo").clip_max(0).alias("foo_clipped"))
# =>
# shape: (4, 2)
# ┌──────┬─────────────┐
# │ foo  ┆ foo_clipped │
# │ ---  ┆ ---         │
# │ i64  ┆ i64         │
# ╞══════╪═════════════╡
# │ -50  ┆ -50         │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 5    ┆ 0           │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ null ┆ null        │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 50   ┆ 0           │
# └──────┴─────────────┘

Parameters:

  • max_val (Numeric)

    Maximum value.

Returns:



4358
4359
4360
# File 'lib/polars/expr.rb', line 4358

def clip_max(max_val)
  wrap_expr(_rbexpr.clip_max(max_val))
end

#clip_min(min_val) ⇒ Expr

Clip (limit) the values in an array to a min boundary.

Only works for numerical types.

If you want to clip other dtypes, consider writing a "when, then, otherwise" expression. See when for more information.

Examples:

df = Polars::DataFrame.new({"foo" => [-50, 5, nil, 50]})
df.with_column(Polars.col("foo").clip_min(0).alias("foo_clipped"))
# =>
# shape: (4, 2)
# ┌──────┬─────────────┐
# │ foo  ┆ foo_clipped │
# │ ---  ┆ ---         │
# │ i64  ┆ i64         │
# ╞══════╪═════════════╡
# │ -50  ┆ 0           │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 5    ┆ 5           │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ null ┆ null        │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 50   ┆ 50          │
# └──────┴─────────────┘

Parameters:

  • min_val (Numeric)

    Minimum value.

Returns:



4324
4325
4326
# File 'lib/polars/expr.rb', line 4324

def clip_min(min_val)
  wrap_expr(_rbexpr.clip_min(min_val))
end

#cosExpr

Compute the element-wise value for the cosine.

Examples:

df = Polars::DataFrame.new({"a" => [0.0]})
df.select(Polars.col("a").cos)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.0 │
# └─────┘

Returns:



4472
4473
4474
# File 'lib/polars/expr.rb', line 4472

def cos
  wrap_expr(_rbexpr.cos)
end

#coshExpr

Compute the element-wise value for the hyperbolic cosine.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").cosh)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 1.543081 │
# └──────────┘

Returns:



4592
4593
4594
# File 'lib/polars/expr.rb', line 4592

def cosh
  wrap_expr(_rbexpr.cosh)
end

#countExpr

Count the number of values in this expression.

Examples:

df = Polars::DataFrame.new({"a" => [8, 9, 10], "b" => [nil, 4, 4]})
df.select(Polars.all.count)
# =>
# shape: (1, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ u32 ┆ u32 │
# ╞═════╪═════╡
# │ 3   ┆ 3   │
# └─────┴─────┘

Returns:



754
755
756
# File 'lib/polars/expr.rb', line 754

def count
  wrap_expr(_rbexpr.count)
end

#cumcount(reverse: false) ⇒ Expr

Get an array with the cumulative count computed at every element.

Counting from 0 to len

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 4]})
df.select(
  [
    Polars.col("a").cumcount,
    Polars.col("a").cumcount(reverse: true).alias("a_reverse")
  ]
)
# =>
# shape: (4, 2)
# ┌─────┬───────────┐
# │ a   ┆ a_reverse │
# │ --- ┆ ---       │
# │ u32 ┆ u32       │
# ╞═════╪═══════════╡
# │ 0   ┆ 3         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 1   ┆ 2         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 2   ┆ 1         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 3   ┆ 0         │
# └─────┴───────────┘

Parameters:

  • reverse (Boolean) (defaults to: false)

    Reverse the operation.

Returns:



1118
1119
1120
# File 'lib/polars/expr.rb', line 1118

def cumcount(reverse: false)
  wrap_expr(_rbexpr.cumcount(reverse))
end

#cummax(reverse: false) ⇒ Expr

Get an array with the cumulative max computed at every element.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 4]})
df.select(
  [
    Polars.col("a").cummax,
    Polars.col("a").cummax(reverse: true).alias("a_reverse")
  ]
)
# =>
# shape: (4, 2)
# ┌─────┬───────────┐
# │ a   ┆ a_reverse │
# │ --- ┆ ---       │
# │ i64 ┆ i64       │
# ╞═════╪═══════════╡
# │ 1   ┆ 4         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 2   ┆ 4         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 3   ┆ 4         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 4   ┆ 4         │
# └─────┴───────────┘

Parameters:

  • reverse (Boolean) (defaults to: false)

    Reverse the operation.

Returns:



1082
1083
1084
# File 'lib/polars/expr.rb', line 1082

def cummax(reverse: false)
  wrap_expr(_rbexpr.cummax(reverse))
end

#cummin(reverse: false) ⇒ Expr

Get an array with the cumulative min computed at every element.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 4]})
df.select(
  [
    Polars.col("a").cummin,
    Polars.col("a").cummin(reverse: true).alias("a_reverse")
  ]
)
# =>
# shape: (4, 2)
# ┌─────┬───────────┐
# │ a   ┆ a_reverse │
# │ --- ┆ ---       │
# │ i64 ┆ i64       │
# ╞═════╪═══════════╡
# │ 1   ┆ 1         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 1   ┆ 2         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 1   ┆ 3         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 1   ┆ 4         │
# └─────┴───────────┘

Parameters:

  • reverse (Boolean) (defaults to: false)

    Reverse the operation.

Returns:



1048
1049
1050
# File 'lib/polars/expr.rb', line 1048

def cummin(reverse: false)
  wrap_expr(_rbexpr.cummin(reverse))
end

#cumprod(reverse: false) ⇒ Expr

Note:

Dtypes in :i8, :u8, :i16, and :u16 are cast to :i64 before summing to prevent overflow issues.

Get an array with the cumulative product computed at every element.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 4]})
df.select(
  [
    Polars.col("a").cumprod,
    Polars.col("a").cumprod(reverse: true).alias("a_reverse")
  ]
)
# =>
# shape: (4, 2)
# ┌─────┬───────────┐
# │ a   ┆ a_reverse │
# │ --- ┆ ---       │
# │ i64 ┆ i64       │
# ╞═════╪═══════════╡
# │ 1   ┆ 24        │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 2   ┆ 24        │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 6   ┆ 12        │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 24  ┆ 4         │
# └─────┴───────────┘

Parameters:

  • reverse (Boolean) (defaults to: false)

    Reverse the operation.

Returns:



1014
1015
1016
# File 'lib/polars/expr.rb', line 1014

def cumprod(reverse: false)
  wrap_expr(_rbexpr.cumprod(reverse))
end

#cumsum(reverse: false) ⇒ Expr

Note:

Dtypes in :i8, :u8, :i16, and :u16 are cast to :i64 before summing to prevent overflow issues.

Get an array with the cumulative sum computed at every element.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 4]})
df.select(
  [
    Polars.col("a").cumsum,
    Polars.col("a").cumsum(reverse: true).alias("a_reverse")
  ]
)
# =>
# shape: (4, 2)
# ┌─────┬───────────┐
# │ a   ┆ a_reverse │
# │ --- ┆ ---       │
# │ i64 ┆ i64       │
# ╞═════╪═══════════╡
# │ 1   ┆ 10        │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 3   ┆ 9         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 6   ┆ 7         │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 10  ┆ 4         │
# └─────┴───────────┘

Parameters:

  • reverse (Boolean) (defaults to: false)

    Reverse the operation.

Returns:



976
977
978
# File 'lib/polars/expr.rb', line 976

def cumsum(reverse: false)
  wrap_expr(_rbexpr.cumsum(reverse))
end

#cumulative_eval(expr, min_periods: 1, parallel: false) ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

This can be really slow as it can have O(n^2) complexity. Don't use this for operations that visit all elements.

Run an expression over a sliding window that increases 1 slot every iteration.

Examples:

df = Polars::DataFrame.new({"values" => [1, 2, 3, 4, 5]})
df.select(
  [
    Polars.col("values").cumulative_eval(
      Polars.element.first - Polars.element.last ** 2
    )
  ]
)
# =>
# shape: (5, 1)
# ┌────────┐
# │ values │
# │ ---    │
# │ f64    │
# ╞════════╡
# │ 0.0    │
# ├╌╌╌╌╌╌╌╌┤
# │ -3.0   │
# ├╌╌╌╌╌╌╌╌┤
# │ -8.0   │
# ├╌╌╌╌╌╌╌╌┤
# │ -15.0  │
# ├╌╌╌╌╌╌╌╌┤
# │ -24.0  │
# └────────┘

Parameters:

  • expr (Expr)

    Expression to evaluate

  • min_periods (Integer) (defaults to: 1)

    Number of valid values there should be in the window before the expression is evaluated. valid values = length - null_count

  • parallel (Boolean) (defaults to: false)

    Run in parallel. Don't do this in a groupby or another operation that already has much parallelization.

Returns:



5108
5109
5110
5111
5112
# File 'lib/polars/expr.rb', line 5108

def cumulative_eval(expr, min_periods: 1, parallel: false)
  wrap_expr(
    _rbexpr.cumulative_eval(expr._rbexpr, min_periods, parallel)
  )
end

#diff(n: 1, null_behavior: "ignore") ⇒ Expr

Calculate the n-th discrete difference.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [20, 10, 30]
  }
)
df.select(Polars.col("a").diff)
# =>
# shape: (3, 1)
# ┌──────┐
# │ a    │
# │ ---  │
# │ i64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ -10  │
# ├╌╌╌╌╌╌┤
# │ 20   │
# └──────┘

Parameters:

  • n (Integer) (defaults to: 1)

    Number of slots to shift.

  • null_behavior ("ignore", "drop") (defaults to: "ignore")

    How to handle null values.

Returns:



4153
4154
4155
# File 'lib/polars/expr.rb', line 4153

def diff(n: 1, null_behavior: "ignore")
  wrap_expr(_rbexpr.diff(n, null_behavior))
end

#dot(other) ⇒ Expr

Compute the dot/inner product between two Expressions.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 3, 5],
    "b" => [2, 4, 6]
  }
)
df.select(Polars.col("a").dot(Polars.col("b")))
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 44  │
# └─────┘

Parameters:

  • other (Expr)

    Expression to compute dot product with.

Returns:



1231
1232
1233
1234
# File 'lib/polars/expr.rb', line 1231

def dot(other)
  other = Utils.expr_to_lit_or_expr(other, str_to_lit: false)
  wrap_expr(_rbexpr.dot(other._rbexpr))
end

#drop_nansExpr

Drop floating point NaN values.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [8, 9, 10, 11],
    "b" => [nil, 4.0, 4.0, Float::NAN]
  }
)
df.select(Polars.col("b").drop_nans)
# =>
# shape: (3, 1)
# ┌──────┐
# │ b    │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ 4.0  │
# ├╌╌╌╌╌╌┤
# │ 4.0  │
# └──────┘

Returns:



938
939
940
# File 'lib/polars/expr.rb', line 938

def drop_nans
  wrap_expr(_rbexpr.drop_nans)
end

#drop_nullsExpr

Drop null values.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [8, 9, 10, 11],
    "b" => [nil, 4.0, 4.0, Float::NAN]
  }
)
df.select(Polars.col("b").drop_nulls)
# =>
# shape: (3, 1)
# ┌─────┐
# │ b   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 4.0 │
# ├╌╌╌╌╌┤
# │ 4.0 │
# ├╌╌╌╌╌┤
# │ NaN │
# └─────┘

Returns:



909
910
911
# File 'lib/polars/expr.rb', line 909

def drop_nulls
  wrap_expr(_rbexpr.drop_nulls)
end

#dtDateTimeExpr

Create an object namespace of all datetime related methods.

Returns:



5222
5223
5224
# File 'lib/polars/expr.rb', line 5222

def dt
  DateTimeExpr.new(self)
end

#entropy(base: 2, normalize: true) ⇒ Expr

Computes the entropy.

Uses the formula -sum(pk * log(pk) where pk are discrete probabilities.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3]})
df.select(Polars.col("a").entropy(base: 2))
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 1.459148 │
# └──────────┘
df.select(Polars.col("a").entropy(base: 2, normalize: false))
# =>
# shape: (1, 1)
# ┌───────────┐
# │ a         │
# │ ---       │
# │ f64       │
# ╞═══════════╡
# │ -6.754888 │
# └───────────┘

Parameters:

  • base (Float) (defaults to: 2)

    Given base, defaults to e.

  • normalize (Boolean) (defaults to: true)

    Normalize pk if it doesn't sum to 1.

Returns:



5057
5058
5059
# File 'lib/polars/expr.rb', line 5057

def entropy(base: 2, normalize: true)
  wrap_expr(_rbexpr.entropy(base, normalize))
end

#ewm_mean(com: nil, span: nil, half_life: nil, alpha: nil, adjust: true, min_periods: 1) ⇒ Expr

Exponentially-weighted moving average.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3]})
df.select(Polars.col("a").ewm_mean(com: 1))
# =>
# shape: (3, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 1.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.666667 │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 2.428571 │
# └──────────┘

Returns:



4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
# File 'lib/polars/expr.rb', line 4810

def ewm_mean(
  com: nil,
  span: nil,
  half_life: nil,
  alpha: nil,
  adjust: true,
  min_periods: 1
)
  alpha = _prepare_alpha(com, span, half_life, alpha)
  wrap_expr(_rbexpr.ewm_mean(alpha, adjust, min_periods))
end

#ewm_std(com: nil, span: nil, half_life: nil, alpha: nil, adjust: true, bias: false, min_periods: 1) ⇒ Expr

Exponentially-weighted moving standard deviation.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3]})
df.select(Polars.col("a").ewm_std(com: 1))
# =>
# shape: (3, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 0.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 0.707107 │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 0.963624 │
# └──────────┘

Returns:



4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
# File 'lib/polars/expr.rb', line 4842

def ewm_std(
  com: nil,
  span: nil,
  half_life: nil,
  alpha: nil,
  adjust: true,
  bias: false,
  min_periods: 1
)
  alpha = _prepare_alpha(com, span, half_life, alpha)
  wrap_expr(_rbexpr.ewm_std(alpha, adjust, bias, min_periods))
end

#ewm_var(com: nil, span: nil, half_life: nil, alpha: nil, adjust: true, bias: false, min_periods: 1) ⇒ Expr

Exponentially-weighted moving variance.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3]})
df.select(Polars.col("a").ewm_var(com: 1))
# =>
# shape: (3, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 0.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 0.5      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 0.928571 │
# └──────────┘

Returns:



4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
# File 'lib/polars/expr.rb', line 4875

def ewm_var(
  com: nil,
  span: nil,
  half_life: nil,
  alpha: nil,
  adjust: true,
  bias: false,
  min_periods: 1
)
  alpha = _prepare_alpha(com, span, half_life, alpha)
  wrap_expr(_rbexpr.ewm_var(alpha, adjust, bias, min_periods))
end

#exclude(columns) ⇒ Expr

Exclude certain columns from a wildcard/regex selection.

You may also use regexes in the exclude list. They must start with ^ and end with $.

Examples:

df = Polars::DataFrame.new(
  {
    "aa" => [1, 2, 3],
    "ba" => ["a", "b", nil],
    "cc" => [nil, 2.5, 1.5]
  }
)
df.select(Polars.all.exclude("ba"))
# =>
# shape: (3, 2)
# ┌─────┬──────┐
# │ aa  ┆ cc   │
# │ --- ┆ ---  │
# │ i64 ┆ f64  │
# ╞═════╪══════╡
# │ 1   ┆ null │
# ├╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ 2   ┆ 2.5  │
# ├╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ 3   ┆ 1.5  │
# └─────┴──────┘

Parameters:

  • columns (Object)

    Column(s) to exclude from selection. This can be:

    • a column name, or multiple column names
    • a regular expression starting with ^ and ending with $
    • a dtype or multiple dtypes

Returns:



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/polars/expr.rb', line 374

def exclude(columns)
  if columns.is_a?(String)
    columns = [columns]
    return wrap_expr(_rbexpr.exclude(columns))
  elsif !columns.is_a?(Array)
    columns = [columns]
    return wrap_expr(_rbexpr.exclude_dtype(columns))
  end

  if !columns.all? { |a| a.is_a?(String) } || !columns.all? { |a| Utils.is_polars_dtype(a) }
    raise ArgumentError, "input should be all string or all DataType"
  end

  if columns[0].is_a?(String)
    wrap_expr(_rbexpr.exclude(columns))
  else
    wrap_expr(_rbexpr.exclude_dtype(columns))
  end
end

#expExpr

Compute the exponential, element-wise.

Examples:

df = Polars::DataFrame.new({"values" => [1.0, 2.0, 4.0]})
df.select(Polars.col("values").exp)
# =>
# shape: (3, 1)
# ┌──────────┐
# │ values   │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 2.718282 │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 7.389056 │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 54.59815 │
# └──────────┘

Returns:



294
295
296
# File 'lib/polars/expr.rb', line 294

def exp
  wrap_expr(_rbexpr.exp)
end

#explodeExpr

Explode a list or utf8 Series.

This means that every item is expanded to a new row.

Examples:

df = Polars::DataFrame.new({"b" => [[1, 2, 3], [4, 5, 6]]})
df.select(Polars.col("b").explode)
# =>
# shape: (6, 1)
# ┌─────┐
# │ b   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 1   │
# ├╌╌╌╌╌┤
# │ 2   │
# ├╌╌╌╌╌┤
# │ 3   │
# ├╌╌╌╌╌┤
# │ 4   │
# ├╌╌╌╌╌┤
# │ 5   │
# ├╌╌╌╌╌┤
# │ 6   │
# └─────┘

Returns:



2766
2767
2768
# File 'lib/polars/expr.rb', line 2766

def explode
  wrap_expr(_rbexpr.explode)
end

#extend_constant(value, n) ⇒ Expr

Extend the Series with given number of values.

Examples:

df = Polars::DataFrame.new({"values" => [1, 2, 3]})
df.select(Polars.col("values").extend_constant(99, 2))
# =>
# shape: (5, 1)
# ┌────────┐
# │ values │
# │ ---    │
# │ i64    │
# ╞════════╡
# │ 1      │
# ├╌╌╌╌╌╌╌╌┤
# │ 2      │
# ├╌╌╌╌╌╌╌╌┤
# │ 3      │
# ├╌╌╌╌╌╌╌╌┤
# │ 99     │
# ├╌╌╌╌╌╌╌╌┤
# │ 99     │
# └────────┘

Parameters:

  • value (Object)

    The value to extend the Series with. This value may be nil to fill with nulls.

  • n (Integer)

    The number of values to extend.

Returns:



4918
4919
4920
# File 'lib/polars/expr.rb', line 4918

def extend_constant(value, n)
  wrap_expr(_rbexpr.extend_constant(value, n))
end

#fill_nan(fill_value) ⇒ Expr

Fill floating point NaN value with a fill value.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1.0, nil, Float::NAN],
    "b" => [4.0, Float::NAN, 6]
  }
)
df.fill_nan("zero")
# =>
# shape: (3, 2)
# ┌──────┬──────┐
# │ a    ┆ b    │
# │ ---  ┆ ---  │
# │ str  ┆ str  │
# ╞══════╪══════╡
# │ 1.0  ┆ 4.0  │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ null ┆ zero │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ zero ┆ 6.0  │
# └──────┴──────┘

Returns:



1822
1823
1824
1825
# File 'lib/polars/expr.rb', line 1822

def fill_nan(fill_value)
  fill_value = Utils.expr_to_lit_or_expr(fill_value, str_to_lit: true)
  wrap_expr(_rbexpr.fill_nan(fill_value._rbexpr))
end

#fill_null(value = nil, strategy: nil, limit: nil) ⇒ Expr

Fill null values using the specified value or strategy.

To interpolate over null values see interpolate.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, nil],
    "b" => [4, nil, 6]
  }
)
df.fill_null(strategy: "zero")
# =>
# shape: (3, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 4   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2   ┆ 0   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 0   ┆ 6   │
# └─────┴─────┘
df.fill_null(99)
# =>
# shape: (3, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 4   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2   ┆ 99  │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 99  ┆ 6   │
# └─────┴─────┘
df.fill_null(strategy: "forward")
# =>
# shape: (3, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 4   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2   ┆ 4   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2   ┆ 6   │
# └─────┴─────┘

Parameters:

  • value (Object) (defaults to: nil)

    Value used to fill null values.

  • strategy (nil, "forward", "backward", "min", "max", "mean", "zero", "one") (defaults to: nil)

    Strategy used to fill null values.

  • limit (Integer) (defaults to: nil)

    Number of consecutive null values to fill when using the 'forward' or 'backward' strategy.

Returns:



1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
# File 'lib/polars/expr.rb', line 1780

def fill_null(value = nil, strategy: nil, limit: nil)
  if !value.nil? && !strategy.nil?
    raise ArgumentError, "cannot specify both 'value' and 'strategy'."
  elsif value.nil? && strategy.nil?
    raise ArgumentError, "must specify either a fill 'value' or 'strategy'"
  elsif ["forward", "backward"].include?(strategy) && !limit.nil?
    raise ArgumentError, "can only specify 'limit' when strategy is set to 'backward' or 'forward'"
  end

  if !value.nil?
    value = Utils.expr_to_lit_or_expr(value, str_to_lit: true)
    wrap_expr(_rbexpr.fill_null(value._rbexpr))
  else
    wrap_expr(_rbexpr.fill_null_with_strategy(strategy, limit))
  end
end

#filter(predicate) ⇒ Expr

Filter a single column.

Mostly useful in an aggregation context. If you want to filter on a DataFrame level, use LazyFrame#filter.

Examples:

df = Polars::DataFrame.new(
  {
    "group_col" => ["g1", "g1", "g2"],
    "b" => [1, 2, 3]
  }
)
(
  df.groupby("group_col").agg(
    [
      Polars.col("b").filter(Polars.col("b") < 2).sum.alias("lt"),
      Polars.col("b").filter(Polars.col("b") >= 2).sum.alias("gte")
    ]
  )
).sort("group_col")
# =>
# shape: (2, 3)
# ┌───────────┬──────┬─────┐
# │ group_col ┆ lt   ┆ gte │
# │ ---       ┆ ---  ┆ --- │
# │ str       ┆ i64  ┆ i64 │
# ╞═══════════╪══════╪═════╡
# │ g1        ┆ 1    ┆ 2   │
# ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┤
# │ g2        ┆ null ┆ 3   │
# └───────────┴──────┴─────┘

Parameters:

  • predicate (Expr)

    Boolean expression.

Returns:



2529
2530
2531
# File 'lib/polars/expr.rb', line 2529

def filter(predicate)
  wrap_expr(_rbexpr.filter(predicate._rbexpr))
end

#firstExpr

Get the first value.

Examples:

df = Polars::DataFrame.new({"a" => [1, 1, 2]})
df.select(Polars.col("a").first)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 1   │
# └─────┘

Returns:



2241
2242
2243
# File 'lib/polars/expr.rb', line 2241

def first
  wrap_expr(_rbexpr.first)
end

#flattenExpr

Explode a list or utf8 Series. This means that every item is expanded to a new row.

Alias for #explode.

Examples:

df = Polars::DataFrame.new({"foo" => ["hello", "world"]})
df.select(Polars.col("foo").flatten)
# =>
# shape: (10, 1)
# ┌─────┐
# │ foo │
# │ --- │
# │ str │
# ╞═════╡
# │ h   │
# ├╌╌╌╌╌┤
# │ e   │
# ├╌╌╌╌╌┤
# │ l   │
# ├╌╌╌╌╌┤
# │ l   │
# ├╌╌╌╌╌┤
# │ ... │
# ├╌╌╌╌╌┤
# │ o   │
# ├╌╌╌╌╌┤
# │ r   │
# ├╌╌╌╌╌┤
# │ l   │
# ├╌╌╌╌╌┤
# │ d   │
# └─────┘

Returns:



2734
2735
2736
# File 'lib/polars/expr.rb', line 2734

def flatten
  wrap_expr(_rbexpr.explode)
end

#floorExpr

Rounds down to the nearest integer value.

Only works on floating point Series.

Examples:

df = Polars::DataFrame.new({"a" => [0.3, 0.5, 1.0, 1.1]})
df.select(Polars.col("a").floor)
# =>
# shape: (4, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 0.0 │
# ├╌╌╌╌╌┤
# │ 0.0 │
# ├╌╌╌╌╌┤
# │ 1.0 │
# ├╌╌╌╌╌┤
# │ 1.0 │
# └─────┘

Returns:



1146
1147
1148
# File 'lib/polars/expr.rb', line 1146

def floor
  wrap_expr(_rbexpr.floor)
end

#floordiv(other) ⇒ Expr

Performs floor division.

Returns:



74
75
76
# File 'lib/polars/expr.rb', line 74

def floordiv(other)
  wrap_expr(_rbexpr.floordiv(_to_rbexpr(other)))
end

#forward_fill(limit: nil) ⇒ Expr

Fill missing values with the latest seen values.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, nil],
    "b" => [4, nil, 6]
  }
)
df.select(Polars.all.forward_fill)
# =>
# shape: (3, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 4   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2   ┆ 4   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2   ┆ 6   │
# └─────┴─────┘

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of consecutive null values to forward fill.

Returns:



1855
1856
1857
# File 'lib/polars/expr.rb', line 1855

def forward_fill(limit: nil)
  wrap_expr(_rbexpr.forward_fill(limit))
end

#head(n = 10) ⇒ Expr

Get the first n rows.

Examples:

df = Polars::DataFrame.new({"foo" => [1, 2, 3, 4, 5, 6, 7]})
df.head(3)
# =>
# shape: (3, 1)
# ┌─────┐
# │ foo │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 1   │
# ├╌╌╌╌╌┤
# │ 2   │
# ├╌╌╌╌╌┤
# │ 3   │
# └─────┘

Parameters:

  • n (Integer) (defaults to: 10)

    Number of rows to return.

Returns:



2817
2818
2819
# File 'lib/polars/expr.rb', line 2817

def head(n = 10)
  wrap_expr(_rbexpr.head(n))
end

#interpolate(method: "linear") ⇒ Expr

Fill nulls with linear interpolation over missing values.

Can also be used to regrid data to a new grid - see examples below.

Examples:

Fill nulls with linear interpolation

df = Polars::DataFrame.new(
  {
    "a" => [1, nil, 3],
    "b" => [1.0, Float::NAN, 3.0]
  }
)
df.select(Polars.all.interpolate)
# =>
# shape: (3, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ f64 │
# ╞═════╪═════╡
# │ 1   ┆ 1.0 │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2   ┆ NaN │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 3   ┆ 3.0 │
# └─────┴─────┘

Returns:



3155
3156
3157
# File 'lib/polars/expr.rb', line 3155

def interpolate(method: "linear")
  wrap_expr(_rbexpr.interpolate(method))
end

#is_between(start, _end, include_bounds: false) ⇒ Expr

Check if this expression is between start and end.

Examples:

df = Polars::DataFrame.new({"num" => [1, 2, 3, 4, 5]})
df.with_column(Polars.col("num").is_between(2, 4))
# =>
# shape: (5, 2)
# ┌─────┬────────────┐
# │ num ┆ is_between │
# │ --- ┆ ---        │
# │ i64 ┆ bool       │
# ╞═════╪════════════╡
# │ 1   ┆ false      │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 2   ┆ false      │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 3   ┆ true       │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 4   ┆ false      │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 5   ┆ false      │
# └─────┴────────────┘

Parameters:

  • start (Object)

    Lower bound as primitive type or datetime.

  • _end (Object)

    Upper bound as primitive type or datetime.

  • include_bounds (Boolean) (defaults to: false)

    False: Exclude both start and end (default). True: Include both start and end. (False, False): Exclude start and exclude end. (True, True): Include start and include end. (False, True): Exclude start and include end. (True, False): Include start and exclude end.

Returns:



2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
# File 'lib/polars/expr.rb', line 2998

def is_between(start, _end, include_bounds: false)
  if include_bounds == false || include_bounds == [false, false]
    ((self > start) & (self < _end)).alias("is_between")
  elsif include_bounds == true || include_bounds == [true, true]
    ((self >= start) & (self <= _end)).alias("is_between")
  elsif include_bounds == [false, true]
    ((self > start) & (self <= _end)).alias("is_between")
  elsif include_bounds == [true, false]
    ((self >= start) & (self < _end)).alias("is_between")
  else
    raise ArgumentError, "include_bounds should be a bool or [bool, bool]."
  end
end

#is_duplicatedExpr

Get mask of duplicated values.

Examples:

df = Polars::DataFrame.new({"a" => [1, 1, 2]})
df.select(Polars.col("a").is_duplicated)
# =>
# shape: (3, 1)
# ┌───────┐
# │ a     │
# │ ---   │
# │ bool  │
# ╞═══════╡
# │ true  │
# ├╌╌╌╌╌╌╌┤
# │ true  │
# ├╌╌╌╌╌╌╌┤
# │ false │
# └───────┘

Returns:



2415
2416
2417
# File 'lib/polars/expr.rb', line 2415

def is_duplicated
  wrap_expr(_rbexpr.is_duplicated)
end

#is_finiteExpr

Returns a boolean Series indicating which values are finite.

Examples:

df = Polars::DataFrame.new(
  {
    "A" => [1.0, 2],
    "B" => [3.0, Float::INFINITY]
  }
)
df.select(Polars.all.is_finite)
# =>
# shape: (2, 2)
# ┌──────┬───────┐
# │ A    ┆ B     │
# │ ---  ┆ ---   │
# │ bool ┆ bool  │
# ╞══════╪═══════╡
# │ true ┆ true  │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
# │ true ┆ false │
# └──────┴───────┘

Returns:



597
598
599
# File 'lib/polars/expr.rb', line 597

def is_finite
  wrap_expr(_rbexpr.is_finite)
end

#is_firstExpr

Get a mask of the first unique value.

Examples:

df = Polars::DataFrame.new(
  {
    "num" => [1, 2, 3, 1, 5]
  }
)
df.with_column(Polars.col("num").is_first.alias("is_first"))
# =>
# shape: (5, 2)
# ┌─────┬──────────┐
# │ num ┆ is_first │
# │ --- ┆ ---      │
# │ i64 ┆ bool     │
# ╞═════╪══════════╡
# │ 1   ┆ true     │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 2   ┆ true     │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 3   ┆ true     │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 1   ┆ false    │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 5   ┆ true     │
# └─────┴──────────┘

Returns:



2391
2392
2393
# File 'lib/polars/expr.rb', line 2391

def is_first
  wrap_expr(_rbexpr.is_first)
end

#is_in(other) ⇒ Expr

Check if elements of this expression are present in the other Series.

Examples:

df = Polars::DataFrame.new(
  {"sets" => [[1, 2, 3], [1, 2], [9, 10]], "optional_members" => [1, 2, 3]}
)
df.select([Polars.col("optional_members").is_in("sets").alias("contains")])
# =>
# shape: (3, 1)
# ┌──────────┐
# │ contains │
# │ ---      │
# │ bool     │
# ╞══════════╡
# │ true     │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ true     │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ false    │
# └──────────┘

Parameters:

  • other (Object)

    Series or sequence of primitive type.

Returns:



2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
# File 'lib/polars/expr.rb', line 2912

def is_in(other)
  if other.is_a?(Array)
    if other.length == 0
      other = Polars.lit(nil)
    else
      other = Polars.lit(Series.new(other))
    end
  else
    other = Utils.expr_to_lit_or_expr(other, str_to_lit: false)
  end
  wrap_expr(_rbexpr.is_in(other._rbexpr))
end

#is_infiniteExpr

Returns a boolean Series indicating which values are infinite.

Examples:

df = Polars::DataFrame.new(
  {
    "A" => [1.0, 2],
    "B" => [3.0, Float::INFINITY]
  }
)
df.select(Polars.all.is_infinite)
# =>
# shape: (2, 2)
# ┌───────┬───────┐
# │ A     ┆ B     │
# │ ---   ┆ ---   │
# │ bool  ┆ bool  │
# ╞═══════╪═══════╡
# │ false ┆ false │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
# │ false ┆ true  │
# └───────┴───────┘

Returns:



624
625
626
# File 'lib/polars/expr.rb', line 624

def is_infinite
  wrap_expr(_rbexpr.is_infinite)
end

#is_nanExpr

Note:

Floating point NaN (Not A Number) should not be confused with missing data represented as nil.

Returns a boolean Series indicating which values are NaN.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, nil, 1, 5],
    "b" => [1.0, 2.0, Float::NAN, 1.0, 5.0]
  }
)
df.with_column(Polars.col(Polars::Float64).is_nan.suffix("_isnan"))
# =>
# shape: (5, 3)
# ┌──────┬─────┬─────────┐
# │ a    ┆ b   ┆ b_isnan │
# │ ---  ┆ --- ┆ ---     │
# │ i64  ┆ f64 ┆ bool    │
# ╞══════╪═════╪═════════╡
# │ 1    ┆ 1.0 ┆ false   │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
# │ 2    ┆ 2.0 ┆ false   │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
# │ null ┆ NaN ┆ true    │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
# │ 1    ┆ 1.0 ┆ false   │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
# │ 5    ┆ 5.0 ┆ false   │
# └──────┴─────┴─────────┘

Returns:



661
662
663
# File 'lib/polars/expr.rb', line 661

def is_nan
  wrap_expr(_rbexpr.is_nan)
end

#is_notExpr

Negate a boolean expression.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [true, false, false],
    "b" => ["a", "b", nil]
  }
)
# =>
# shape: (3, 2)
# ┌───────┬──────┐
# │ a     ┆ b    │
# │ ---   ┆ ---  │
# │ bool  ┆ str  │
# ╞═══════╪══════╡
# │ true  ┆ a    │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ false ┆ b    │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
# │ false ┆ null │
# └───────┴──────┘
df.select(Polars.col("a").is_not)
# =>
# shape: (3, 1)
# ┌───────┐
# │ a     │
# │ ---   │
# │ bool  │
# ╞═══════╡
# │ false │
# ├╌╌╌╌╌╌╌┤
# │ true  │
# ├╌╌╌╌╌╌╌┤
# │ true  │
# └───────┘

Returns:



504
505
506
# File 'lib/polars/expr.rb', line 504

def is_not
  wrap_expr(_rbexpr.is_not)
end

#is_not_nanExpr

Note:

Floating point NaN (Not A Number) should not be confused with missing data represented as nil.

Returns a boolean Series indicating which values are not NaN.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, nil, 1, 5],
    "b" => [1.0, 2.0, Float::NAN, 1.0, 5.0]
  }
)
df.with_column(Polars.col(Polars::Float64).is_not_nan.suffix("_is_not_nan"))
# =>
# shape: (5, 3)
# ┌──────┬─────┬──────────────┐
# │ a    ┆ b   ┆ b_is_not_nan │
# │ ---  ┆ --- ┆ ---          │
# │ i64  ┆ f64 ┆ bool         │
# ╞══════╪═════╪══════════════╡
# │ 1    ┆ 1.0 ┆ true         │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 2    ┆ 2.0 ┆ true         │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ null ┆ NaN ┆ false        │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 1    ┆ 1.0 ┆ true         │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 5    ┆ 5.0 ┆ true         │
# └──────┴─────┴──────────────┘

Returns:



698
699
700
# File 'lib/polars/expr.rb', line 698

def is_not_nan
  wrap_expr(_rbexpr.is_not_nan)
end

#is_not_nullExpr

Returns a boolean Series indicating which values are not null.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, nil, 1, 5],
    "b" => [1.0, 2.0, Float::NAN, 1.0, 5.0]
  }
)
df.with_column(Polars.all.is_not_null.suffix("_not_null"))
# =>
# shape: (5, 4)
# ┌──────┬─────┬────────────┬────────────┐
# │ a    ┆ b   ┆ a_not_null ┆ b_not_null │
# │ ---  ┆ --- ┆ ---        ┆ ---        │
# │ i64  ┆ f64 ┆ bool       ┆ bool       │
# ╞══════╪═════╪════════════╪════════════╡
# │ 1    ┆ 1.0 ┆ true       ┆ true       │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 2    ┆ 2.0 ┆ true       ┆ true       │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ null ┆ NaN ┆ false      ┆ true       │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 1    ┆ 1.0 ┆ true       ┆ true       │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 5    ┆ 5.0 ┆ true       ┆ true       │
# └──────┴─────┴────────────┴────────────┘

Returns:



570
571
572
# File 'lib/polars/expr.rb', line 570

def is_not_null
  wrap_expr(_rbexpr.is_not_null)
end

#is_nullExpr

Returns a boolean Series indicating which values are null.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, nil, 1, 5],
    "b" => [1.0, 2.0, Float::NAN, 1.0, 5.0]
  }
)
df.with_column(Polars.all.is_null.suffix("_isnull"))
# =>
# shape: (5, 4)
# ┌──────┬─────┬──────────┬──────────┐
# │ a    ┆ b   ┆ a_isnull ┆ b_isnull │
# │ ---  ┆ --- ┆ ---      ┆ ---      │
# │ i64  ┆ f64 ┆ bool     ┆ bool     │
# ╞══════╪═════╪══════════╪══════════╡
# │ 1    ┆ 1.0 ┆ false    ┆ false    │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 2    ┆ 2.0 ┆ false    ┆ false    │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ null ┆ NaN ┆ true     ┆ false    │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 1    ┆ 1.0 ┆ false    ┆ false    │
# ├╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 5    ┆ 5.0 ┆ false    ┆ false    │
# └──────┴─────┴──────────┴──────────┘

Returns:



537
538
539
# File 'lib/polars/expr.rb', line 537

def is_null
  wrap_expr(_rbexpr.is_null)
end

#is_uniqueExpr

Get mask of unique values.

Examples:

df = Polars::DataFrame.new({"a" => [1, 1, 2]})
df.select(Polars.col("a").is_unique)
# =>
# shape: (3, 1)
# ┌───────┐
# │ a     │
# │ ---   │
# │ bool  │
# ╞═══════╡
# │ false │
# ├╌╌╌╌╌╌╌┤
# │ false │
# ├╌╌╌╌╌╌╌┤
# │ true  │
# └───────┘

Returns:



2359
2360
2361
# File 'lib/polars/expr.rb', line 2359

def is_unique
  wrap_expr(_rbexpr.is_unique)
end

#keep_nameExpr

Keep the original root name of the expression.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2],
    "b" => [3, 4]
  }
)
df.with_columns([(Polars.col("a") * 9).alias("c").keep_name])
# =>
# shape: (2, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 9   ┆ 3   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 18  ┆ 4   │
# └─────┴─────┘

Returns:



417
418
419
# File 'lib/polars/expr.rb', line 417

def keep_name
  wrap_expr(_rbexpr.keep_name)
end

#kurtosis(fisher: true, bias: true) ⇒ Expr

Compute the kurtosis (Fisher or Pearson) of a dataset.

Kurtosis is the fourth central moment divided by the square of the variance. If Fisher's definition is used, then 3.0 is subtracted from the result to give 0.0 for a normal distribution. If bias is False then the kurtosis is calculated using k statistics to eliminate bias coming from biased moment estimators

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 2, 1]})
df.select(Polars.col("a").kurtosis)
# =>
# shape: (1, 1)
# ┌───────────┐
# │ a         │
# │ ---       │
# │ f64       │
# ╞═══════════╡
# │ -1.153061 │
# └───────────┘

Parameters:

  • fisher (Boolean) (defaults to: true)

    If true, Fisher's definition is used (normal ==> 0.0). If false, Pearson's definition is used (normal ==> 3.0).

  • bias (Boolean) (defaults to: true)

    If false, the calculations are corrected for statistical bias.

Returns:



4254
4255
4256
# File 'lib/polars/expr.rb', line 4254

def kurtosis(fisher: true, bias: true)
  wrap_expr(_rbexpr.kurtosis(fisher, bias))
end

#lastExpr

Get the last value.

Examples:

df = Polars::DataFrame.new({"a" => [1, 1, 2]})
df.select(Polars.col("a").last)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 2   │
# └─────┘

Returns:



2261
2262
2263
# File 'lib/polars/expr.rb', line 2261

def last
  wrap_expr(_rbexpr.last)
end

#lenExpr

Count the number of values in this expression.

Alias for #count.

Examples:

df = Polars::DataFrame.new({"a" => [8, 9, 10], "b" => [nil, 4, 4]})
df.select(Polars.all.len)
# =>
# shape: (1, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ u32 ┆ u32 │
# ╞═════╪═════╡
# │ 3   ┆ 3   │
# └─────┴─────┘

Returns:



776
777
778
# File 'lib/polars/expr.rb', line 776

def len
  count
end

#limit(n = 10) ⇒ Expr

Get the first n rows.

Alias for #head.

Parameters:

  • n (Integer) (defaults to: 10)

    Number of rows to return.

Returns:



2856
2857
2858
# File 'lib/polars/expr.rb', line 2856

def limit(n = 10)
  head(n)
end

#listExpr

Aggregate to list.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 2, 3],
    "b" => [4, 5, 6]
  }
)
df.select(Polars.all.list)
# =>
# shape: (1, 2)
# ┌───────────┬───────────┐
# │ a         ┆ b         │
# │ ---       ┆ ---       │
# │ list[i64] ┆ list[i64] │
# ╞═══════════╪═══════════╡
# │ [1, 2, 3] ┆ [4, 5, 6] │
# └───────────┴───────────┘

Returns:



5164
5165
5166
# File 'lib/polars/expr.rb', line 5164

def list
  wrap_expr(_rbexpr.list)
end

#log(base = Math::E) ⇒ Expr

Compute the logarithm to a given base.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3]})
df.select(Polars.col("a").log(2))
# =>
# shape: (3, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 0.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.584963 │
# └──────────┘

Parameters:

  • base (Float) (defaults to: Math::E)

    Given base, defaults to e.

Returns:



5018
5019
5020
# File 'lib/polars/expr.rb', line 5018

def log(base = Math::E)
  wrap_expr(_rbexpr.log(base))
end

#log10Expr

Compute the base 10 logarithm of the input array, element-wise.

Examples:

df = Polars::DataFrame.new({"values" => [1.0, 2.0, 4.0]})
df.select(Polars.col("values").log10)
# =>
# shape: (3, 1)
# ┌─────────┐
# │ values  │
# │ ---     │
# │ f64     │
# ╞═════════╡
# │ 0.0     │
# ├╌╌╌╌╌╌╌╌╌┤
# │ 0.30103 │
# ├╌╌╌╌╌╌╌╌╌┤
# │ 0.60206 │
# └─────────┘

Returns:



270
271
272
# File 'lib/polars/expr.rb', line 270

def log10
  log(10)
end

#lower_boundExpr

Calculate the lower bound.

Returns a unit Series with the lowest value possible for the dtype of this expression.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 2, 1]})
df.select(Polars.col("a").lower_bound)
# =>
# shape: (1, 1)
# ┌──────────────────────┐
# │ a                    │
# │ ---                  │
# │ i64                  │
# ╞══════════════════════╡
# │ -9223372036854775808 │
# └──────────────────────┘

Returns:



4381
4382
4383
# File 'lib/polars/expr.rb', line 4381

def lower_bound
  wrap_expr(_rbexpr.lower_bound)
end

#map_alias(&f) ⇒ Expr

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

Examples:

df = Polars::DataFrame.new(
  {
    "A" => [1, 2],
    "B" => [3, 4]
  }
)
df.select(
  Polars.all.reverse.map_alias { |colName| colName + "_reverse" }
)
# =>
# shape: (2, 2)
# ┌───────────┬───────────┐
# │ A_reverse ┆ B_reverse │
# │ ---       ┆ ---       │
# │ i64       ┆ i64       │
# ╞═══════════╪═══════════╡
# │ 2         ┆ 4         │
# ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
# │ 1         ┆ 3         │
# └───────────┴───────────┘

Returns:



460
461
462
# File 'lib/polars/expr.rb', line 460

def map_alias(&f)
  Utils.wrap_expr(_rbexpr.map_alias(f))
end

#maxExpr

Get maximum value.

Examples:

df = Polars::DataFrame.new({"a" => [-1.0, Float::NAN, 1.0]})
df.select(Polars.col("a").max)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.0 │
# └─────┘

Returns:



1960
1961
1962
# File 'lib/polars/expr.rb', line 1960

def max
  wrap_expr(_rbexpr.max)
end

#meanExpr

Get mean value.

Examples:

df = Polars::DataFrame.new({"a" => [-1, 0, 1]})
df.select(Polars.col("a").mean)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 0.0 │
# └─────┘

Returns:



2064
2065
2066
# File 'lib/polars/expr.rb', line 2064

def mean
  wrap_expr(_rbexpr.mean)
end

#medianExpr

Get median value using linear interpolation.

Examples:

df = Polars::DataFrame.new({"a" => [-1, 0, 1]})
df.select(Polars.col("a").median)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 0.0 │
# └─────┘

Returns:



2084
2085
2086
# File 'lib/polars/expr.rb', line 2084

def median
  wrap_expr(_rbexpr.median)
end

#metaMetaExpr

Create an object namespace of all meta related expression methods.

Returns:



5229
5230
5231
# File 'lib/polars/expr.rb', line 5229

def meta
  MetaExpr.new(self)
end

#minExpr

Get minimum value.

Examples:

df = Polars::DataFrame.new({"a" => [-1.0, Float::NAN, 1.0]})
df.select(Polars.col("a").min)
# =>
# shape: (1, 1)
# ┌──────┐
# │ a    │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ -1.0 │
# └──────┘

Returns:



1980
1981
1982
# File 'lib/polars/expr.rb', line 1980

def min
  wrap_expr(_rbexpr.min)
end

#modeExpr

Compute the most occurring value(s).

Can return multiple Values.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [1, 1, 2, 3],
    "b" => [1, 1, 2, 2]
  }
)
df.select(Polars.all.mode)
# =>
# shape: (2, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 1   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 1   ┆ 2   │
# └─────┴─────┘

Returns:



1261
1262
1263
# File 'lib/polars/expr.rb', line 1261

def mode
  wrap_expr(_rbexpr.mode)
end

#n_uniqueExpr

Count unique values.

Examples:

df = Polars::DataFrame.new({"a" => [1, 1, 2]})
df.select(Polars.col("a").n_unique)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 2   │
# └─────┘

Returns:



2124
2125
2126
# File 'lib/polars/expr.rb', line 2124

def n_unique
  wrap_expr(_rbexpr.n_unique)
end

#nan_maxExpr

Get maximum value, but propagate/poison encountered NaN values.

Examples:

df = Polars::DataFrame.new({"a" => [0.0, Float::NAN]})
df.select(Polars.col("a").nan_max)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ NaN │
# └─────┘

Returns:



2000
2001
2002
# File 'lib/polars/expr.rb', line 2000

def nan_max
  wrap_expr(_rbexpr.nan_max)
end

#nan_minExpr

Get minimum value, but propagate/poison encountered NaN values.

Examples:

df = Polars::DataFrame.new({"a" => [0.0, Float::NAN]})
df.select(Polars.col("a").nan_min)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ NaN │
# └─────┘

Returns:



2020
2021
2022
# File 'lib/polars/expr.rb', line 2020

def nan_min
  wrap_expr(_rbexpr.nan_min)
end

#null_countExpr

Count null values.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [nil, 1, nil],
    "b" => [1, 2, 3]
  }
)
df.select(Polars.all.null_count)
# =>
# shape: (1, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ u32 ┆ u32 │
# ╞═════╪═════╡
# │ 2   ┆ 0   │
# └─────┴─────┘

Returns:



2149
2150
2151
# File 'lib/polars/expr.rb', line 2149

def null_count
  wrap_expr(_rbexpr.null_count)
end

#over(expr) ⇒ Expr

Apply window function over a subgroup.

This is similar to a groupby + aggregation + self join. Or similar to window functions in Postgres.

Examples:

df = Polars::DataFrame.new(
  {
    "groups" => ["g1", "g1", "g2"],
    "values" => [1, 2, 3]
  }
)
df.with_column(
  Polars.col("values").max.over("groups").alias("max_by_group")
)
# =>
# shape: (3, 3)
# ┌────────┬────────┬──────────────┐
# │ groups ┆ values ┆ max_by_group │
# │ ---    ┆ ---    ┆ ---          │
# │ str    ┆ i64    ┆ i64          │
# ╞════════╪════════╪══════════════╡
# │ g1     ┆ 1      ┆ 2            │
# ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ g1     ┆ 2      ┆ 2            │
# ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ g2     ┆ 3      ┆ 3            │
# └────────┴────────┴──────────────┘
df = Polars::DataFrame.new(
  {
    "groups" => [1, 1, 2, 2, 1, 2, 3, 3, 1],
    "values" => [1, 2, 3, 4, 5, 6, 7, 8, 8]
  }
)
df.lazy
  .select([Polars.col("groups").sum.over("groups")])
  .collect
# =>
# shape: (9, 1)
# ┌────────┐
# │ groups │
# │ ---    │
# │ i64    │
# ╞════════╡
# │ 4      │
# ├╌╌╌╌╌╌╌╌┤
# │ 4      │
# ├╌╌╌╌╌╌╌╌┤
# │ 6      │
# ├╌╌╌╌╌╌╌╌┤
# │ 6      │
# ├╌╌╌╌╌╌╌╌┤
# │ ...    │
# ├╌╌╌╌╌╌╌╌┤
# │ 6      │
# ├╌╌╌╌╌╌╌╌┤
# │ 6      │
# ├╌╌╌╌╌╌╌╌┤
# │ 6      │
# ├╌╌╌╌╌╌╌╌┤
# │ 4      │
# └────────┘

Parameters:

  • expr (Object)

    Column(s) to group by.

Returns:



2334
2335
2336
2337
# File 'lib/polars/expr.rb', line 2334

def over(expr)
  rbexprs = Utils.selection_to_rbexpr_list(expr)
  wrap_expr(_rbexpr.over(rbexprs))
end

#pct_change(n: 1) ⇒ Expr

Computes percentage change between values.

Percentage change (as fraction) between current element and most-recent non-null element at least n period(s) before the current element.

Computes the change from the previous row by default.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [10, 11, 12, nil, 12]
  }
)
df.with_column(Polars.col("a").pct_change.alias("pct_change"))
# =>
# shape: (5, 2)
# ┌──────┬────────────┐
# │ a    ┆ pct_change │
# │ ---  ┆ ---        │
# │ i64  ┆ f64        │
# ╞══════╪════════════╡
# │ 10   ┆ null       │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 11   ┆ 0.1        │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 12   ┆ 0.090909   │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ null ┆ 0.0        │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 12   ┆ 0.0        │
# └──────┴────────────┘

Parameters:

  • n (Integer) (defaults to: 1)

    Periods to shift for forming percent change.

Returns:



4193
4194
4195
# File 'lib/polars/expr.rb', line 4193

def pct_change(n: 1)
  wrap_expr(_rbexpr.pct_change(n))
end

#pow(exponent) ⇒ Expr

Raise expression to the power of exponent.

Examples:

df = Polars::DataFrame.new({"foo" => [1, 2, 3, 4]})
df.select(Polars.col("foo").pow(3))
# =>
# shape: (4, 1)
# ┌──────┐
# │ foo  │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ 1.0  │
# ├╌╌╌╌╌╌┤
# │ 8.0  │
# ├╌╌╌╌╌╌┤
# │ 27.0 │
# ├╌╌╌╌╌╌┤
# │ 64.0 │
# └──────┘

Returns:



2882
2883
2884
2885
# File 'lib/polars/expr.rb', line 2882

def pow(exponent)
  exponent = Utils.expr_to_lit_or_expr(exponent)
  wrap_expr(_rbexpr.pow(exponent._rbexpr))
end

#prefix(prefix) ⇒ Expr

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

Returns:



424
425
426
# File 'lib/polars/expr.rb', line 424

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

#productExpr

Compute the product of an expression.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3]})
df.select(Polars.col("a").product)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 6   │
# └─────┘

Returns:



2104
2105
2106
# File 'lib/polars/expr.rb', line 2104

def product
  wrap_expr(_rbexpr.product)
end

#quantile(quantile, interpolation: "nearest") ⇒ Expr

Get quantile value.

Examples:

df = Polars::DataFrame.new({"a" => [0, 1, 2, 3, 4, 5]})
df.select(Polars.col("a").quantile(0.3))
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.0 │
# └─────┘
df.select(Polars.col("a").quantile(0.3, interpolation: "higher"))
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 2.0 │
# └─────┘
df.select(Polars.col("a").quantile(0.3, interpolation: "lower"))
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.0 │
# └─────┘
df.select(Polars.col("a").quantile(0.3, interpolation: "midpoint"))
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.5 │
# └─────┘
df.select(Polars.col("a").quantile(0.3, interpolation: "linear"))
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.5 │
# └─────┘

Parameters:

  • quantile (Float)

    Quantile between 0.0 and 1.0.

  • interpolation ("nearest", "higher", "lower", "midpoint", "linear") (defaults to: "nearest")

    Interpolation method.

Returns:



2488
2489
2490
2491
# File 'lib/polars/expr.rb', line 2488

def quantile(quantile, interpolation: "nearest")
  quantile = Utils.expr_to_lit_or_expr(quantile, str_to_lit: false)
  wrap_expr(_rbexpr.quantile(quantile._rbexpr, interpolation))
end

#rank(method: "average", reverse: false) ⇒ Expr

Assign ranks to data, dealing with ties appropriately.

Examples:

The 'average' method:

df = Polars::DataFrame.new({"a" => [3, 6, 1, 1, 6]})
df.select(Polars.col("a").rank)
# =>
# shape: (5, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f32 │
# ╞═════╡
# │ 3.0 │
# ├╌╌╌╌╌┤
# │ 4.5 │
# ├╌╌╌╌╌┤
# │ 1.5 │
# ├╌╌╌╌╌┤
# │ 1.5 │
# ├╌╌╌╌╌┤
# │ 4.5 │
# └─────┘

The 'ordinal' method:

df = Polars::DataFrame.new({"a" => [3, 6, 1, 1, 6]})
df.select(Polars.col("a").rank(method: "ordinal"))
# =>
# shape: (5, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 3   │
# ├╌╌╌╌╌┤
# │ 4   │
# ├╌╌╌╌╌┤
# │ 1   │
# ├╌╌╌╌╌┤
# │ 2   │
# ├╌╌╌╌╌┤
# │ 5   │
# └─────┘

Parameters:

  • method ("average", "min", "max", "dense", "ordinal", "random") (defaults to: "average")

    The method used to assign ranks to tied elements. The following methods are available:

    • 'average' : The average of the ranks that would have been assigned to all the tied values is assigned to each value.
    • 'min' : The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as "competition" ranking.)
    • 'max' : The maximum of the ranks that would have been assigned to all the tied values is assigned to each value.
    • 'dense' : Like 'min', but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements.
    • 'ordinal' : All values are given a distinct rank, corresponding to the order that the values occur in the Series.
    • 'random' : Like 'ordinal', but the rank for ties is not dependent on the order that the values occur in the Series.
  • reverse (Boolean) (defaults to: false)

    Reverse the operation.

Returns:



4120
4121
4122
# File 'lib/polars/expr.rb', line 4120

def rank(method: "average", reverse: false)
  wrap_expr(_rbexpr.rank(method, reverse))
end

#rechunkExpr

Create a single chunk of memory for this Series.

Examples:

Create a Series with 3 nulls, append column a then rechunk

df = Polars::DataFrame.new({"a" => [1, 1, 2]})
df.select(Polars.repeat(nil, 3).append(Polars.col("a")).rechunk)
# =>
# shape: (6, 1)
# ┌─────────┐
# │ literal │
# │ ---     │
# │ i64     │
# ╞═════════╡
# │ null    │
# ├╌╌╌╌╌╌╌╌╌┤
# │ null    │
# ├╌╌╌╌╌╌╌╌╌┤
# │ null    │
# ├╌╌╌╌╌╌╌╌╌┤
# │ 1       │
# ├╌╌╌╌╌╌╌╌╌┤
# │ 1       │
# ├╌╌╌╌╌╌╌╌╌┤
# │ 2       │
# └─────────┘

Returns:



880
881
882
# File 'lib/polars/expr.rb', line 880

def rechunk
  wrap_expr(_rbexpr.rechunk)
end

#reinterpret(signed: false) ⇒ Expr

Reinterpret the underlying bits as a signed/unsigned integer.

This operation is only allowed for 64bit integers. For lower bits integers, you can safely use that cast operation.

Examples:

s = Polars::Series.new("a", [1, 1, 2], dtype: :u64)
df = Polars::DataFrame.new([s])
df.select(
  [
    Polars.col("a").reinterpret(signed: true).alias("reinterpreted"),
    Polars.col("a").alias("original")
  ]
)
# =>
# shape: (3, 2)
# ┌───────────────┬──────────┐
# │ reinterpreted ┆ original │
# │ ---           ┆ ---      │
# │ i64           ┆ u64      │
# ╞═══════════════╪══════════╡
# │ 1             ┆ 1        │
# ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 1             ┆ 1        │
# ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 2             ┆ 2        │
# └───────────────┴──────────┘

Parameters:

  • signed (Boolean) (defaults to: false)

    If true, reinterpret as :i64. Otherwise, reinterpret as :u64.

Returns:



3088
3089
3090
# File 'lib/polars/expr.rb', line 3088

def reinterpret(signed: false)
  wrap_expr(_rbexpr.reinterpret(signed))
end

#repeat_by(by) ⇒ Expr

Repeat the elements in this Series as specified in the given expression.

The repeated elements are expanded into a List.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => ["x", "y", "z"],
    "n" => [1, 2, 3]
  }
)
df.select(Polars.col("a").repeat_by("n"))
# =>
# shape: (3, 1)
# ┌─────────────────┐
# │ a               │
# │ ---             │
# │ list[str]       │
# ╞═════════════════╡
# │ ["x"]           │
# ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ ["y", "y"]      │
# ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ ["z", "z", "z"] │
# └─────────────────┘

Parameters:

  • by (Object)

    Numeric column that determines how often the values will be repeated. The column will be coerced to UInt32. Give this dtype to make the coercion a no-op.

Returns:



2957
2958
2959
2960
# File 'lib/polars/expr.rb', line 2957

def repeat_by(by)
  by = Utils.expr_to_lit_or_expr(by, str_to_lit: false)
  wrap_expr(_rbexpr.repeat_by(by._rbexpr))
end

#reshape(dims) ⇒ Expr

Reshape this Expr to a flat Series or a Series of Lists.

Examples:

df = Polars::DataFrame.new({"foo" => [1, 2, 3, 4, 5, 6, 7, 8, 9]})
df.select(Polars.col("foo").reshape([3, 3]))
# =>
# shape: (3, 1)
# ┌───────────┐
# │ foo       │
# │ ---       │
# │ list[i64] │
# ╞═══════════╡
# │ [1, 2, 3] │
# ├╌╌╌╌╌╌╌╌╌╌╌┤
# │ [4, 5, 6] │
# ├╌╌╌╌╌╌╌╌╌╌╌┤
# │ [7, 8, 9] │
# └───────────┘

Parameters:

  • dims (Array)

    Tuple of the dimension sizes. If a -1 is used in any of the dimensions, that dimension is inferred.

Returns:



4700
4701
4702
# File 'lib/polars/expr.rb', line 4700

def reshape(dims)
  wrap_expr(_rbexpr.reshape(dims))
end

#reverseExpr

Reverse the selection.

Returns:



1894
1895
1896
# File 'lib/polars/expr.rb', line 1894

def reverse
  wrap_expr(_rbexpr.reverse)
end

#rolling_max(window_size, weights: nil, min_periods: nil, center: false, by: nil, closed: "left") ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using groupby_rolling this method can cache the window size computation.

Apply a rolling max (moving max) over the values in this array.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

df = Polars::DataFrame.new({"A" => [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]})
df.select(
  [
    Polars.col("A").rolling_max(2)
  ]
)
# =>
# shape: (6, 1)
# ┌──────┐
# │ A    │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ 2.0  │
# ├╌╌╌╌╌╌┤
# │ 3.0  │
# ├╌╌╌╌╌╌┤
# │ 4.0  │
# ├╌╌╌╌╌╌┤
# │ 5.0  │
# ├╌╌╌╌╌╌┤
# │ 6.0  │
# └──────┘

Parameters:

  • window_size (Integer)

    The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1mo (1 calendar month)
    • 1y (1 calendar year)
    • 1i (1 index count)

    If a timedelta or the dynamic string language is used, the by and closed arguments must also be set.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • by (String) (defaults to: nil)

    If the window_size is temporal for instance "5h" or "3s, you must set the column that will be used to determine the windows. This column must be of dtype {Date, Datetime}

  • closed ("left", "right", "both", "none") (defaults to: "left")

    Define whether the temporal window interval is closed or not.

Returns:



3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
# File 'lib/polars/expr.rb', line 3329

def rolling_max(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false,
  by: nil,
  closed: "left"
)
  window_size, min_periods = _prepare_rolling_window_args(
    window_size, min_periods
  )
  wrap_expr(
    _rbexpr.rolling_max(
      window_size, weights, min_periods, center, by, closed
    )
  )
end

#rolling_mean(window_size, weights: nil, min_periods: nil, center: false, by: nil, closed: "left") ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using groupby_rolling this method can cache the window size computation.

Apply a rolling mean (moving mean) over the values in this array.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

df = Polars::DataFrame.new({"A" => [1.0, 8.0, 6.0, 2.0, 16.0, 10.0]})
df.select(
  [
    Polars.col("A").rolling_mean(2)
  ]
)
# =>
# shape: (6, 1)
# ┌──────┐
# │ A    │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ 4.5  │
# ├╌╌╌╌╌╌┤
# │ 7.0  │
# ├╌╌╌╌╌╌┤
# │ 4.0  │
# ├╌╌╌╌╌╌┤
# │ 9.0  │
# ├╌╌╌╌╌╌┤
# │ 13.0 │
# └──────┘

Parameters:

  • window_size (Integer)

    The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1mo (1 calendar month)
    • 1y (1 calendar year)
    • 1i (1 index count)

    If a timedelta or the dynamic string language is used, the by and closed arguments must also be set.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • by (String) (defaults to: nil)

    If the window_size is temporal for instance "5h" or "3s, you must set the column that will be used to determine the windows. This column must be of dtype {Date, Datetime}

  • closed ("left", "right", "both", "none") (defaults to: "left")

    Define whether the temporal window interval is closed or not.

Returns:



3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
# File 'lib/polars/expr.rb', line 3423

def rolling_mean(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false,
  by: nil,
  closed: "left"
)
  window_size, min_periods = _prepare_rolling_window_args(
    window_size, min_periods
  )
  wrap_expr(
    _rbexpr.rolling_mean(
      window_size, weights, min_periods, center, by, closed
    )
  )
end

#rolling_median(window_size, weights: nil, min_periods: nil, center: false, by: nil, closed: "left") ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using groupby_rolling this method can cache the window size computation.

Compute a rolling median.

Examples:

df = Polars::DataFrame.new({"A" => [1.0, 2.0, 3.0, 4.0, 6.0, 8.0]})
df.select(
  [
    Polars.col("A").rolling_median(3)
  ]
)
# =>
# shape: (6, 1)
# ┌──────┐
# │ A    │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ null │
# ├╌╌╌╌╌╌┤
# │ 2.0  │
# ├╌╌╌╌╌╌┤
# │ 3.0  │
# ├╌╌╌╌╌╌┤
# │ 4.0  │
# ├╌╌╌╌╌╌┤
# │ 6.0  │
# └──────┘

Parameters:

  • window_size (Integer)

    The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1mo (1 calendar month)
    • 1y (1 calendar year)
    • 1i (1 index count)

    If a timedelta or the dynamic string language is used, the by and closed arguments must also be set.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • by (String) (defaults to: nil)

    If the window_size is temporal for instance "5h" or "3s, you must set the column that will be used to determine the windows. This column must be of dtype {Date, Datetime}

  • closed ("left", "right", "both", "none") (defaults to: "left")

    Define whether the temporal window interval is closed or not.

Returns:



3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
# File 'lib/polars/expr.rb', line 3795

def rolling_median(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false,
  by: nil,
  closed: "left"
)
  window_size, min_periods = _prepare_rolling_window_args(
    window_size, min_periods
  )
  wrap_expr(
    _rbexpr.rolling_median(
      window_size, weights, min_periods, center, by, closed
    )
  )
end

#rolling_min(window_size, weights: nil, min_periods: nil, center: false, by: nil, closed: "left") ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using groupby_rolling this method can cache the window size computation.

Apply a rolling min (moving min) over the values in this array.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

df = Polars::DataFrame.new({"A" => [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]})
df.select(
  [
    Polars.col("A").rolling_min(2)
  ]
)
# =>
# shape: (6, 1)
# ┌──────┐
# │ A    │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ 1.0  │
# ├╌╌╌╌╌╌┤
# │ 2.0  │
# ├╌╌╌╌╌╌┤
# │ 3.0  │
# ├╌╌╌╌╌╌┤
# │ 4.0  │
# ├╌╌╌╌╌╌┤
# │ 5.0  │
# └──────┘

Parameters:

  • window_size (Integer)

    The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1mo (1 calendar month)
    • 1y (1 calendar year)
    • 1i (1 index count)

    If a timedelta or the dynamic string language is used, the by and closed arguments must also be set.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • by (String) (defaults to: nil)

    If the window_size is temporal for instance "5h" or "3s, you must set the column that will be used to determine the windows. This column must be of dtype {Date, Datetime}

  • closed ("left", "right", "both", "none") (defaults to: "left")

    Define whether the temporal window interval is closed or not.

Returns:



3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
# File 'lib/polars/expr.rb', line 3235

def rolling_min(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false,
  by: nil,
  closed: "left"
)
  window_size, min_periods = _prepare_rolling_window_args(
    window_size, min_periods
  )
  wrap_expr(
    _rbexpr.rolling_min(
      window_size, weights, min_periods, center, by, closed
    )
  )
end

#rolling_quantile(quantile, interpolation: "nearest", window_size: 2, weights: nil, min_periods: nil, center: false, by: nil, closed: "left") ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using groupby_rolling this method can cache the window size computation.

Compute a rolling quantile.

Examples:

df = Polars::DataFrame.new({"A" => [1.0, 2.0, 3.0, 4.0, 6.0, 8.0]})
df.select(
  [
    Polars.col("A").rolling_quantile(0.33, window_size: 3)
  ]
)
# =>
# shape: (6, 1)
# ┌──────┐
# │ A    │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ null │
# ├╌╌╌╌╌╌┤
# │ 1.0  │
# ├╌╌╌╌╌╌┤
# │ 2.0  │
# ├╌╌╌╌╌╌┤
# │ 3.0  │
# ├╌╌╌╌╌╌┤
# │ 4.0  │
# └──────┘

Parameters:

  • quantile (Float)

    Quantile between 0.0 and 1.0.

  • interpolation ("nearest", "higher", "lower", "midpoint", "linear") (defaults to: "nearest")

    Interpolation method.

  • window_size (Integer) (defaults to: 2)

    The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1mo (1 calendar month)
    • 1y (1 calendar year)
    • 1i (1 index count)

    If a timedelta or the dynamic string language is used, the by and closed arguments must also be set.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • by (String) (defaults to: nil)

    If the window_size is temporal for instance "5h" or "3s, you must set the column that will be used to determine the windows. This column must be of dtype {Date, Datetime}

  • closed ("left", "right", "both", "none") (defaults to: "left")

    Define whether the temporal window interval is closed or not.

Returns:



3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
# File 'lib/polars/expr.rb', line 3889

def rolling_quantile(
  quantile,
  interpolation: "nearest",
  window_size: 2,
  weights: nil,
  min_periods: nil,
  center: false,
  by: nil,
  closed: "left"
)
  window_size, min_periods = _prepare_rolling_window_args(
    window_size, min_periods
  )
  wrap_expr(
    _rbexpr.rolling_quantile(
      quantile, interpolation, window_size, weights, min_periods, center, by, closed
    )
  )
end

#rolling_skew(window_size, bias: true) ⇒ Expr

Compute a rolling skew.

Parameters:

  • window_size (Integer)

    Integer size of the rolling window.

  • bias (Boolean) (defaults to: true)

    If false, the calculations are corrected for statistical bias.

Returns:



3985
3986
3987
# File 'lib/polars/expr.rb', line 3985

def rolling_skew(window_size, bias: true)
  wrap_expr(_rbexpr.rolling_skew(window_size, bias))
end

#rolling_std(window_size, weights: nil, min_periods: nil, center: false, by: nil, closed: "left") ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using groupby_rolling this method can cache the window size computation.

Compute a rolling standard deviation.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

df = Polars::DataFrame.new({"A" => [1.0, 2.0, 3.0, 4.0, 6.0, 8.0]})
df.select(
  [
    Polars.col("A").rolling_std(3)
  ]
)
# =>
# shape: (6, 1)
# ┌──────────┐
# │ A        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ null     │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ null     │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.527525 │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 2.0      │
# └──────────┘

Parameters:

  • window_size (Integer)

    The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1mo (1 calendar month)
    • 1y (1 calendar year)
    • 1i (1 index count)

    If a timedelta or the dynamic string language is used, the by and closed arguments must also be set.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • by (String) (defaults to: nil)

    If the window_size is temporal for instance "5h" or "3s, you must set the column that will be used to determine the windows. This column must be of dtype {Date, Datetime}

  • closed ("left", "right", "both", "none") (defaults to: "left")

    Define whether the temporal window interval is closed or not.

Returns:



3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
# File 'lib/polars/expr.rb', line 3611

def rolling_std(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false,
  by: nil,
  closed: "left"
)
  window_size, min_periods = _prepare_rolling_window_args(
    window_size, min_periods
  )
  wrap_expr(
    _rbexpr.rolling_std(
      window_size, weights, min_periods, center, by, closed
    )
  )
end

#rolling_sum(window_size, weights: nil, min_periods: nil, center: false, by: nil, closed: "left") ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using groupby_rolling this method can cache the window size computation.

Apply a rolling sum (moving sum) over the values in this array.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

df = Polars::DataFrame.new({"A" => [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]})
df.select(
  [
    Polars.col("A").rolling_sum(2)
  ]
)
# =>
# shape: (6, 1)
# ┌──────┐
# │ A    │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ 3.0  │
# ├╌╌╌╌╌╌┤
# │ 5.0  │
# ├╌╌╌╌╌╌┤
# │ 7.0  │
# ├╌╌╌╌╌╌┤
# │ 9.0  │
# ├╌╌╌╌╌╌┤
# │ 11.0 │
# └──────┘

Parameters:

  • window_size (Integer)

    The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1mo (1 calendar month)
    • 1y (1 calendar year)
    • 1i (1 index count)

    If a timedelta or the dynamic string language is used, the by and closed arguments must also be set.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • by (String) (defaults to: nil)

    If the window_size is temporal for instance "5h" or "3s, you must set the column that will be used to determine the windows. This column must be of dtype {Date, Datetime}

  • closed ("left", "right", "both", "none") (defaults to: "left")

    Define whether the temporal window interval is closed or not.

Returns:



3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
# File 'lib/polars/expr.rb', line 3517

def rolling_sum(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false,
  by: nil,
  closed: "left"
)
  window_size, min_periods = _prepare_rolling_window_args(
    window_size, min_periods
  )
  wrap_expr(
    _rbexpr.rolling_sum(
      window_size, weights, min_periods, center, by, closed
    )
  )
end

#rolling_var(window_size, weights: nil, min_periods: nil, center: false, by: nil, closed: "left") ⇒ Expr

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using groupby_rolling this method can cache the window size computation.

Compute a rolling variance.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

df = Polars::DataFrame.new({"A" => [1.0, 2.0, 3.0, 4.0, 6.0, 8.0]})
df.select(
  [
    Polars.col("A").rolling_var(3)
  ]
)
# =>
# shape: (6, 1)
# ┌──────────┐
# │ A        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ null     │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ null     │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 2.333333 │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 4.0      │
# └──────────┘

Parameters:

  • window_size (Integer)

    The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1mo (1 calendar month)
    • 1y (1 calendar year)
    • 1i (1 index count)

    If a timedelta or the dynamic string language is used, the by and closed arguments must also be set.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • by (String) (defaults to: nil)

    If the window_size is temporal for instance "5h" or "3s, you must set the column that will be used to determine the windows. This column must be of dtype {Date, Datetime}

  • closed ("left", "right", "both", "none") (defaults to: "left")

    Define whether the temporal window interval is closed or not.

Returns:



3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
# File 'lib/polars/expr.rb', line 3705

def rolling_var(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false,
  by: nil,
  closed: "left"
)
  window_size, min_periods = _prepare_rolling_window_args(
    window_size, min_periods
  )
  wrap_expr(
    _rbexpr.rolling_var(
      window_size, weights, min_periods, center, by, closed
    )
  )
end

#round(decimals = 0) ⇒ Expr

Round underlying floating point data by decimals digits.

Examples:

df = Polars::DataFrame.new({"a" => [0.33, 0.52, 1.02, 1.17]})
df.select(Polars.col("a").round(1))
# =>
# shape: (4, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 0.3 │
# ├╌╌╌╌╌┤
# │ 0.5 │
# ├╌╌╌╌╌┤
# │ 1.0 │
# ├╌╌╌╌╌┤
# │ 1.2 │
# └─────┘

Parameters:

  • decimals (Integer) (defaults to: 0)

    Number of decimals to round by.

Returns:



1203
1204
1205
# File 'lib/polars/expr.rb', line 1203

def round(decimals = 0)
  wrap_expr(_rbexpr.round(decimals))
end

#sample(frac: nil, with_replacement: true, shuffle: false, seed: nil, n: nil) ⇒ Expr

Sample from this expression.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3]})
df.select(Polars.col("a").sample(frac: 1.0, with_replacement: true, seed: 1))
# =>
# shape: (3, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 3   │
# ├╌╌╌╌╌┤
# │ 1   │
# ├╌╌╌╌╌┤
# │ 1   │
# └─────┘

Parameters:

  • frac (Float) (defaults to: nil)

    Fraction of items to return. Cannot be used with n.

  • with_replacement (Boolean) (defaults to: true)

    Allow values to be sampled more than once.

  • shuffle (Boolean) (defaults to: false)

    Shuffle the order of sampled data points.

  • seed (Integer) (defaults to: nil)

    Seed for the random number generator. If set to None (default), a random seed is used.

  • n (Integer) (defaults to: nil)

    Number of items to return. Cannot be used with frac.

Returns:



4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
# File 'lib/polars/expr.rb', line 4767

def sample(
  frac: nil,
  with_replacement: true,
  shuffle: false,
  seed: nil,
  n: nil
)
  if !n.nil? && !frac.nil?
    raise ArgumentError, "cannot specify both `n` and `frac`"
  end

  if !n.nil? && frac.nil?
    return wrap_expr(_rbexpr.sample_n(n, with_replacement, shuffle, seed))
  end

  if frac.nil?
    frac = 1.0
  end
  wrap_expr(
    _rbexpr.sample_frac(frac, with_replacement, shuffle, seed)
  )
end

#search_sorted(element) ⇒ Expr

Find indices where elements should be inserted to maintain order.

Examples:

df = Polars::DataFrame.new(
  {
    "values" => [1, 2, 3, 5]
  }
)
df.select(
  [
    Polars.col("values").search_sorted(0).alias("zero"),
    Polars.col("values").search_sorted(3).alias("three"),
    Polars.col("values").search_sorted(6).alias("six")
  ]
)
# =>
# shape: (1, 3)
# ┌──────┬───────┬─────┐
# │ zero ┆ three ┆ six │
# │ ---  ┆ ---   ┆ --- │
# │ u32  ┆ u32   ┆ u32 │
# ╞══════╪═══════╪═════╡
# │ 0    ┆ 2     ┆ 4   │
# └──────┴───────┴─────┘

Parameters:

  • element (Object)

    Expression or scalar value.

Returns:



1546
1547
1548
1549
# File 'lib/polars/expr.rb', line 1546

def search_sorted(element)
  element = Utils.expr_to_lit_or_expr(element, str_to_lit: false)
  wrap_expr(_rbexpr.search_sorted(element._rbexpr))
end

#shift(periods = 1) ⇒ Expr

Shift the values by a given period.

Examples:

df = Polars::DataFrame.new({"foo" => [1, 2, 3, 4]})
df.select(Polars.col("foo").shift(1))
# =>
# shape: (4, 1)
# ┌──────┐
# │ foo  │
# │ ---  │
# │ i64  │
# ╞══════╡
# │ null │
# ├╌╌╌╌╌╌┤
# │ 1    │
# ├╌╌╌╌╌╌┤
# │ 2    │
# ├╌╌╌╌╌╌┤
# │ 3    │
# └──────┘

Parameters:

  • periods (Integer) (defaults to: 1)

    Number of places to shift (may be negative).

Returns:



1677
1678
1679
# File 'lib/polars/expr.rb', line 1677

def shift(periods = 1)
  wrap_expr(_rbexpr.shift(periods))
end

#shift_and_fill(periods, fill_value) ⇒ Expr

Shift the values by a given period and fill the resulting null values.

Examples:

df = Polars::DataFrame.new({"foo" => [1, 2, 3, 4]})
df.select(Polars.col("foo").shift_and_fill(1, "a"))
# =>
# shape: (4, 1)
# ┌─────┐
# │ foo │
# │ --- │
# │ str │
# ╞═════╡
# │ a   │
# ├╌╌╌╌╌┤
# │ 1   │
# ├╌╌╌╌╌┤
# │ 2   │
# ├╌╌╌╌╌┤
# │ 3   │
# └─────┘

Parameters:

  • periods (Integer)

    Number of places to shift (may be negative).

  • fill_value (Object)

    Fill nil values with the result of this expression.

Returns:



1708
1709
1710
1711
# File 'lib/polars/expr.rb', line 1708

def shift_and_fill(periods, fill_value)
  fill_value = Utils.expr_to_lit_or_expr(fill_value, str_to_lit: true)
  wrap_expr(_rbexpr.shift_and_fill(periods, fill_value._rbexpr))
end

#shrink_dtypeExpr

Shrink numeric columns to the minimal required datatype.

Shrink to the dtype needed to fit the extrema of this Series. This can be used to reduce memory pressure.

Examples:

Polars::DataFrame.new(
  {
    "a" => [1, 2, 3],
    "b" => [1, 2, 2 << 32],
    "c" => [-1, 2, 1 << 30],
    "d" => [-112, 2, 112],
    "e" => [-112, 2, 129],
    "f" => ["a", "b", "c"],
    "g" => [0.1, 1.32, 0.12],
    "h" => [true, nil, false]
  }
).select(Polars.all.shrink_dtype)
# =>
# shape: (3, 8)
# ┌─────┬────────────┬────────────┬──────┬──────┬─────┬──────┬───────┐
# │ a   ┆ b          ┆ c          ┆ d    ┆ e    ┆ f   ┆ g    ┆ h     │
# │ --- ┆ ---        ┆ ---        ┆ ---  ┆ ---  ┆ --- ┆ ---  ┆ ---   │
# │ i8  ┆ i64        ┆ i32        ┆ i8   ┆ i16  ┆ str ┆ f32  ┆ bool  │
# ╞═════╪════════════╪════════════╪══════╪══════╪═════╪══════╪═══════╡
# │ 1   ┆ 1          ┆ -1         ┆ -112 ┆ -112 ┆ a   ┆ 0.1  ┆ true  │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
# │ 2   ┆ 2          ┆ 2          ┆ 2    ┆ 2    ┆ b   ┆ 1.32 ┆ null  │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
# │ 3   ┆ 8589934592 ┆ 1073741824 ┆ 112  ┆ 129  ┆ c   ┆ 0.12 ┆ false │
# └─────┴────────────┴────────────┴──────┴──────┴─────┴──────┴───────┘

Returns:



5201
5202
5203
# File 'lib/polars/expr.rb', line 5201

def shrink_dtype
  wrap_expr(_rbexpr.shrink_dtype)
end

#shuffle(seed: nil) ⇒ Expr

Shuffle the contents of this expr.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3]})
df.select(Polars.col("a").shuffle(seed: 1))
# =>
# shape: (3, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 2   │
# ├╌╌╌╌╌┤
# │ 1   │
# ├╌╌╌╌╌┤
# │ 3   │
# └─────┘

Parameters:

  • seed (Integer) (defaults to: nil)

    Seed for the random number generator. If set to None (default), a random seed is generated using the random module.

Returns:



4728
4729
4730
4731
4732
4733
# File 'lib/polars/expr.rb', line 4728

def shuffle(seed: nil)
  if seed.nil?
    seed = rand(10000)
  end
  wrap_expr(_rbexpr.shuffle(seed))
end

#signExpr

Compute the element-wise indication of the sign.

Examples:

df = Polars::DataFrame.new({"a" => [-9.0, -0.0, 0.0, 4.0, nil]})
df.select(Polars.col("a").sign)
# =>
# shape: (5, 1)
# ┌──────┐
# │ a    │
# │ ---  │
# │ i64  │
# ╞══════╡
# │ -1   │
# ├╌╌╌╌╌╌┤
# │ 0    │
# ├╌╌╌╌╌╌┤
# │ 0    │
# ├╌╌╌╌╌╌┤
# │ 1    │
# ├╌╌╌╌╌╌┤
# │ null │
# └──────┘

Returns:



4432
4433
4434
# File 'lib/polars/expr.rb', line 4432

def sign
  wrap_expr(_rbexpr.sign)
end

#sinExpr

Compute the element-wise value for the sine.

Examples:

df = Polars::DataFrame.new({"a" => [0.0]})
df.select(Polars.col("a").sin)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 0.0 │
# └─────┘

Returns:



4452
4453
4454
# File 'lib/polars/expr.rb', line 4452

def sin
  wrap_expr(_rbexpr.sin)
end

#sinhExpr

Compute the element-wise value for the hyperbolic sine.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").sinh)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 1.175201 │
# └──────────┘

Returns:



4572
4573
4574
# File 'lib/polars/expr.rb', line 4572

def sinh
  wrap_expr(_rbexpr.sinh)
end

#skew(bias: true) ⇒ Expr

Compute the sample skewness of a data set.

For normally distributed data, the skewness should be about zero. For unimodal continuous distributions, a skewness value greater than zero means that there is more weight in the right tail of the distribution. The function skewtest can be used to determine if the skewness value is close enough to zero, statistically speaking.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 2, 1]})
df.select(Polars.col("a").skew)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 0.343622 │
# └──────────┘

Parameters:

  • bias (Boolean) (defaults to: true)

    If false, the calculations are corrected for statistical bias.

Returns:



4222
4223
4224
# File 'lib/polars/expr.rb', line 4222

def skew(bias: true)
  wrap_expr(_rbexpr.skew(bias))
end

#slice(offset, length = nil) ⇒ Expr

Get a slice of this expression.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [8, 9, 10, 11],
    "b" => [nil, 4, 4, 4]
  }
)
df.select(Polars.all.slice(1, 2))
# =>
# shape: (2, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 9   ┆ 4   │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 10  ┆ 4   │
# └─────┴─────┘

Parameters:

  • offset (Integer)

    Start index. Negative indexing is supported.

  • length (Integer) (defaults to: nil)

    Length of the slice. If set to nil, all rows starting at the offset will be selected.

Returns:



809
810
811
812
813
814
815
816
817
# File 'lib/polars/expr.rb', line 809

def slice(offset, length = nil)
  if !offset.is_a?(Expr)
    offset = Polars.lit(offset)
  end
  if !length.is_a?(Expr)
    length = Polars.lit(length)
  end
  wrap_expr(_rbexpr.slice(offset._rbexpr, length._rbexpr))
end

#sort(reverse: false, nulls_last: false) ⇒ Expr

Sort this column. In projection/ selection context the whole column is sorted.

If used in a groupby context, the groups are sorted.

Examples:

df = Polars::DataFrame.new(
  {
    "group" => [
        "one",
        "one",
        "one",
        "two",
        "two",
        "two"
    ],
    "value" => [1, 98, 2, 3, 99, 4]
  }
)
df.select(Polars.col("value").sort)
# =>
# shape: (6, 1)
# ┌───────┐
# │ value │
# │ ---   │
# │ i64   │
# ╞═══════╡
# │ 1     │
# ├╌╌╌╌╌╌╌┤
# │ 2     │
# ├╌╌╌╌╌╌╌┤
# │ 3     │
# ├╌╌╌╌╌╌╌┤
# │ 4     │
# ├╌╌╌╌╌╌╌┤
# │ 98    │
# ├╌╌╌╌╌╌╌┤
# │ 99    │
# └───────┘
df.select(Polars.col("value").sort)
# =>
# shape: (6, 1)
# ┌───────┐
# │ value │
# │ ---   │
# │ i64   │
# ╞═══════╡
# │ 1     │
# ├╌╌╌╌╌╌╌┤
# │ 2     │
# ├╌╌╌╌╌╌╌┤
# │ 3     │
# ├╌╌╌╌╌╌╌┤
# │ 4     │
# ├╌╌╌╌╌╌╌┤
# │ 98    │
# ├╌╌╌╌╌╌╌┤
# │ 99    │
# └───────┘
df.groupby("group").agg(Polars.col("value").sort)
# =>
# shape: (2, 2)
# ┌───────┬────────────┐
# │ group ┆ value      │
# │ ---   ┆ ---        │
# │ str   ┆ list[i64]  │
# ╞═══════╪════════════╡
# │ two   ┆ [3, 4, 99] │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ one   ┆ [1, 2, 98] │
# └───────┴────────────┘

Parameters:

  • reverse (Boolean) (defaults to: false)

    false -> order from small to large. true -> order from large to small.

  • nulls_last (Boolean) (defaults to: false)

    If true nulls are considered to be larger than any valid value.

Returns:



1388
1389
1390
# File 'lib/polars/expr.rb', line 1388

def sort(reverse: false, nulls_last: false)
  wrap_expr(_rbexpr.sort_with(reverse, nulls_last))
end

#sort_by(by, reverse: false) ⇒ Expr

Sort this column by the ordering of another column, or multiple other columns.

In projection/ selection context the whole column is sorted. If used in a groupby context, the groups are sorted.

Examples:

df = Polars::DataFrame.new(
  {
    "group" => [
      "one",
      "one",
      "one",
      "two",
      "two",
      "two"
    ],
    "value" => [1, 98, 2, 3, 99, 4]
  }
)
df.select(Polars.col("group").sort_by("value"))
# =>
# shape: (6, 1)
# ┌───────┐
# │ group │
# │ ---   │
# │ str   │
# ╞═══════╡
# │ one   │
# ├╌╌╌╌╌╌╌┤
# │ one   │
# ├╌╌╌╌╌╌╌┤
# │ two   │
# ├╌╌╌╌╌╌╌┤
# │ two   │
# ├╌╌╌╌╌╌╌┤
# │ one   │
# ├╌╌╌╌╌╌╌┤
# │ two   │
# └───────┘

Parameters:

  • by (Object)

    The column(s) used for sorting.

  • reverse (Boolean) (defaults to: false)

    false -> order from small to large. true -> order from large to small.

Returns:



1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
# File 'lib/polars/expr.rb', line 1598

def sort_by(by, reverse: false)
  if !by.is_a?(Array)
    by = [by]
  end
  if !reverse.is_a?(Array)
    reverse = [reverse]
  end
  by = Utils.selection_to_rbexpr_list(by)

  wrap_expr(_rbexpr.sort_by(by, reverse))
end

#sqrtExpr

Compute the square root of the elements.

Examples:

df = Polars::DataFrame.new({"values" => [1.0, 2.0, 4.0]})
df.select(Polars.col("values").sqrt)
# =>
# shape: (3, 1)
# ┌──────────┐
# │ values   │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 1.0      │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 1.414214 │
# ├╌╌╌╌╌╌╌╌╌╌┤
# │ 2.0      │
# └──────────┘

Returns:



246
247
248
# File 'lib/polars/expr.rb', line 246

def sqrt
  self**0.5
end

#std(ddof: 1) ⇒ Expr

Get standard deviation.

Examples:

df = Polars::DataFrame.new({"a" => [-1, 0, 1]})
df.select(Polars.col("a").std)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.0 │
# └─────┘

Parameters:

  • ddof (Integer) (defaults to: 1)

    Degrees of freedom.

Returns:



1917
1918
1919
# File 'lib/polars/expr.rb', line 1917

def std(ddof: 1)
  wrap_expr(_rbexpr.std(ddof))
end

#strStringExpr

Create an object namespace of all string related methods.

Returns:



5236
5237
5238
# File 'lib/polars/expr.rb', line 5236

def str
  StringExpr.new(self)
end

#structStructExpr

Create an object namespace of all struct related methods.

Returns:



5243
5244
5245
# File 'lib/polars/expr.rb', line 5243

def struct
  StructExpr.new(self)
end

#suffix(suffix) ⇒ Expr

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

Returns:



431
432
433
# File 'lib/polars/expr.rb', line 431

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

#sumExpr

Note:

Dtypes in :i8, :u8, :i16, and :u16 are cast to :i64 before summing to prevent overflow issues.

Get sum value.

Examples:

df = Polars::DataFrame.new({"a" => [-1, 0, 1]})
df.select(Polars.col("a").sum)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 0   │
# └─────┘

Returns:



2044
2045
2046
# File 'lib/polars/expr.rb', line 2044

def sum
  wrap_expr(_rbexpr.sum)
end

#tail(n = 10) ⇒ Expr

Get the last n rows.

Examples:

df = Polars::DataFrame.new({"foo" => [1, 2, 3, 4, 5, 6, 7]})
df.tail(3)
# =>
# shape: (3, 1)
# ┌─────┐
# │ foo │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 5   │
# ├╌╌╌╌╌┤
# │ 6   │
# ├╌╌╌╌╌┤
# │ 7   │
# └─────┘

Parameters:

  • n (Integer) (defaults to: 10)

    Number of rows to return.

Returns:



2844
2845
2846
# File 'lib/polars/expr.rb', line 2844

def tail(n = 10)
  wrap_expr(_rbexpr.tail(n))
end

#take(indices) ⇒ Expr

Take values by index.

Examples:

df = Polars::DataFrame.new(
  {
    "group" => [
      "one",
      "one",
      "one",
      "two",
      "two",
      "two"
    ],
    "value" => [1, 98, 2, 3, 99, 4]
  }
)
df.groupby("group", maintain_order: true).agg(Polars.col("value").take(1))
# =>
# shape: (2, 2)
# ┌───────┬───────┐
# │ group ┆ value │
# │ ---   ┆ ---   │
# │ str   ┆ i64   │
# ╞═══════╪═══════╡
# │ one   ┆ 98    │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
# │ two   ┆ 99    │
# └───────┴───────┘

Parameters:

  • indices (Expr)

    An expression that leads to a :u32 dtyped Series.

Returns:



1643
1644
1645
1646
1647
1648
1649
1650
# File 'lib/polars/expr.rb', line 1643

def take(indices)
  if indices.is_a?(Array)
    indices_lit = Polars.lit(Series.new("", indices, dtype: :u32))
  else
    indices_lit = Utils.expr_to_lit_or_expr(indices, str_to_lit: false)
  end
  wrap_expr(_rbexpr.take(indices_lit._rbexpr))
end

#take_every(n) ⇒ Expr

Take every nth value in the Series and return as a new Series.

Examples:

df = Polars::DataFrame.new({"foo" => [1, 2, 3, 4, 5, 6, 7, 8, 9]})
df.select(Polars.col("foo").take_every(3))
# =>
# shape: (3, 1)
# ┌─────┐
# │ foo │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 1   │
# ├╌╌╌╌╌┤
# │ 4   │
# ├╌╌╌╌╌┤
# │ 7   │
# └─────┘

Returns:



2790
2791
2792
# File 'lib/polars/expr.rb', line 2790

def take_every(n)
  wrap_expr(_rbexpr.take_every(n))
end

#tanExpr

Compute the element-wise value for the tangent.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").tan)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 1.557408 │
# └──────────┘

Returns:



4492
4493
4494
# File 'lib/polars/expr.rb', line 4492

def tan
  wrap_expr(_rbexpr.tan)
end

#tanhExpr

Compute the element-wise value for the hyperbolic tangent.

Examples:

df = Polars::DataFrame.new({"a" => [1.0]})
df.select(Polars.col("a").tanh)
# =>
# shape: (1, 1)
# ┌──────────┐
# │ a        │
# │ ---      │
# │ f64      │
# ╞══════════╡
# │ 0.761594 │
# └──────────┘

Returns:



4612
4613
4614
# File 'lib/polars/expr.rb', line 4612

def tanh
  wrap_expr(_rbexpr.tanh)
end

#to_physicalExpr

Cast to physical representation of the logical dtype.

  • :date -> :i32
  • :datetime -> :i64
  • :time -> :i64
  • :duration -> :i64
  • :cat -> :u32
  • Other data types will be left unchanged.

Examples:

Polars::DataFrame.new({"vals" => ["a", "x", nil, "a"]}).with_columns(
  [
    Polars.col("vals").cast(:cat),
    Polars.col("vals")
      .cast(:cat)
      .to_physical
      .alias("vals_physical")
  ]
)
# =>
# shape: (4, 2)
# ┌──────┬───────────────┐
# │ vals ┆ vals_physical │
# │ ---  ┆ ---           │
# │ cat  ┆ u32           │
# ╞══════╪═══════════════╡
# │ a    ┆ 0             │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ x    ┆ 1             │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ null ┆ null          │
# ├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ a    ┆ 0             │
# └──────┴───────────────┘

Returns:



177
178
179
# File 'lib/polars/expr.rb', line 177

def to_physical
  wrap_expr(_rbexpr.to_physical)
end

#to_sString Also known as: inspect

Returns a string representing the Expr.

Returns:

  • (String)


17
18
19
# File 'lib/polars/expr.rb', line 17

def to_s
  _rbexpr.to_str
end

#top_k(k: 5, reverse: false) ⇒ Expr

Return the k largest elements.

If 'reverse: true` the smallest elements will be given.

Examples:

df = Polars::DataFrame.new(
  {
    "value" => [1, 98, 2, 3, 99, 4]
  }
)
df.select(
  [
    Polars.col("value").top_k.alias("top_k"),
    Polars.col("value").top_k(reverse: true).alias("bottom_k")
  ]
)
# =>
# shape: (5, 2)
# ┌───────┬──────────┐
# │ top_k ┆ bottom_k │
# │ ---   ┆ ---      │
# │ i64   ┆ i64      │
# ╞═══════╪══════════╡
# │ 99    ┆ 1        │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 98    ┆ 2        │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 4     ┆ 3        │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 3     ┆ 4        │
# ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ 2     ┆ 98       │
# └───────┴──────────┘

Parameters:

  • k (Integer) (defaults to: 5)

    Number of elements to return.

  • reverse (Boolean) (defaults to: false)

    Return the smallest elements.

Returns:



1432
1433
1434
# File 'lib/polars/expr.rb', line 1432

def top_k(k: 5, reverse: false)
  wrap_expr(_rbexpr.top_k(k, reverse))
end

#unique(maintain_order: false) ⇒ Expr

Get unique values of this expression.

Examples:

df = Polars::DataFrame.new({"a" => [1, 1, 2]})
df.select(Polars.col("a").unique(maintain_order: true))
# =>
# shape: (2, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 1   │
# ├╌╌╌╌╌┤
# │ 2   │
# └─────┘

Parameters:

  • maintain_order (Boolean) (defaults to: false)

    Maintain order of data. This requires more work.

Returns:



2217
2218
2219
2220
2221
2222
2223
# File 'lib/polars/expr.rb', line 2217

def unique(maintain_order: false)
  if maintain_order
    wrap_expr(_rbexpr.unique_stable)
  else
    wrap_expr(_rbexpr.unique)
  end
end

#unique_countsExpr

Return a count of the unique values in the order of appearance.

This method differs from value_counts in that it does not return the values, only the counts and might be faster

Examples:

df = Polars::DataFrame.new(
  {
    "id" => ["a", "b", "b", "c", "c", "c"]
  }
)
df.select(
  [
    Polars.col("id").unique_counts
  ]
)
# =>
# shape: (3, 1)
# ┌─────┐
# │ id  │
# │ --- │
# │ u32 │
# ╞═════╡
# │ 1   │
# ├╌╌╌╌╌┤
# │ 2   │
# ├╌╌╌╌╌┤
# │ 3   │
# └─────┘

Returns:



4991
4992
4993
# File 'lib/polars/expr.rb', line 4991

def unique_counts
  wrap_expr(_rbexpr.unique_counts)
end

#upper_boundExpr

Calculate the upper bound.

Returns a unit Series with the highest value possible for the dtype of this expression.

Examples:

df = Polars::DataFrame.new({"a" => [1, 2, 3, 2, 1]})
df.select(Polars.col("a").upper_bound)
# =>
# shape: (1, 1)
# ┌─────────────────────┐
# │ a                   │
# │ ---                 │
# │ i64                 │
# ╞═════════════════════╡
# │ 9223372036854775807 │
# └─────────────────────┘

Returns:



4404
4405
4406
# File 'lib/polars/expr.rb', line 4404

def upper_bound
  wrap_expr(_rbexpr.upper_bound)
end

#value_counts(multithreaded: false, sort: false) ⇒ Expr

Count all unique values and create a struct mapping value to count.

Examples:

df = Polars::DataFrame.new(
  {
    "id" => ["a", "b", "b", "c", "c", "c"]
  }
)
df.select(
  [
    Polars.col("id").value_counts(sort: true),
  ]
)
# =>
# shape: (3, 1)
# ┌───────────┐
# │ id        │
# │ ---       │
# │ struct[2] │
# ╞═══════════╡
# │ {"c",3}   │
# ├╌╌╌╌╌╌╌╌╌╌╌┤
# │ {"b",2}   │
# ├╌╌╌╌╌╌╌╌╌╌╌┤
# │ {"a",1}   │
# └───────────┘

Parameters:

  • multithreaded (Boolean) (defaults to: false)

    Better to turn this off in the aggregation context, as it can lead to contention.

  • sort (Boolean) (defaults to: false)

    Ensure the output is sorted from most values to least.

Returns:



4956
4957
4958
# File 'lib/polars/expr.rb', line 4956

def value_counts(multithreaded: false, sort: false)
  wrap_expr(_rbexpr.value_counts(multithreaded, sort))
end

#var(ddof: 1) ⇒ Expr

Get variance.

Examples:

df = Polars::DataFrame.new({"a" => [-1, 0, 1]})
df.select(Polars.col("a").var)
# =>
# shape: (1, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.0 │
# └─────┘

Parameters:

  • ddof (Integer) (defaults to: 1)

    Degrees of freedom.

Returns:



1940
1941
1942
# File 'lib/polars/expr.rb', line 1940

def var(ddof: 1)
  wrap_expr(_rbexpr.var(ddof))
end

#where(predicate) ⇒ Expr

Filter a single column.

Alias for #filter.

Examples:

df = Polars::DataFrame.new(
  {
    "group_col" => ["g1", "g1", "g2"],
    "b" => [1, 2, 3]
  }
)
(
  df.groupby("group_col").agg(
    [
      Polars.col("b").where(Polars.col("b") < 2).sum.alias("lt"),
      Polars.col("b").where(Polars.col("b") >= 2).sum.alias("gte")
    ]
  )
).sort("group_col")
# =>
# shape: (2, 3)
# ┌───────────┬──────┬─────┐
# │ group_col ┆ lt   ┆ gte │
# │ ---       ┆ ---  ┆ --- │
# │ str       ┆ i64  ┆ i64 │
# ╞═══════════╪══════╪═════╡
# │ g1        ┆ 1    ┆ 2   │
# ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┤
# │ g2        ┆ null ┆ 3   │
# └───────────┴──────┴─────┘

Parameters:

  • predicate (Expr)

    Boolean expression.

Returns:



2568
2569
2570
# File 'lib/polars/expr.rb', line 2568

def where(predicate)
  filter(predicate)
end

#|(other) ⇒ Expr

Bitwise OR.

Returns:



39
40
41
# File 'lib/polars/expr.rb', line 39

def |(other)
  wrap_expr(_rbexpr._or(_to_rbexpr(other)))
end