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.
-
#agg(expr) ⇒ Expr
Run any polars aggregation expression against the lists' elements.
-
#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
Count how often the value produced by
elementoccurs. -
#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(empty_as_null: true, keep_nulls: true) ⇒ 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
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: false) ⇒ Expr
Get the value by index in the sublists.
-
#head(n = 5) ⇒ Expr
Slice the first
nvalues of every sublist. -
#item(allow_empty: false) ⇒ Expr
Get the single value of the sublists.
-
#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
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(descending: 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
nvalues 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: nil, fields: nil, upper_bound: 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.
-
#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.
499 500 501 |
# File 'lib/polars/list_expr.rb', line 499 def [](item) get(item) end |
#agg(expr) ⇒ Expr
Run any polars aggregation expression against the lists' elements.
1079 1080 1081 |
# File 'lib/polars/list_expr.rb', line 1079 def agg(expr) Utils.wrap_expr(_rbexpr.list_agg(expr._rbexpr)) 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.
767 768 769 |
# File 'lib/polars/list_expr.rb', line 767 def arg_max Utils.wrap_expr(_rbexpr.list_arg_max) end |
#arg_min ⇒ Expr
Retrieve the index of the minimal value in every sublist.
742 743 744 |
# File 'lib/polars/list_expr.rb', line 742 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.
447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/polars/list_expr.rb', line 447 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.
688 689 690 |
# File 'lib/polars/list_expr.rb', line 688 def contains(item, nulls_equal: true) Utils.wrap_expr(_rbexpr.list_contains(Utils.parse_into_expression(item), nulls_equal)) end |
#count_matches(element) ⇒ Expr
Count how often the value produced by element occurs.
938 939 940 |
# File 'lib/polars/list_expr.rb', line 938 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.
790 791 792 |
# File 'lib/polars/list_expr.rb', line 790 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.
107 108 109 |
# File 'lib/polars/list_expr.rb', line 107 def drop_nulls Utils.wrap_expr(_rbexpr.list_drop_nulls) end |
#eval(expr) ⇒ Expr
Run any polars expression against the lists' elements.
1040 1041 1042 |
# File 'lib/polars/list_expr.rb', line 1040 def eval(expr) Utils.wrap_expr(_rbexpr.list_eval(expr._rbexpr)) end |
#explode(empty_as_null: true, keep_nulls: true) ⇒ Expr
Returns a column with a separate row for every list element.
911 912 913 |
# File 'lib/polars/list_expr.rb', line 911 def explode(empty_as_null: true, keep_nulls: true) Utils.wrap_expr(_rbexpr.explode(empty_as_null, keep_nulls)) end |
#filter(predicate) ⇒ Expr
Filter elements in each list by a boolean expression.
1107 1108 1109 |
# File 'lib/polars/list_expr.rb', line 1107 def filter(predicate) Utils.wrap_expr(_rbexpr.list_filter(predicate._rbexpr)) end |
#first ⇒ Expr
Get the first value of the sublists.
597 598 599 |
# File 'lib/polars/list_expr.rb', line 597 def first get(0, null_on_oob: true) end |
#gather(indices, 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.
532 533 534 535 |
# File 'lib/polars/list_expr.rb', line 532 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.
570 571 572 573 574 575 576 577 |
# File 'lib/polars/list_expr.rb', line 570 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: false) ⇒ 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.
491 492 493 494 |
# File 'lib/polars/list_expr.rb', line 491 def get(index, null_on_oob: false) 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.
859 860 861 |
# File 'lib/polars/list_expr.rb', line 859 def head(n = 5) slice(0, n) end |
#item(allow_empty: false) ⇒ Expr
Get the single value of the sublists.
This errors if the sublist length is not exactly one.
661 662 663 |
# File 'lib/polars/list_expr.rb', line 661 def item(allow_empty: false) agg(F.element.item(allow_empty: allow_empty)) 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.
716 717 718 719 |
# File 'lib/polars/list_expr.rb', line 716 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.
619 620 621 |
# File 'lib/polars/list_expr.rb', line 619 def last get(-1, null_on_oob: true) end |
#len ⇒ Expr
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.
199 200 201 |
# File 'lib/polars/list_expr.rb', line 199 def max Utils.wrap_expr(_rbexpr.list_max) end |
#mean ⇒ Expr
Compute the mean value of the lists in the array.
241 242 243 |
# File 'lib/polars/list_expr.rb', line 241 def mean Utils.wrap_expr(_rbexpr.list_mean) end |
#median ⇒ Expr
Compute the median value of the lists in the array.
262 263 264 |
# File 'lib/polars/list_expr.rb', line 262 def median Utils.wrap_expr(_rbexpr.list_median) end |
#min ⇒ Expr
Compute the min value of the lists in the array.
220 221 222 |
# File 'lib/polars/list_expr.rb', line 220 def min Utils.wrap_expr(_rbexpr.list_min) end |
#n_unique ⇒ Expr
Count the number of unique values in every sub-lists.
418 419 420 |
# File 'lib/polars/list_expr.rb', line 418 def n_unique Utils.wrap_expr(_rbexpr.list_n_unique) end |
#reverse ⇒ Expr
Reverse the arrays in the list.
369 370 371 |
# File 'lib/polars/list_expr.rb', line 369 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.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/polars/list_expr.rb', line 141 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.
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 |
# File 'lib/polars/list_expr.rb', line 1179 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.
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 |
# File 'lib/polars/list_expr.rb', line 1218 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.
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 |
# File 'lib/polars/list_expr.rb', line 1257 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.
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 |
# File 'lib/polars/list_expr.rb', line 1140 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.
811 812 813 814 |
# File 'lib/polars/list_expr.rb', line 811 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.
836 837 838 839 840 |
# File 'lib/polars/list_expr.rb', line 836 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(descending: false, nulls_last: false) ⇒ Expr
Sort the arrays in the list.
344 345 346 |
# File 'lib/polars/list_expr.rb', line 344 def sort(descending: false, nulls_last: false) Utils.wrap_expr(_rbexpr.list_sort(descending, nulls_last)) end |
#std(ddof: 1) ⇒ Expr
Compute the std value of the lists in the array.
288 289 290 |
# File 'lib/polars/list_expr.rb', line 288 def std(ddof: 1) Utils.wrap_expr(_rbexpr.list_std(ddof)) end |
#sum ⇒ Expr
Sum all the lists in the array.
178 179 180 |
# File 'lib/polars/list_expr.rb', line 178 def sum Utils.wrap_expr(_rbexpr.list_sum) end |
#tail(n = 5) ⇒ Expr
Slice the last n values of every sublist.
880 881 882 883 |
# File 'lib/polars/list_expr.rb', line 880 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.
965 966 967 |
# File 'lib/polars/list_expr.rb', line 965 def to_array(width) Utils.wrap_expr(_rbexpr.list_to_array(width)) end |
#to_struct(n_field_strategy: nil, fields: nil, upper_bound: nil) ⇒ Expr
Convert the series of type List to a series of type Struct.
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 |
# File 'lib/polars/list_expr.rb', line 1004 def to_struct(n_field_strategy: nil, fields: nil, upper_bound: nil) if !fields.is_a?(::Array) if fields.nil? fields = upper_bound.times.map { |i| "field_#{i}" } else fields = upper_bound.times.map { |i| fields.(i) } end end Utils.wrap_expr(_rbexpr.list_to_struct(fields)) end |
#unique(maintain_order: false) ⇒ Expr
Get the unique/distinct values in the list.
393 394 395 |
# File 'lib/polars/list_expr.rb', line 393 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.
314 315 316 |
# File 'lib/polars/list_expr.rb', line 314 def var(ddof: 1) Utils.wrap_expr(_rbexpr.list_var(ddof)) end |