Class: Polars::ArrayNameSpace

Inherits:
Object
  • Object
show all
Defined in:
lib/polars/array_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

#allSeries

Evaluate whether all boolean values are true for every subarray.

Examples:

s = Polars::Series.new(
  [[true, true], [false, true], [false, false], [nil, nil], nil],
  dtype: Polars::Array.new(Polars::Boolean, 2)
)
s.arr.all
# =>
# shape: (5,)
# Series: '' [bool]
# [
#         true
#         false
#         false
#         true
#         null
# ]

Returns:



381
382
383
# File 'lib/polars/array_name_space.rb', line 381

def all
  super
end

#anySeries

Evaluate whether any boolean value is true for every subarray.

Examples:

s = Polars::Series.new(
  [[true, true], [false, true], [false, false], [nil, nil], nil],
  dtype: Polars::Array.new(Polars::Boolean, 2)
)
s.arr.any
# =>
# shape: (5,)
# Series: '' [bool]
# [
#         true
#         true
#         false
#         false
#         null
# ]

Returns:



215
216
217
# File 'lib/polars/array_name_space.rb', line 215

def any
  super
end

#arg_maxSeries

Retrieve the index of the maximum value in every sub-array.

Examples:

s = Polars::Series.new("a", [[0, 9, 3], [9, 1, 2]], dtype: Polars::Array.new(Polars::Int64, 3))
s.arr.arg_max
# =>
# shape: (2,)
# Series: 'a' [u32]
# [
#         1
#         0
# ]

Returns:



468
469
470
# File 'lib/polars/array_name_space.rb', line 468

def arg_max
  super
end

#arg_minSeries

Retrieve the index of the minimal value in every sub-array.

Examples:

s = Polars::Series.new("a", [[3, 2, 1], [9, 1, 2]], dtype: Polars::Array.new(Polars::Int64, 3))
s.arr.arg_min
# =>
# shape: (2,)
# Series: 'a' [u32]
# [
#         2
#         1
# ]

Returns:



450
451
452
# File 'lib/polars/array_name_space.rb', line 450

def arg_min
  super
end

#contains(item) ⇒ Series

Check if sub-arrays contain the given item.

Examples:

s = Polars::Series.new(
  "a", [[3, 2, 1], [1, 2, 3], [4, 5, 6]], dtype: Polars::Array.new(Polars::Int32, 3)
)
s.arr.contains(1)
# =>
# shape: (3,)
# Series: 'a' [bool]
# [
#         true
#         true
#         false
# ]

Parameters:

  • item (Object)

    Item that will be checked for membership

Returns:



612
613
614
# File 'lib/polars/array_name_space.rb', line 612

def contains(item)
  super
end

#count_matches(element) ⇒ Series

Count how often the value produced by element occurs.

Examples:

s = Polars::Series.new("a", [[1, 2, 3], [2, 2, 2]], dtype: Polars::Array.new(Polars::Int64, 3))
s.arr.count_matches(2)
# =>
# shape: (2,)
# Series: 'a' [u32]
# [
#         1
#         3
# ]

Parameters:

  • element (Object)

    An expression that produces a single value

Returns:



633
634
635
# File 'lib/polars/array_name_space.rb', line 633

def count_matches(element)
  super
end

#explodeSeries

Returns a column with a separate row for every array element.

Examples:

s = Polars::Series.new("a", [[1, 2, 3], [4, 5, 6]], dtype: Polars::Array.new(Polars::Int64, 3))
s.arr.explode
# =>
# shape: (6,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         4
#         5
#         6
# ]

Returns:



588
589
590
# File 'lib/polars/array_name_space.rb', line 588

def explode
  super
end

#firstSeries

Get the first value of the sub-arrays.

Examples:

