Class: Polars::ArrayExpr
- Inherits:
-
Object
- Object
- Polars::ArrayExpr
- Defined in:
- lib/polars/array_expr.rb
Overview
Namespace for array related expressions.
Instance Method Summary collapse
-
#agg(expr) ⇒ Expr
Run any polars aggregation expression against the arrays' elements.
-
#all(ignore_nulls: true) ⇒ Expr
Evaluate whether all boolean values are true for every subarray.
-
#any(ignore_nulls: true) ⇒ Expr
Evaluate whether any boolean value is true for every subarray.
-
#arg_max ⇒ Expr
Retrieve the index of the maximum value in every sub-array.
-
#arg_min ⇒ Expr
Retrieve the index of the minimal value in every sub-array.
-
#contains(item, nulls_equal: true) ⇒ Expr
Check if sub-arrays contain the given item.
-
#count_matches(element) ⇒ Expr
Count how often the value produced by
elementoccurs. -
#eval(expr, as_list: false) ⇒ Expr
Run any polars expression against the arrays' elements.
-
#explode(empty_as_null: true, keep_nulls: true) ⇒ Expr
Returns a column with a separate row for every array element.
-
#first ⇒ Expr
Get the first value of the sub-arrays.
-
#get(index, null_on_oob: false) ⇒ Expr
Get the value by index in the sub-arrays.
-
#head(n = 5, as_array: false) ⇒ Expr
Get the first
nelements of the sub-arrays. -
#join(separator, ignore_nulls: true) ⇒ Expr
Join all string items in a sub-array and place a separator between them.
-
#last ⇒ Expr
Get the last value of the sub-arrays.
-
#len ⇒ Expr
Return the number of elements in each array.
-
#max ⇒ Expr
Compute the max values of the sub-arrays.
-
#mean ⇒ Expr
Compute the mean of the values of the sub-arrays.
-
#median ⇒ Expr
Compute the median of the values of the sub-arrays.
-
#min ⇒ Expr
Compute the min values of the sub-arrays.
-
#n_unique ⇒ Expr
Count the number of unique values in every sub-arrays.
-
#reverse ⇒ Expr
Reverse the arrays in this column.
-
#shift(n = 1) ⇒ Expr
Shift array values by the given number of indices.
-
#slice(offset, length = nil, as_array: false) ⇒ Expr
Slice every subarray.
-
#sort(descending: false, nulls_last: false) ⇒ Expr
Sort the arrays in this column.
-
#std(ddof: 1) ⇒ Expr
Compute the std of the values of the sub-arrays.
-
#sum ⇒ Expr
Compute the sum values of the sub-arrays.
-
#tail(n = 5, as_array: false) ⇒ Expr
Slice the last
nvalues of every sublist. -
#to_list ⇒ Expr
Convert an Array column into a List column with the same inner data type.
-
#to_struct(fields: nil) ⇒ Expr
Convert the Series of type
Arrayto a Series of typeStruct. -
#unique(maintain_order: false) ⇒ Expr
Get the unique/distinct values in the array.
-
#var(ddof: 1) ⇒ Expr
Compute the var of the values of the sub-arrays.
Instance Method Details
#agg(expr) ⇒ Expr
Run any polars aggregation expression against the arrays' elements.
1011 1012 1013 |
# File 'lib/polars/array_expr.rb', line 1011 def agg(expr) Utils.wrap_expr(_rbexpr.arr_agg(expr._rbexpr)) end |
#all(ignore_nulls: true) ⇒ Expr
Evaluate whether all boolean values are true for every subarray.
515 516 517 |
# File 'lib/polars/array_expr.rb', line 515 def all(ignore_nulls: true) agg(F.element.all(ignore_nulls: ignore_nulls)) end |
#any(ignore_nulls: true) ⇒ Expr
Evaluate whether any boolean value is true for every subarray.
473 474 475 |
# File 'lib/polars/array_expr.rb', line 473 def any(ignore_nulls: true) agg(F.element.any(ignore_nulls: ignore_nulls)) end |
#arg_max ⇒ Expr
Retrieve the index of the maximum value in every sub-array.
637 638 639 |
# File 'lib/polars/array_expr.rb', line 637 def arg_max Utils.wrap_expr(_rbexpr.arr_arg_max) end |
#arg_min ⇒ Expr
Retrieve the index of the minimal value in every sub-array.
611 612 613 |
# File 'lib/polars/array_expr.rb', line 611 def arg_min Utils.wrap_expr(_rbexpr.arr_arg_min) end |
#contains(item, nulls_equal: true) ⇒ Expr
Check if sub-arrays contain the given item.
824 825 826 827 |
# File 'lib/polars/array_expr.rb', line 824 def contains(item, nulls_equal: true) item = Utils.parse_into_expression(item, str_as_lit: true) Utils.wrap_expr(_rbexpr.arr_contains(item, nulls_equal)) end |
#count_matches(element) ⇒ Expr
Count how often the value produced by element occurs.
852 853 854 855 |
# File 'lib/polars/array_expr.rb', line 852 def count_matches(element) element = Utils.parse_into_expression(element, str_as_lit: true) Utils.wrap_expr(_rbexpr.arr_count_matches(element)) end |
#eval(expr, as_list: false) ⇒ Expr
Run any polars expression against the arrays' elements.
970 971 972 |
# File 'lib/polars/array_expr.rb', line 970 def eval(expr, as_list: false) Utils.wrap_expr(_rbexpr.arr_eval(expr._rbexpr, as_list)) end |
#explode(empty_as_null: true, keep_nulls: true) ⇒ Expr
Returns a column with a separate row for every array element.
794 795 796 |
# File 'lib/polars/array_expr.rb', line 794 def explode(empty_as_null: true, keep_nulls: true) Utils.wrap_expr(_rbexpr.explode(empty_as_null, keep_nulls)) end |
#first ⇒ Expr
Get the first value of the sub-arrays.
699 700 701 |
# File 'lib/polars/array_expr.rb', line 699 def first get(0, null_on_oob: true) end |
#get(index, null_on_oob: false) ⇒ Expr
Get the value by index in the sub-arrays.
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.
673 674 675 676 |
# File 'lib/polars/array_expr.rb', line 673 def get(index, null_on_oob: false) index = Utils.parse_into_expression(index) Utils.wrap_expr(_rbexpr.arr_get(index, null_on_oob)) end |
#head(n = 5, as_array: false) ⇒ Expr
Get the first n elements of the sub-arrays.
135 136 137 |
# File 'lib/polars/array_expr.rb', line 135 def head(n = 5, as_array: false) slice(0, n, as_array: as_array) end |
#join(separator, ignore_nulls: true) ⇒ Expr
Join all string items in a sub-array and place a separator between them.
This errors if inner type of array != String.
761 762 763 764 |
# File 'lib/polars/array_expr.rb', line 761 def join(separator, ignore_nulls: true) separator = Utils.parse_into_expression(separator, str_as_lit: true) Utils.wrap_expr(_rbexpr.arr_join(separator, ignore_nulls)) end |
#last ⇒ Expr
Get the last value of the sub-arrays.
724 725 726 |
# File 'lib/polars/array_expr.rb', line 724 def last get(-1, null_on_oob: true) end |
#len ⇒ Expr
Return the number of elements in each array.
32 33 34 |
# File 'lib/polars/array_expr.rb', line 32 def len Utils.wrap_expr(_rbexpr.arr_len) end |
#max ⇒ Expr
Compute the max values of the sub-arrays.
231 232 233 |
# File 'lib/polars/array_expr.rb', line 231 def max Utils.wrap_expr(_rbexpr.arr_max) end |
#mean ⇒ Expr
Compute the mean of the values of the sub-arrays.
327 328 329 |
# File 'lib/polars/array_expr.rb', line 327 def mean Utils.wrap_expr(_rbexpr.arr_mean) end |
#median ⇒ Expr
Compute the median of the values of the sub-arrays.
351 352 353 |
# File 'lib/polars/array_expr.rb', line 351 def median Utils.wrap_expr(_rbexpr.arr_median) end |
#min ⇒ Expr
Compute the min values of the sub-arrays.
207 208 209 |
# File 'lib/polars/array_expr.rb', line 207 def min Utils.wrap_expr(_rbexpr.arr_min) end |
#n_unique ⇒ Expr
Count the number of unique values in every sub-arrays.
407 408 409 |
# File 'lib/polars/array_expr.rb', line 407 def n_unique agg(F.element.n_unique) end |
#reverse ⇒ Expr
Reverse the arrays in this column.
585 586 587 |
# File 'lib/polars/array_expr.rb', line 585 def reverse eval(F.element.reverse) end |
#shift(n = 1) ⇒ Expr
This method is similar to the LAG operation in SQL when the value for n
is positive. With a negative value for n, it is similar to LEAD.
Shift array values by the given number of indices.
941 942 943 944 |
# File 'lib/polars/array_expr.rb', line 941 def shift(n = 1) n = Utils.parse_into_expression(n) Utils.wrap_expr(_rbexpr.arr_shift(n)) end |
#slice(offset, length = nil, as_array: false) ⇒ Expr
Slice every subarray.
82 83 84 85 86 87 88 89 90 |
# File 'lib/polars/array_expr.rb', line 82 def slice( offset, length = nil, as_array: false ) offset = Utils.parse_into_expression(offset) length = !length.nil? ? Utils.parse_into_expression(length) : nil Utils.wrap_expr(_rbexpr.arr_slice(offset, length, as_array)) end |
#sort(descending: false, nulls_last: false) ⇒ Expr
Sort the arrays in this column.
559 560 561 |
# File 'lib/polars/array_expr.rb', line 559 def sort(descending: false, nulls_last: false) Utils.wrap_expr(_rbexpr.arr_sort(descending, nulls_last)) end |
#std(ddof: 1) ⇒ Expr
Compute the std of the values of the sub-arrays.
279 280 281 |
# File 'lib/polars/array_expr.rb', line 279 def std(ddof: 1) Utils.wrap_expr(_rbexpr.arr_std(ddof)) end |
#sum ⇒ Expr
Compute the sum values of the sub-arrays.
255 256 257 |
# File 'lib/polars/array_expr.rb', line 255 def sum Utils.wrap_expr(_rbexpr.arr_sum) end |
#tail(n = 5, as_array: false) ⇒ Expr
Slice the last n values of every sublist.
182 183 184 185 |
# File 'lib/polars/array_expr.rb', line 182 def tail(n = 5, as_array: false) n = Utils.parse_into_expression(n) Utils.wrap_expr(_rbexpr.arr_tail(n, as_array)) end |
#to_list ⇒ Expr
Convert an Array column into a List column with the same inner data type.
431 432 433 |
# File 'lib/polars/array_expr.rb', line 431 def to_list Utils.wrap_expr(_rbexpr.arr_to_list) end |
#to_struct(fields: nil) ⇒ Expr
Convert the Series of type Array to a Series of type Struct.
890 891 892 893 894 895 896 897 898 899 |
# File 'lib/polars/array_expr.rb', line 890 def to_struct(fields: nil) if fields.is_a?(Enumerable) field_names = fields.to_a rbexpr = _rbexpr.arr_to_struct(nil) Utils.wrap_expr(rbexpr).struct.rename_fields(field_names) else rbexpr = _rbexpr.arr_to_struct(fields) Utils.wrap_expr(rbexpr) end end |
#unique(maintain_order: false) ⇒ Expr
Get the unique/distinct values in the array.
379 380 381 382 383 |
# File 'lib/polars/array_expr.rb', line 379 def unique(maintain_order: false) eval( F.element.unique(maintain_order: maintain_order), as_list: true ) end |
#var(ddof: 1) ⇒ Expr
Compute the var of the values of the sub-arrays.
303 304 305 |
# File 'lib/polars/array_expr.rb', line 303 def var(ddof: 1) Utils.wrap_expr(_rbexpr.arr_var(ddof)) end |