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:



167
168
169
# File 'lib/polars/array_name_space.rb', line 167

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:



143
144
145
# File 'lib/polars/array_name_space.rb', line 143

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:



254
255
256
# File 'lib/polars/array_name_space.rb', line 254

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:



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

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:



398
399
400
# File 'lib/polars/array_name_space.rb', line 398

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:



419
420
421
# File 'lib/polars/array_name_space.rb', line 419

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:



374
375
376
# File 'lib/polars/array_name_space.rb', line 374

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:



303
304
305
# File 'lib/polars/array_name_space.rb', line 303

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:



282
283
284
# File 'lib/polars/array_name_space.rb', line 282

def get(index)
  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:



352
353
354
# File 'lib/polars/array_name_space.rb', line 352

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:



324
325
326
# File 'lib/polars/array_name_space.rb', line 324

def last
  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

#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

#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:



218
219
220
# File 'lib/polars/array_name_space.rb', line 218

def reverse
  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:



200
201
202
# File 'lib/polars/array_name_space.rb', line 200

def sort(descending: false, nulls_last: false)
  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

#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:



119
120
121
# File 'lib/polars/array_name_space.rb', line 119

def to_list
  super
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:



101
102
103
# File 'lib/polars/array_name_space.rb', line 101

def unique(maintain_order: false)
  super
end