Class: Polars::Selector

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

Overview

Base column selector expression/proxy.

Instance Method Summary collapse

Instance Method Details

#&(other) ⇒ Selector

AND.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/polars/selector.rb', line 66

def &(other)
  if Utils.is_column(other)
    colname = other.meta.output_name
    other = by_name(colname)
  end
  if Utils.is_selector(other)
    Selector._from_rbselector(
      _rbselector.intersect(other._rbselector)
    )
  else
    as_expr & other
  end
end

#-(other) ⇒ Selector

Difference.



99
100
101
102
103
104
105
106
107
# File 'lib/polars/selector.rb', line 99

def -(other)
  if Utils.is_selector(other)
    Selector._from_rbselector(
      _rbselector.difference(other._rbselector)
    )
  else
    as_expr - other
  end
end

#^(other) ⇒ Selector

XOR.



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/polars/selector.rb', line 112

def ^(other)
  if Utils.is_column(other)
    other = by_name(other.meta.output_name)
  end
  if Utils.is_selector(other)
    Selector._from_rbselector(
      _rbselector.exclusive_or(other._rbselector)
    )
  else
    as_expr ^ other
  end
end

#as_exprExpr

Materialize the selector as a normal expression.

This ensures that the operators |, &, ~ and - are applied on the data and not on the selector sets.

Examples:

Inverting the boolean selector will choose the non-boolean columns:

df = Polars::DataFrame.new(
  {
    "colx" => ["aa", "bb", "cc"],
    "coly" => [true, false, true],
    "colz" => [1, 2, 3]
  }
)
df.select(~Polars.cs.boolean)
# =>
# shape: (3, 2)
# ┌──────┬──────┐
# │ colx ┆ colz │
# │ ---  ┆ ---  │
# │ str  ┆ i64  │
# ╞══════╪══════╡
# │ aa   ┆ 1    │
# │ bb   ┆ 2    │
# │ cc   ┆ 3    │
# └──────┴──────┘

To invert the values in the selected boolean columns, we need to materialize the selector as a standard expression instead:

df.select(~Polars.cs.boolean.as_expr)
# =>
# shape: (3, 1)
# ┌───────┐
# │ coly  │
# │ ---   │
# │ bool  │
# ╞═══════╡
# │ false │
# │ true  │
# │ false │
# └───────┘


241
242
243
# File 'lib/polars/selector.rb', line 241

def as_expr
  Expr._from_rbexpr(_rbexpr)
end

#exclude(columns, *more_columns) ⇒ Selector

Exclude columns from a multi-column expression.

Only works after a wildcard or regex column selection, and you cannot provide both string column names and dtypes (you may prefer to use selectors instead).

Examples:

Exclude by column name(s):

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

Exclude using a column name, a selector, and a dtype:

df.select(Polars.cs.exclude("aa", Polars.cs.string, Polars::UInt32))
# =>
# shape: (3, 1)
# ┌──────┐
# │ cc   │
# │ ---  │
# │ f64  │
# ╞══════╡
# │ null │
# │ 2.5  │
# │ 1.5  │
# └──────┘


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/polars/selector.rb', line 173

def exclude(columns, *more_columns)
  exclude_cols = []
  exclude_dtypes = []
  ((columns.is_a?(::Array) ? columns : [columns]) + more_columns).each do |item|
    if item.is_a?(::String)
      exclude_cols << item
    elsif Utils.is_polars_dtype(item)
      exclude_dtypes << item
    else
      msg = (
        "invalid input for `exclude`" +
        "\n\nExpected one or more `str` or `DataType`; found #{item.inspect} instead."
      )
      raise TypeError, msg
    end
  end

  if exclude_cols.any? && exclude_dtypes.any?
    msg = "cannot exclude by both column name and dtype; use a selector instead"
    raise TypeError, msg
  elsif exclude_dtypes.any?
    self - Selectors.by_dtype(exclude_dtypes)
  else
    self - Selector._by_name(exclude_cols, strict: false, expand_patterns: true)
  end
end

#inspectString

Returns a string representing the Selector.



18
19
20
# File 'lib/polars/selector.rb', line 18

def inspect
  Expr._from_rbexpr(_rbexpr).to_s
end

#|(other) ⇒ Selector

OR.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/polars/selector.rb', line 83

def |(other)
  if Utils.is_column(other)
    other = by_name(other.meta.output_name)
  end
  if Utils.is_selector(other)
    Selector._from_rbselector(
      _rbselector.union(other._rbselector)
    )
  else
    as_expr | other
  end
end

#~Selector

Invert the selector.



59
60
61
# File 'lib/polars/selector.rb', line 59

def ~
  Selectors.all - self
end