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, nulls_equal: true) ⇒ 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) ⇒ Expr
Run any polars expression against the lists' elements.
-
#explode ⇒ Expr
Returns a column with a separate row for every list element.
-
#filter(predicate) ⇒ Expr
Filter elements in each list by a boolean expression.
-
#first ⇒ Expr
Get the first value of the sublists.
-
#gather(indices, null_on_oob: false) ⇒ Expr
(also: #take)
Take sublists by multiple indices.
-
#gather_every(n, offset = 0) ⇒ Expr
Take every n-th value start from offset in sublists.
-
#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.
-
#len ⇒ Expr
(also: #lengths)
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.
-
#median ⇒ Expr
Compute the median value of the lists in the array.
-
#min ⇒ Expr
Compute the min value of the lists in the array.
-
#n_unique ⇒ Expr
Count the number of unique values in every sub-lists.
-
#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.
-
#set_difference(other) ⇒ Expr
Compute the SET DIFFERENCE between the elements in this list and the elements of
other
. -
#set_intersection(other) ⇒ Expr
Compute the SET INTERSECTION between the elements in this list and the elements of
other
. -
#set_symmetric_difference(other) ⇒ Expr
Compute the SET SYMMETRIC DIFFERENCE between the elements in this list and the elements of
other
. -
#set_union(other) ⇒ Expr
Compute the SET UNION between the elements in this list and the elements of
other
. -
#shift(n = 1) ⇒ Expr
Shift values by the given period.
-
#slice(offset, length = nil) ⇒ Expr
Slice every sublist.
-
#sort(reverse: false, nulls_last: false) ⇒ Expr
Sort the arrays in the list.
-
#std(ddof: 1) ⇒ Expr
Compute the std value of the lists in the array.
-
#sum ⇒ Expr
Sum all the lists in the array.
-
#tail(n = 5) ⇒ Expr
Slice the last
n
values of every sublist. -
#to_array(width) ⇒ Expr
Convert a List column into an Array column with the same inner data type.
-
#to_struct(n_field_strategy: "first_non_null", fields: nil, upper_bound: 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.
-
#var(ddof: 1) ⇒ Expr
Compute the var value of the lists in the array.
Instance Method Details
#[](item) ⇒ Expr
Get the value by index in the sublists.
500 501 502 |
# File 'lib/polars/list_expr.rb', line 500 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.
727 728 729 |
# File 'lib/polars/list_expr.rb', line 727 def arg_max Utils.wrap_expr(_rbexpr.list_arg_max) end |
#arg_min ⇒ Expr
Retrieve the index of the minimal value in every sublist.
702 703 704 |
# File 'lib/polars/list_expr.rb', line 702 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.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/polars/list_expr.rb', line 448 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, nulls_equal: true) ⇒ Expr
Check if sublists contain the given item.
648 649 650 |
# File 'lib/polars/list_expr.rb', line 648 def contains(item, nulls_equal: true) Utils.wrap_expr(_rbexpr.list_contains(Utils.parse_into_expression(item), nulls_equal)) end |
#count_matches(element) ⇒ Expr Also known as: count_match
Count how often the value produced by element
occurs.
893 894 895 |
# File 'lib/polars/list_expr.rb', line 893 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.
750 751 752 |
# File 'lib/polars/list_expr.rb', line 750 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) ⇒ Expr
Run any polars expression against the lists' elements.
988 989 990 |
# File 'lib/polars/list_expr.rb', line 988 def eval(expr) Utils.wrap_expr(_rbexpr.list_eval(expr._rbexpr)) end |
#explode ⇒ Expr
Returns a column with a separate row for every list element.
866 867 868 |
# File 'lib/polars/list_expr.rb', line 866 def explode Utils.wrap_expr(_rbexpr.explode) end |
#filter(predicate) ⇒ Expr
Filter elements in each list by a boolean expression.
1016 1017 1018 |
# File 'lib/polars/list_expr.rb', line 1016 def filter(predicate) Utils.wrap_expr(_rbexpr.list_filter(predicate._rbexpr)) end |
#first ⇒ Expr
Get the first value of the sublists.
599 600 601 |
# File 'lib/polars/list_expr.rb', line 599 def first get(0) end |
#gather(indices, 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
.
533 534 535 536 |
# File 'lib/polars/list_expr.rb', line 533 def gather(indices, null_on_oob: false) indices = Utils.parse_into_expression(indices) Utils.wrap_expr(_rbexpr.list_gather(indices, null_on_oob)) end |
#gather_every(n, offset = 0) ⇒ Expr
Take every n-th value start from offset in sublists.
572 573 574 575 576 577 578 579 |
# File 'lib/polars/list_expr.rb', line 572 def gather_every( n, offset = 0 ) n = Utils.parse_into_expression(n) offset = Utils.parse_into_expression(offset) Utils.wrap_expr(_rbexpr.list_gather_every(n, offset)) 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 nil
.
492 493 494 495 |
# File 'lib/polars/list_expr.rb', line 492 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.
819 820 821 |
# File 'lib/polars/list_expr.rb', line 819 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
.
676 677 678 679 |
# File 'lib/polars/list_expr.rb', line 676 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.
621 622 623 |
# File 'lib/polars/list_expr.rb', line 621 def last get(-1) end |
#len ⇒ Expr Also known as: lengths
Get the length of the arrays as :u32
.
83 84 85 |
# File 'lib/polars/list_expr.rb', line 83 def len 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 |
#median ⇒ Expr
Compute the median value of the lists in the array.
263 264 265 |
# File 'lib/polars/list_expr.rb', line 263 def median Utils.wrap_expr(_rbexpr.list_median) 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 |
#n_unique ⇒ Expr
Count the number of unique values in every sub-lists.
419 420 421 |
# File 'lib/polars/list_expr.rb', line 419 def n_unique Utils.wrap_expr(_rbexpr.list_n_unique) end |
#reverse ⇒ Expr
Reverse the arrays in the list.
370 371 372 |
# File 'lib/polars/list_expr.rb', line 370 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 |
#set_difference(other) ⇒ Expr
Compute the SET DIFFERENCE between the elements in this list and the elements of other
.
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 |
# File 'lib/polars/list_expr.rb', line 1088 def set_difference(other) if other.respond_to?(:each) if !other.is_a?(::Array) && !other.is_a?(Series) && !other.is_a?(DataFrame) other = other.to_a end other = F.lit(other)._rbexpr else other = Utils.parse_into_expression(other) end Utils.wrap_expr(_rbexpr.list_set_operation(other, "difference")) end |
#set_intersection(other) ⇒ Expr
Compute the SET INTERSECTION between the elements in this list and the elements of other
.
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 |
# File 'lib/polars/list_expr.rb', line 1127 def set_intersection(other) if other.respond_to?(:each) if !other.is_a?(::Array) && !other.is_a?(Series) && !other.is_a?(DataFrame) other = other.to_a end other = F.lit(other)._rbexpr else other = Utils.parse_into_expression(other) end Utils.wrap_expr(_rbexpr.list_set_operation(other, "intersection")) end |
#set_symmetric_difference(other) ⇒ Expr
Compute the SET SYMMETRIC DIFFERENCE between the elements in this list and the elements of other
.
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 |
# File 'lib/polars/list_expr.rb', line 1166 def set_symmetric_difference(other) if other.respond_to?(:each) if !other.is_a?(::Array) && !other.is_a?(Series) && !other.is_a?(DataFrame) other = other.to_a end other = F.lit(other)._rbexpr else other = Utils.parse_into_expression(other) end Utils.wrap_expr(_rbexpr.list_set_operation(other, "symmetric_difference")) end |
#set_union(other) ⇒ Expr
Compute the SET UNION between the elements in this list and the elements of other
.
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 |
# File 'lib/polars/list_expr.rb', line 1049 def set_union(other) if other.respond_to?(:each) if !other.is_a?(::Array) && !other.is_a?(Series) && !other.is_a?(DataFrame) other = other.to_a end other = F.lit(other)._rbexpr else other = Utils.parse_into_expression(other) end Utils.wrap_expr(_rbexpr.list_set_operation(other, "union")) end |
#shift(n = 1) ⇒ Expr
Shift values by the given period.
771 772 773 774 |
# File 'lib/polars/list_expr.rb', line 771 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.
796 797 798 799 800 |
# File 'lib/polars/list_expr.rb', line 796 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, nulls_last: false) ⇒ Expr
Sort the arrays in the list.
345 346 347 |
# File 'lib/polars/list_expr.rb', line 345 def sort(reverse: false, nulls_last: false) Utils.wrap_expr(_rbexpr.list_sort(reverse, nulls_last)) end |
#std(ddof: 1) ⇒ Expr
Compute the std value of the lists in the array.
289 290 291 |
# File 'lib/polars/list_expr.rb', line 289 def std(ddof: 1) Utils.wrap_expr(_rbexpr.list_std(ddof)) 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.
840 841 842 843 |
# File 'lib/polars/list_expr.rb', line 840 def tail(n = 5) n = Utils.parse_into_expression(n) Utils.wrap_expr(_rbexpr.list_tail(n)) end |
#to_array(width) ⇒ Expr
Convert a List column into an Array column with the same inner data type.
921 922 923 |
# File 'lib/polars/list_expr.rb', line 921 def to_array(width) Utils.wrap_expr(_rbexpr.list_to_array(width)) end |
#to_struct(n_field_strategy: "first_non_null", fields: nil, upper_bound: nil) ⇒ Expr
Convert the series of type List
to a series of type Struct
.
960 961 962 |
# File 'lib/polars/list_expr.rb', line 960 def to_struct(n_field_strategy: "first_non_null", fields: nil, upper_bound: nil) Utils.wrap_expr(_rbexpr.list_to_struct(n_field_strategy, fields, nil)) end |
#unique(maintain_order: false) ⇒ Expr
Get the unique/distinct values in the list.
394 395 396 |
# File 'lib/polars/list_expr.rb', line 394 def unique(maintain_order: false) Utils.wrap_expr(_rbexpr.list_unique(maintain_order)) end |
#var(ddof: 1) ⇒ Expr
Compute the var value of the lists in the array.
315 316 317 |
# File 'lib/polars/list_expr.rb', line 315 def var(ddof: 1) Utils.wrap_expr(_rbexpr.list_var(ddof)) end |