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.
-
#all ⇒ Expr
Evaluate whether all boolean values in a list are true.
-
#any ⇒ Expr
Evaluate whether any boolean value in a list is true.
-
#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
element
occurs. -
#diff(n: 1, null_behavior: "ignore") ⇒ Expr
Calculate the n-th discrete difference of every sublist.
-
#drop_nulls ⇒ Expr
Drop all null values in the list.
-
#eval(expr, parallel: false) ⇒ Expr
Run any polars expression against the lists' elements.
-
#first ⇒ Expr
Get the first value of the sublists.
-
#gather(index, null_on_oob: false) ⇒ Expr
(also: #take)
Take sublists by multiple indices.
-
#get(index, null_on_oob: true) ⇒ Expr
Get the value by index in the sublists.
-
#head(n = 5) ⇒ Expr
Slice the first
n
values of every sublist. -
#join(separator, ignore_nulls: true) ⇒ 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.
-
#sample(n: nil, fraction: nil, with_replacement: false, shuffle: false, seed: nil) ⇒ Expr
Sample from this 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
n
values of every sublist. -
#to_struct(n_field_strategy: "first_non_null", name_generator: nil) ⇒ Expr
Convert the series of type
List
to 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.
397 398 399 |
# File 'lib/polars/list_expr.rb', line 397 def [](item) get(item) end |
#all ⇒ Expr
Evaluate whether all boolean values in a list are true.
35 36 37 |
# File 'lib/polars/list_expr.rb', line 35 def all Utils.wrap_expr(_rbexpr.list_all) end |
#any ⇒ Expr
Evaluate whether any boolean value in a list is true.
62 63 64 |
# File 'lib/polars/list_expr.rb', line 62 def any Utils.wrap_expr(_rbexpr.list_any) end |
#arg_max ⇒ Expr
Retrieve the index of the maximum value in every sublist.
583 584 585 |
# File 'lib/polars/list_expr.rb', line 583 def arg_max Utils.wrap_expr(_rbexpr.list_arg_max) end |
#arg_min ⇒ Expr
Retrieve the index of the minimal value in every sublist.
558 559 560 |
# File 'lib/polars/list_expr.rb', line 558 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.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/polars/list_expr.rb', line 345 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.
504 505 506 |
# File 'lib/polars/list_expr.rb', line 504 def contains(item) Utils.wrap_expr(_rbexpr.list_contains(Utils.parse_into_expression(item))) end |
#count_matches(element) ⇒ Expr Also known as: count_match
Count how often the value produced by element
occurs.
724 725 726 |
# File 'lib/polars/list_expr.rb', line 724 def count_matches(element) Utils.wrap_expr(_rbexpr.list_count_matches(Utils.parse_into_expression(element))) end |
#diff(n: 1, null_behavior: "ignore") ⇒ Expr
Calculate the n-th discrete difference of every sublist.
606 607 608 |
# File 'lib/polars/list_expr.rb', line 606 def diff(n: 1, null_behavior: "ignore") Utils.wrap_expr(_rbexpr.list_diff(n, null_behavior)) end |
#drop_nulls ⇒ Expr
Drop all null values in the list.
The original order of the remaining elements is preserved.
108 109 110 |
# File 'lib/polars/list_expr.rb', line 108 def drop_nulls Utils.wrap_expr(_rbexpr.list_drop_nulls) end |
#eval(expr, parallel: false) ⇒ Expr
Run any polars expression against the lists' elements.
787 788 789 |
# File 'lib/polars/list_expr.rb', line 787 def eval(expr, parallel: false) Utils.wrap_expr(_rbexpr.list_eval(expr._rbexpr, parallel)) end |
#first ⇒ Expr
Get the first value of the sublists.
457 458 459 |
# File 'lib/polars/list_expr.rb', line 457 def first get(0) end |
#gather(index, null_on_oob: false) ⇒ Expr Also known as: take
Take sublists by multiple indices.
The indices may be defined in a single column, or by sublists in another
column of dtype List
.
430 431 432 433 434 435 436 |
# File 'lib/polars/list_expr.rb', line 430 def gather(index, null_on_oob: false) if index.is_a?(::Array) index = Series.new(index) end index = Utils.parse_into_expression(index, str_as_lit: false) Utils.wrap_expr(_rbexpr.list_gather(index, null_on_oob)) end |
#get(index, null_on_oob: true) ⇒ 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
.
389 390 391 392 |
# File 'lib/polars/list_expr.rb', line 389 def get(index, null_on_oob: true) index = Utils.parse_into_expression(index) Utils.wrap_expr(_rbexpr.list_get(index, null_on_oob)) end |
#head(n = 5) ⇒ Expr
Slice the first n
values of every sublist.
675 676 677 |
# File 'lib/polars/list_expr.rb', line 675 def head(n = 5) slice(0, n) end |
#join(separator, ignore_nulls: true) ⇒ Expr
Join all string items in a sublist and place a separator between them.
This errors if inner type of list != :str
.
532 533 534 535 |
# File 'lib/polars/list_expr.rb', line 532 def join(separator, ignore_nulls: true) separator = Utils.parse_into_expression(separator, str_as_lit: true) Utils.wrap_expr(_rbexpr.list_join(separator, ignore_nulls)) end |
#last ⇒ Expr
Get the last value of the sublists.
479 480 481 |
# File 'lib/polars/list_expr.rb', line 479 def last get(-1) end |
#lengths ⇒ Expr Also known as: len
Get the length of the arrays as :u32
.
83 84 85 |
# File 'lib/polars/list_expr.rb', line 83 def lengths Utils.wrap_expr(_rbexpr.list_len) end |
#max ⇒ Expr
Compute the max value of the lists in the array.
200 201 202 |
# File 'lib/polars/list_expr.rb', line 200 def max Utils.wrap_expr(_rbexpr.list_max) end |
#mean ⇒ Expr
Compute the mean value of the lists in the array.
242 243 244 |
# File 'lib/polars/list_expr.rb', line 242 def mean Utils.wrap_expr(_rbexpr.list_mean) end |
#min ⇒ Expr
Compute the min value of the lists in the array.
221 222 223 |
# File 'lib/polars/list_expr.rb', line 221 def min Utils.wrap_expr(_rbexpr.list_min) end |
#reverse ⇒ Expr
Reverse the arrays in the list.
292 293 294 |
# File 'lib/polars/list_expr.rb', line 292 def reverse Utils.wrap_expr(_rbexpr.list_reverse) end |
#sample(n: nil, fraction: nil, with_replacement: false, shuffle: false, seed: nil) ⇒ Expr
Sample from this list.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/polars/list_expr.rb', line 142 def sample(n: nil, fraction: nil, with_replacement: false, shuffle: false, seed: nil) if !n.nil? && !fraction.nil? msg = "cannot specify both `n` and `fraction`" raise ArgumentError, msg end if !fraction.nil? fraction = Utils.parse_into_expression(fraction) return Utils.wrap_expr( _rbexpr.list_sample_fraction( fraction, with_replacement, shuffle, seed ) ) end n = 1 if n.nil? n = Utils.parse_into_expression(n) Utils.wrap_expr(_rbexpr.list_sample_n(n, with_replacement, shuffle, seed)) end |
#shift(n = 1) ⇒ Expr
Shift values by the given period.
627 628 629 630 |
# File 'lib/polars/list_expr.rb', line 627 def shift(n = 1) n = Utils.parse_into_expression(n) Utils.wrap_expr(_rbexpr.list_shift(n)) end |
#slice(offset, length = nil) ⇒ Expr
Slice every sublist.
652 653 654 655 656 |
# File 'lib/polars/list_expr.rb', line 652 def slice(offset, length = nil) offset = Utils.parse_into_expression(offset, str_as_lit: false) length = Utils.parse_into_expression(length, str_as_lit: false) Utils.wrap_expr(_rbexpr.list_slice(offset, length)) end |
#sort(reverse: false) ⇒ Expr
Sort the arrays in the list.
267 268 269 |
# File 'lib/polars/list_expr.rb', line 267 def sort(reverse: false) Utils.wrap_expr(_rbexpr.list_sort(reverse)) end |
#sum ⇒ Expr
Sum all the lists in the array.
179 180 181 |
# File 'lib/polars/list_expr.rb', line 179 def sum Utils.wrap_expr(_rbexpr.list_sum) end |
#tail(n = 5) ⇒ Expr
Slice the last n
values of every sublist.
696 697 698 699 |
# File 'lib/polars/list_expr.rb', line 696 def tail(n = 5) n = Utils.parse_into_expression(n) Utils.wrap_expr(_rbexpr.list_tail(n)) 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
.
752 753 754 755 |
# File 'lib/polars/list_expr.rb', line 752 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.
316 317 318 |
# File 'lib/polars/list_expr.rb', line 316 def unique(maintain_order: false) Utils.wrap_expr(_rbexpr.list_unique(maintain_order)) end |