Class: Polars::ListExpr
- Inherits:
-
Object
- Object
- Polars::ListExpr
- Defined in:
- lib/polars/list_expr.rb
Overview
Namespace for list related expressions.
Instance Method Summary collapse
-
#[](item) ⇒ Expr
Get the value by index in the sublists.
-
#arg_max ⇒ Expr
Retrieve the index of the maximum value in every sublist.
-
#arg_min ⇒ Expr
Retrieve the index of the minimal value in every sublist.
-
#concat(other) ⇒ Expr
Concat the arrays in a Series dtype List in linear time.
-
#contains(item) ⇒ Expr
Check if sublists contain the given item.
-
#count_matches(element) ⇒ Expr
(also: #count_match)
Count how often the value produced by
elementoccurs. -
#diff(n: 1, null_behavior: "ignore") ⇒ Expr
Calculate the n-th discrete difference of every sublist.
-
#eval(expr, parallel: false) ⇒ Expr
Run any polars expression against the lists' elements.
-
#first ⇒ Expr
Get the first value of the sublists.
-
#get(index) ⇒ Expr
Get the value by index in the sublists.
-
#head(n = 5) ⇒ Expr
Slice the first
nvalues of every sublist. -
#join(separator) ⇒ Expr
Join all string items in a sublist and place a separator between them.
-
#last ⇒ Expr
Get the last value of the sublists.
-
#lengths ⇒ Expr
(also: #len)
Get the length of the arrays as
:u32. -
#max ⇒ Expr
Compute the max value of the lists in the array.
-
#mean ⇒ Expr
Compute the mean value of the lists in the array.
-
#min ⇒ Expr
Compute the min value of the lists in the array.
-
#reverse ⇒ Expr
Reverse the arrays in the list.
-
#shift(n = 1) ⇒ Expr
Shift values by the given period.
-
#slice(offset, length = nil) ⇒ Expr
Slice every sublist.
-
#sort(reverse: false) ⇒ Expr
Sort the arrays in the list.
-
#sum ⇒ Expr
Sum all the lists in the array.
-
#tail(n = 5) ⇒ Expr
Slice the last
nvalues of every sublist. -
#take(index, null_on_oob: false) ⇒ Expr
Take sublists by multiple indices.
-
#to_struct(n_field_strategy: "first_non_null", name_generator: nil) ⇒ Expr
Convert the series of type
Listto a series of typeStruct. -
#unique(maintain_order: false) ⇒ Expr
Get the unique/distinct values in the list.
Instance Method Details
#[](item) ⇒ Expr
Get the value by index in the sublists.
265 266 267 |
# File 'lib/polars/list_expr.rb', line 265 def [](item) get(item) end |
#arg_max ⇒ Expr
Retrieve the index of the maximum value in every sublist.
433 434 435 |
# File 'lib/polars/list_expr.rb', line 433 def arg_max Utils.wrap_expr(_rbexpr.list_arg_max) end |
#arg_min ⇒ Expr
Retrieve the index of the minimal value in every sublist.
408 409 410 |
# File 'lib/polars/list_expr.rb', line 408 def arg_min Utils.wrap_expr(_rbexpr.list_arg_min) end |
#concat(other) ⇒ Expr
Concat the arrays in a Series dtype List in linear time.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/polars/list_expr.rb', line 217 def concat(other) if other.is_a?(::Array) && ![Expr, String, Series].any? { |c| other[0].is_a?(c) } return concat(Series.new([other])) end if !other.is_a?(::Array) other_list = [other] else other_list = other.dup end other_list.insert(0, Utils.wrap_expr(_rbexpr)) Polars.concat_list(other_list) end |
#contains(item) ⇒ Expr
Check if sublists contain the given item.
356 357 358 |
# File 'lib/polars/list_expr.rb', line 356 def contains(item) Utils.wrap_expr(_rbexpr.list_contains(Utils.expr_to_lit_or_expr(item)._rbexpr)) end |
#count_matches(element) ⇒ Expr Also known as: count_match
Count how often the value produced by element occurs.
574 575 576 |
# File 'lib/polars/list_expr.rb', line 574 def count_matches(element) Utils.wrap_expr(_rbexpr.list_count_matches(Utils.expr_to_lit_or_expr(element)._rbexpr)) end |
#diff(n: 1, null_behavior: "ignore") ⇒ Expr
Calculate the n-th discrete difference of every sublist.
456 457 458 |
# File 'lib/polars/list_expr.rb', line 456 def diff(n: 1, null_behavior: "ignore") Utils.wrap_expr(_rbexpr.list_diff(n, null_behavior)) end |
#eval(expr, parallel: false) ⇒ Expr
Run any polars expression against the lists' elements.
637 638 639 |
# File 'lib/polars/list_expr.rb', line 637 def eval(expr, parallel: false) Utils.wrap_expr(_rbexpr.list_eval(expr._rbexpr, parallel)) end |
#first ⇒ Expr
Get the first value of the sublists.
309 310 311 |
# File 'lib/polars/list_expr.rb', line 309 def first get(0) end |
#get(index) ⇒ Expr
Get the value by index in the sublists.
So index 0 would return the first item of every sublist
and index -1 would return the last item of every sublist
if an index is out of bounds, it will return a None.
257 258 259 260 |
# File 'lib/polars/list_expr.rb', line 257 def get(index) index = Utils.parse_as_expression(index) Utils.wrap_expr(_rbexpr.list_get(index)) end |
#head(n = 5) ⇒ Expr
Slice the first n values of every sublist.
525 526 527 |
# File 'lib/polars/list_expr.rb', line 525 def head(n = 5) slice(0, n) end |
#join(separator) ⇒ Expr
Join all string items in a sublist and place a separator between them.
This errors if inner type of list != :str.
382 383 384 385 |
# File 'lib/polars/list_expr.rb', line 382 def join(separator) separator = Utils.parse_as_expression(separator, str_as_lit: true) Utils.wrap_expr(_rbexpr.list_join(separator)) end |
#last ⇒ Expr
Get the last value of the sublists.
331 332 333 |
# File 'lib/polars/list_expr.rb', line 331 def last get(-1) end |
#lengths ⇒ Expr Also known as: len
Get the length of the arrays as :u32.
29 30 31 |
# File 'lib/polars/list_expr.rb', line 29 def lengths Utils.wrap_expr(_rbexpr.list_len) end |
#max ⇒ Expr
Compute the max value of the lists in the array.
72 73 74 |
# File 'lib/polars/list_expr.rb', line 72 def max Utils.wrap_expr(_rbexpr.list_max) end |
#mean ⇒ Expr
Compute the mean value of the lists in the array.
114 115 116 |
# File 'lib/polars/list_expr.rb', line 114 def mean Utils.wrap_expr(_rbexpr.list_mean) end |
#min ⇒ Expr
Compute the min value of the lists in the array.
93 94 95 |
# File 'lib/polars/list_expr.rb', line 93 def min Utils.wrap_expr(_rbexpr.list_min) end |
#reverse ⇒ Expr
Reverse the arrays in the list.
164 165 166 |
# File 'lib/polars/list_expr.rb', line 164 def reverse Utils.wrap_expr(_rbexpr.list_reverse) end |
#shift(n = 1) ⇒ Expr
Shift values by the given period.
477 478 479 480 |
# File 'lib/polars/list_expr.rb', line 477 def shift(n = 1) n = Utils.parse_as_expression(n) Utils.wrap_expr(_rbexpr.list_shift(n)) end |
#slice(offset, length = nil) ⇒ Expr
Slice every sublist.
502 503 504 505 506 |
# File 'lib/polars/list_expr.rb', line 502 def slice(offset, length = nil) offset = Utils.expr_to_lit_or_expr(offset, str_to_lit: false)._rbexpr length = Utils.expr_to_lit_or_expr(length, str_to_lit: false)._rbexpr Utils.wrap_expr(_rbexpr.list_slice(offset, length)) end |
#sort(reverse: false) ⇒ Expr
Sort the arrays in the list.
139 140 141 |
# File 'lib/polars/list_expr.rb', line 139 def sort(reverse: false) Utils.wrap_expr(_rbexpr.list_sort(reverse)) end |
#sum ⇒ Expr
Sum all the lists in the array.
51 52 53 |
# File 'lib/polars/list_expr.rb', line 51 def sum Utils.wrap_expr(_rbexpr.list_sum) end |
#tail(n = 5) ⇒ Expr
Slice the last n values of every sublist.
546 547 548 549 |
# File 'lib/polars/list_expr.rb', line 546 def tail(n = 5) offset = -Utils.expr_to_lit_or_expr(n, str_to_lit: false) slice(offset, n) end |
#take(index, null_on_oob: false) ⇒ Expr
Take sublists by multiple indices.
The indices may be defined in a single column, or by sublists in another
column of dtype List.
283 284 285 286 287 288 289 |
# File 'lib/polars/list_expr.rb', line 283 def take(index, null_on_oob: false) if index.is_a?(::Array) index = Series.new(index) end index = Utils.expr_to_lit_or_expr(index, str_to_lit: false)._rbexpr Utils.wrap_expr(_rbexpr.list_take(index, null_on_oob)) end |
#to_struct(n_field_strategy: "first_non_null", name_generator: nil) ⇒ Expr
Convert the series of type List to a series of type Struct.
602 603 604 605 |
# File 'lib/polars/list_expr.rb', line 602 def to_struct(n_field_strategy: "first_non_null", name_generator: nil) raise Todo if name_generator Utils.wrap_expr(_rbexpr.list_to_struct(n_field_strategy, name_generator, 0)) end |
#unique(maintain_order: false) ⇒ Expr
Get the unique/distinct values in the list.
188 189 190 |
# File 'lib/polars/list_expr.rb', line 188 def unique(maintain_order: false) Utils.wrap_expr(_rbexpr.list_unique(maintain_order)) end |