Class: Polars::ListNameSpace

Inherits:
Object
  • Object
show all
Defined in:
lib/polars/list_name_space.rb

Overview

Series.arr namespace.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Polars::ExprDispatch

Instance Method Details

#[](item) ⇒ Series

Get the value by index in the sublists.

Returns:



107
108
109
# File 'lib/polars/list_name_space.rb', line 107

def [](item)
  get(item)
end

#arg_maxSeries

Retrieve the index of the maximum value in every sublist.

Returns:



168
169
170
# File 'lib/polars/list_name_space.rb', line 168

def arg_max
  super
end

#arg_minSeries

Retrieve the index of the minimal value in every sublist.

Returns:



161
162
163
# File 'lib/polars/list_name_space.rb', line 161

def arg_min
  super
end

#concat(other) ⇒ Series

Concat the arrays in a Series dtype List in linear time.

Parameters:

  • other (Object)

    Columns to concat into a List Series

Returns:



86
87
88
# File 'lib/polars/list_name_space.rb', line 86

def concat(other)
  super
end

#contains(item) ⇒ Series

Check if sublists contain the given item.

Parameters:

  • item (Object)

    Item that will be checked for membership.

Returns:



154
155
156
# File 'lib/polars/list_name_space.rb', line 154

def contains(item)
  super
end

#diff(n: 1, null_behavior: "ignore") ⇒ Series

Calculate the n-th discrete difference of every sublist.

Examples:

s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
s.arr.diff
# =>
# shape: (2,)
# Series: 'a' [list]
# [
#         [null, 1, ... 1]
#         [null, -8, -1]
# ]

Parameters:

  • n (Integer) (defaults to: 1)

    Number of slots to shift.

  • null_behavior ("ignore", "drop") (defaults to: "ignore")

    How to handle null values.

Returns:



191
192
193
# File 'lib/polars/list_name_space.rb', line 191

def diff(n: 1, null_behavior: "ignore")
  super
end

#eval(expr, parallel: false) ⇒ Series

Run any polars expression against the lists' elements.

Examples:

df = Polars::DataFrame.new({"a" => [1, 8, 3], "b" => [4, 5, 2]})
df.with_column(
  Polars.concat_list(["a", "b"]).arr.eval(Polars.element.rank).alias("rank")
)
# =>
# shape: (3, 3)
# ┌─────┬─────┬────────────┐
# │ a   ┆ b   ┆ rank       │
# │ --- ┆ --- ┆ ---        │
# │ i64 ┆ i64 ┆ list[f32]  │
# ╞═════╪═════╪════════════╡
# │ 1   ┆ 4   ┆ [1.0, 2.0] │
# ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 8   ┆ 5   ┆ [2.0, 1.0] │
# ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ 3   ┆ 2   ┆ [2.0, 1.0] │
# └─────┴─────┴────────────┘

Parameters:

  • expr (Expr)

    Expression to run. Note that you can select an element with Polars.first, or Polars.col

  • parallel (Boolean) (defaults to: false)

    Run all expression parallel. Don't activate this blindly. Parallelism is worth it if there is enough work to do per thread.

    This likely should not be use in the groupby context, because we already parallel execution per group

Returns:



342
343
344
# File 'lib/polars/list_name_space.rb', line 342

def eval(expr, parallel: false)
  super
end

#firstSeries

Get the first value of the sublists.

Returns:



137
138
139
# File 'lib/polars/list_name_space.rb', line 137

def first
  super
end

#get(index) ⇒ Series

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.

Parameters:

  • index (Integer)

    Index to return per sublist

Returns:



100
101
102
# File 'lib/polars/list_name_space.rb', line 100

def get(index)
  super
end

#head(n = 5) ⇒ Series

Slice the first n values of every sublist.

Examples:

s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
s.arr.head(2)
# =>
# shape: (2,)
# Series: 'a' [list]
# [
#         [1, 2]
#         [10, 2]
# ]

Parameters:

  • n (Integer) (defaults to: 5)

    Number of values to return for each sublist.

Returns:



257
258
259
# File 'lib/polars/list_name_space.rb', line 257

def head(n = 5)
  super
end

#join(separator) ⇒ Series

Join all string items in a sublist and place a separator between them.

This errors if inner type of list != Utf8.

Examples:

s = Polars::Series.new([["foo", "bar"], ["hello", "world"]])
s.arr.join("-")
# =>
# shape: (2,)
# Series: '' [str]
# [
#     "foo-bar"
#     "hello-world"
# ]