s = Polars::Series.new(
  "a", [[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype: Polars::Array.new(Polars::Int32, 3)
)
s.arr.first
# =>
# shape: (3,)
# Series: 'a' [i32]
# [
#         1
#         4
#         7
# ]

Returns:



517
518
519
# File 'lib/polars/array_name_space.rb', line 517

def first
  super
end

#get(index) ⇒ Series

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.

Examples:

s = Polars::Series.new(
  "a", [[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype: Polars::Array.new(Polars::Int32, 3)
)
s.arr.get(Polars::Series.new([1, -2, 4]))
# =>
# shape: (3,)
# Series: 'a' [i32]
# [
#         2
#         5
#         null
# ]

Parameters:

  • index (Integer)

    Index to return per sublist

Returns:



496
497
498
# File 'lib/polars/array_name_space.rb', line 496

def get(index)
  super
end

#head(n = 5, as_array: false) ⇒ Series

Get the first n elements of the sub-arrays.

Examples:

s = Polars::Series.new(
  [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]],
  dtype: Polars::Array.new(Polars::Int64, 6)
)
s.arr.head
# =>
# shape: (2,)
# Series: '' [list[i64]]
# [
#         [1, 2, … 5]
#         [7, 8, … 11]
# ]
s.arr.head(3, as_array: true)
# =>
# shape: (2,)
# Series: '' [array[i64, 3]]
# [
#         [1, 2, 3]
#         [7, 8, 9]
# ]

Parameters:

  • n (Integer) (defaults to: 5)

    Number of values to return for each sublist.

  • as_array (Boolean) (defaults to: false)

    Return result as a fixed-length Array, otherwise as a List. If true n must be a constant value.

Returns:



320
321
322
# File 'lib/polars/array_name_space.rb', line 320

def head(n = 5, as_array: false)
  super
end

#join(separator, ignore_nulls: true) ⇒ Series

Join all string items in a sub-array and place a separator between them.

This errors if inner type of array != String.

Examples:

s = Polars::Series.new([["x", "y"], ["a", "b"]], dtype: Polars::Array.new(Polars::String, 2))
s.arr.join("-")
# =>
# shape: (2,)
# Series: '' [str]
# [
#         "x-y"
#         "a-b"
# ]

Parameters:

  • separator (String)

    string to separate the items with

  • ignore_nulls (Boolean) (defaults to: true)

    Ignore null values (default).

    If set to False, null values will be propagated. If the sub-list contains any null values, the output is nil.

Returns:



566
567
568
# File 'lib/polars/array_name_space.rb', line 566

def join(separator, ignore_nulls: true)
  super
end

#lastSeries

Get the last value of the sub-arrays.

Examples:

s = Polars::Series.new(
  "a", [[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype: Polars::Array.new(Polars::Int32, 3)
)
s.arr.last
# =>
# shape: (3,)
# Series: 'a' [i32]
# [
#         3
#         6
#         9
# ]

Returns:



538
539
540
# File 'lib/polars/array_name_space.rb', line 538

def last
  super
end

#lenSeries

Return the number of elements in each array.

Examples:

s = Polars::Series.new("a", [[1, 2], [4, 3]], dtype: Polars::Array.new(Polars::Int8, 2))
s.arr.len
# =>
# shape: (2,)
# Series: 'a' [u32]
# [
#         2
#         2
# ]

Returns:



233
234
235
# File 'lib/polars/array_name_space.rb', line 233

def len
  super
end

#maxSeries

Compute the max values of the sub-arrays.

Examples:

s = Polars::Series.new(
  "a", [[1, 2], [4, 3]], dtype: Polars::Array.new(2, Polars::Int64)
)
s.arr.max
# =>
# shape: (2,)
# Series: 'a' [i64]
# [
#         2
#         4
# ]

Returns:



49
50
51
# File 'lib/polars/array_name_space.rb', line 49

def max
  super
end

#medianSeries

Compute the median of the values of the sub-arrays.

Examples:

s = Polars::Series.new("a", [[1, 2], [4, 3]], dtype: Polars::Array.new(Polars::Int64, 2))
s.arr.median
# =>
# shape: (2,)
# Series: 'a' [f64]
# [
#         1.5
#         3.5
# ]

Returns:



127
128
129
# File 'lib/polars/array_name_space.rb', line 127

def median
  super
end

#minSeries

Compute the min values of the sub-arrays.

Examples:

s = Polars::Series.new(
  "a", [[1, 2], [4, 3]], dtype: Polars::Array.new(2, Polars::Int64)
)
s.arr.min
# =>
# shape: (2,)
# Series: 'a' [i64]
# [
#         1
#         3
# ]

Returns:



29
30
31
# File 'lib/polars/array_name_space.rb', line 29

def min
  super
end

#n_uniqueSeries

Count the number of unique values in every sub-arrays.

Examples:

s = Polars::Series.new("a", [[1, 2], [4, 4]], dtype: Polars::Array.new(Polars::Int64, 2))
s.arr.n_unique
# =>
# shape: (2,)
# Series: 'a' [u32]
# [
#         2
#         1
# ]

Returns:



173
174
175
# File 'lib/polars/array_name_space.rb', line 173

def n_unique
  super
end

#reverseSeries

Reverse the arrays in this column.

Examples:

s = Polars::Series.new("a", [[3, 2, 1], [9, 1, 2]], dtype: Polars::Array.new(Polars::Int64, 3))
s.arr.reverse
# =>
# shape: (2,)
# Series: 'a' [array[i64, 3]]
# [
#         [1, 2, 3]
#         [2, 1, 9]
# ]

Returns:



432
433
434
# File 'lib/polars/array_name_space.rb', line 432

def reverse
  super
end

#shift(n = 1) ⇒ Series

Note:

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.

Examples:

By default, array values are shifted forward by one index.

s = Polars::Series.new([[1, 2, 3], [4, 5, 6]], dtype: Polars::Array.new(Polars::Int64, 3))
s.arr.shift
# =>
# shape: (2,)
# Series: '' [array[i64, 3]]
# [
#         [null, 1, 2]
#         [null, 4, 5]
# ]

Pass a negative value to shift in the opposite direction instead.

s.arr.shift(-2)
# =>
# shape: (2,)
# Series: '' [array[i64, 3]]
# [
#         [3, null, null]
#         [6, null, null]
# ]

Parameters:

  • n (Integer) (defaults to: 1)

    Number of indices to shift forward. If a negative value is passed, values are shifted in the opposite direction instead.

Returns:



700
701
702
# File 'lib/polars/array_name_space.rb', line 700

def shift(n = 1)
  super
end

#slice(offset, length = nil, as_array: false) ⇒ Series

Slice the sub-arrays.

Examples:

s = Polars::Series.new(
  [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]],
  dtype: Polars::Array.new(Polars::Int64, 6)
)
s.arr.slice(1)
# =>
# shape: (2,)
# Series: '' [list[i64]]
# [
#         [2, 3, … 6]
#         [8, 9, … 12]
# ]
s.arr.slice(1, 3, as_array: true)
# =>
# shape: (2,)
# Series: '' [array[i64, 3]]
# [
#         [2, 3, 4]
#         [8, 9, 10]
# ]
s.arr.slice(-2)
# =>
# shape: (2,)
# Series: '' [list[i64]]
# [
#         [5, 6]
#         [11, 12]
# ]

Parameters:

  • offset (Integer)

    The starting index of the slice.

  • length (Integer) (defaults to: nil)

    The length of the slice.

Returns:



279
280
281
282
283
284
285
# File 'lib/polars/array_name_space.rb', line 279

def slice(
  offset,
  length = nil,
  as_array: false
)
  super
end

#sort(descending: false, nulls_last: false) ⇒ Series

Sort the arrays in this column.

Examples:

s = Polars::Series.new("a", [[3, 2, 1], [9, 1, 2]], dtype: Polars::Array.new(Polars::Int64, 3))
s.arr.sort
# =>
# shape: (2,)
# Series: 'a' [array[i64, 3]]
# [
#         [1, 2, 3]
#         [1, 2, 9]
# ]
s.arr.sort(descending: true)
# =>
# shape: (2,)
# Series: 'a' [array[i64, 3]]
# [
#         [3, 2, 1]
#         [9, 2, 1]
# ]

Parameters:

  • descending (Boolean) (defaults to: false)

    Sort in descending order.

  • nulls_last (Boolean) (defaults to: false)

    Place null values last.

Returns:



414
415
416
# File 'lib/polars/array_name_space.rb', line 414

def sort(descending: false, nulls_last: false)
  super
end

#std(ddof: 1) ⇒ Series

Compute the std of the values of the sub-arrays.

Examples:

s = Polars::Series.new("a", [[1, 2], [4, 3]], dtype: Polars::Array.new(Polars::Int64, 2))
s.arr.std
# =>
# shape: (2,)
# Series: 'a' [f64]
# [
#         0.707107
#         0.707107
# ]

Returns:



91
92
93
# File 'lib/polars/array_name_space.rb', line 91

def std(ddof: 1)
  super
end

#sumSeries

Compute the sum values of the sub-arrays.

Examples:

df = Polars::DataFrame.new(
  {"a" => [[1, 2], [4, 3]]},
  schema: {"a" => Polars::Array.new(2, Polars::Int64)}
)
df.select(Polars.col("a").arr.sum)
# =>
# shape: (2, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 3   │
# │ 7   │
# └─────┘

Returns:



73
74
75
# File 'lib/polars/array_name_space.rb', line 73

def sum
  super
end

#tail(n = 5, as_array: false) ⇒ Series

Slice the last n values of every sublist.

Examples:

s = Polars::Series.new(
  [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]],
  dtype: Polars::Array.new(Polars::Int64, 6)
)
s.arr.tail
# =>
# shape: (2,)
# Series: '' [list[i64]]
# [
#         [2, 3, … 6]
#         [8, 9, … 12]
# ]
s.arr.tail(3, as_array: true)
# =>
# shape: (2,)
# Series: '' [array[i64, 3]]
# [
#         [4, 5, 6]
#         [10, 11, 12]
# ]

Parameters:

  • n (Integer) (defaults to: 5)

    Number of values to return for each sublist.

  • as_array (Boolean) (defaults to: false)

    Return result as a fixed-length Array, otherwise as a List. If true n must be a constant value.

Returns:



357
358
359
# File 'lib/polars/array_name_space.rb', line 357

def tail(n = 5, as_array: false)
  super
end

#to_listSeries

Convert an Array column into a List column with the same inner data type.

Examples:

s = Polars::Series.new([[1, 2], [3, 4]], dtype: Polars::Array.new(Polars::Int8, 2))
s.arr.to_list
# =>
# shape: (2,)
# Series: '' [list[i8]]
# [
#         [1, 2]
#         [3, 4]
# ]

Returns:



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

def to_list
  super
end

#to_struct(fields: nil) ⇒ Series

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

Examples:

Convert array to struct with default field name assignment:

s1 = Polars::Series.new("n", [[0, 1, 2], [3, 4, 5]], dtype: Polars::Array.new(Polars::Int8, 3))
s2 = s1.arr.to_struct
# =>
# shape: (2,)
# Series: 'n' [struct[3]]
# [
#         {0,1,2}
#         {3,4,5}
# ]
s2.struct.fields
# => ["field_0", "field_1", "field_2"]

Parameters:

  • fields (Object) (defaults to: nil)

    If the name and number of the desired fields is known in advance a list of field names can be given, which will be assigned by index. Otherwise, to dynamically assign field names, a custom function can be used; if neither are set, fields will be field_0, field_1 .. field_n.

Returns:



661
662
663
664
665
666
# File 'lib/polars/array_name_space.rb', line 661

def to_struct(
  fields: nil
)
  s = Utils.wrap_s(_s)
  s.to_frame.select(F.col(s.name).arr.to_struct(fields: fields)).to_series
end

#unique(maintain_order: false) ⇒ Series

Get the unique/distinct values in the array.

Examples:

df = Polars::DataFrame.new(
  {
    "a" => [[1, 1, 2]]
  },
  schema_overrides: {"a" => Polars::Array.new(Polars::Int64, 3)}
)
df.select(Polars.col("a").arr.unique)
# =>
# shape: (1, 1)
# ┌───────────┐
# │ a         │
# │ ---       │
# │ list[i64] │
# ╞═══════════╡
# │ [1, 2]    │
# └───────────┘

Parameters:

  • maintain_order (Boolean) (defaults to: false)

    Maintain order of data. This requires more work.

Returns:



155
156
157
# File 'lib/polars/array_name_space.rb', line 155

def unique(maintain_order: false)
  super
end

#var(ddof: 1) ⇒ Series

Compute the var of the values of the sub-arrays.

Examples:

s = Polars::Series.new("a", [[1, 2], [4, 3]], dtype: Polars::Array.new(Polars::Int64, 2))
s.arr.var
# =>
# shape: (2,)
# Series: 'a' [f64]
# [
#         0.5
#         0.5
# ]

Returns:



109
110
111
# File 'lib/polars/array_name_space.rb', line 109

def var(ddof: 1)
  super
end