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.

Returns:



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.

Returns:



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.

Returns:



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 │
# └───────┘

Returns:



206
207
208
# File 'lib/polars/selector.rb', line 206

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).

Parameters:

  • columns (Object)

    The name or datatype of the column(s) to exclude. Accepts regular expression input. Regular expressions should start with ^ and end with $.

  • more_columns (Array)

    Additional names or datatypes of columns to exclude, specified as positional arguments.

Returns:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/polars/selector.rb', line 138

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 - Selectors.by_name(exclude_cols, require_all: false)
  end
end

#inspectString

Returns a string representing the Selector.

Returns:



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

def inspect
  Expr._from_rbexpr(_rbexpr).to_s
end

#|(other) ⇒ Selector

OR.

Returns:



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.

Returns:



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

def ~
  Selectors.all - self
end