Parameters:

  • separator (String)

    string to separate the items with

Returns:



130
131
132
# File 'lib/polars/list_name_space.rb', line 130

def join(separator)
  super
end

#lastSeries

Get the last value of the sublists.

Returns:



144
145
146
# File 'lib/polars/list_name_space.rb', line 144

def last
  super
end

#lengthsSeries

Get the length of the arrays as UInt32.

Examples:

s = Polars::Series.new([[1, 2, 3], [5]])
s.arr.lengths
# =>
# shape: (2,)
# Series: '' [u32]
# [
#         3
#         1
# ]

Returns:



27
28
29
# File 'lib/polars/list_name_space.rb', line 27

def lengths
  super
end

#maxSeries

Compute the max value of the arrays in the list.

Returns:



41
42
43
# File 'lib/polars/list_name_space.rb', line 41

def max
  super
end

#meanSeries

Compute the mean value of the arrays in the list.

Returns:



55
56
57
# File 'lib/polars/list_name_space.rb', line 55

def mean
  super
end

#minSeries

Compute the min value of the arrays in the list.

Returns:



48
49
50
# File 'lib/polars/list_name_space.rb', line 48

def min
  super
end

#reverseSeries

Reverse the arrays in the list.

Returns:



69
70
71
# File 'lib/polars/list_name_space.rb', line 69

def reverse
  super
end

#shift(periods = 1) ⇒ Series

Shift values by the given period.

Examples:

s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
s.arr.shift
# =>
# shape: (2,)
# Series: 'a' [list]
# [
#         [null, 1, ... 3]
#         [null, 10, 2]
# ]

Parameters:

  • periods (Integer) (defaults to: 1)

    Number of places to shift (may be negative).

Returns:



212
213
214
# File 'lib/polars/list_name_space.rb', line 212

def shift(periods = 1)
  super
end

#slice(offset, length = nil) ⇒ Series

Slice every sublist.

Examples:

s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
s.arr.slice(1, 2)
# =>
# shape: (2,)
# Series: 'a' [list]
# [
#         [2, 3]
#         [2, 1]
# ]

Parameters:

  • offset (Integer)

    Start index. Negative indexing is supported.

  • length (Integer) (defaults to: nil)

    Length of the slice. If set to nil (default), the slice is taken to the end of the list.

Returns:



236
237
238
# File 'lib/polars/list_name_space.rb', line 236

def slice(offset, length = nil)
  super
end

#sort(reverse: false) ⇒ Series

Sort the arrays in the list.

Returns:



62
63
64
# File 'lib/polars/list_name_space.rb', line 62

def sort(reverse: false)
  super
end

#sumSeries

Sum all the arrays in the list.

Returns:



34
35
36
# File 'lib/polars/list_name_space.rb', line 34

def sum
  super
end

#tail(n = 5) ⇒ Series

Slice the last n values of every sublist.

Examples:

s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
s.arr.tail(2)
# =>
# shape: (2,)
# Series: 'a' [list]
# [
#         [3, 4]
#         [2, 1]
# ]

Parameters:

  • n (Integer) (defaults to: 5)

    Number of values to return for each sublist.

Returns:



278
279
280
# File 'lib/polars/list_name_space.rb', line 278

def tail(n = 5)
  super
end

#to_struct(n_field_strategy: "first_non_null", name_generator: nil) ⇒ Series

Convert the series of type List to a series of type Struct.

Examples:

df = Polars::DataFrame.new({"a" => [[1, 2, 3], [1, 2]]})
df.select([Polars.col("a").arr.to_struct])
# =>
# shape: (2, 1)
# ┌────────────┐
# │ a          │
# │ ---        │
# │ struct[3]  │
# ╞════════════╡
# │ {1,2,3}    │
# ├╌╌╌╌╌╌╌╌╌╌╌╌┤
# │ {1,2,null} │
# └────────────┘

Parameters:

  • n_field_strategy ("first_non_null", "max_width") (defaults to: "first_non_null")

    Strategy to determine the number of fields of the struct.

  • name_generator (Object) (defaults to: nil)

    A custom function that can be used to generate the field names. Default field names are field_0, field_1 .. field_n

Returns:



306
307
308
# File 'lib/polars/list_name_space.rb', line 306

def to_struct(n_field_strategy: "first_non_null", name_generator: nil)
  super
end

#uniqueSeries

Get the unique/distinct values in the list.

Returns:



76
77
78
# File 'lib/polars/list_name_space.rb', line 76

def unique
  super
end