Class: Polars::Series

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

Overview

A Series represents a single column in a polars DataFrame.

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, values = nil, dtype: nil, strict: true, nan_to_null: false, dtype_if_empty: nil) ⇒ Series

Create a new Series.

Examples:

Constructing a Series by specifying name and values positionally:

s = Polars::Series.new("a", [1, 2, 3])

Notice that the dtype is automatically inferred as a polars Int64:

s.dtype
# => Polars::Int64

Constructing a Series with a specific dtype:

s2 = Polars::Series.new("a", [1, 2, 3], dtype: :f32)

It is possible to construct a Series with values as the first positional argument. This syntax considered an anti-pattern, but it can be useful in certain scenarios. You must specify any other arguments through keywords.

s3 = Polars::Series.new([1, 2, 3])

Parameters:

  • name (String, Array, nil) (defaults to: nil)

    Name of the series. Will be used as a column name when used in a DataFrame. When not specified, name is set to an empty string.

  • values (Array, nil) (defaults to: nil)

    One-dimensional data in various forms. Supported are: Array and Series.

  • dtype (Symbol, nil) (defaults to: nil)

    Polars dtype of the Series data. If not specified, the dtype is inferred.

  • strict (Boolean) (defaults to: true)

    Throw error on numeric overflow.

  • nan_to_null (Boolean) (defaults to: false)

    Not used.

  • dtype_if_empty (Symbol, nil) (defaults to: nil)

    If no dtype is specified and values contains nil or an empty array, set the Polars dtype of the Series data. If not specified, Float32 is used.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/polars/series.rb', line 35

def initialize(name = nil, values = nil, dtype: nil, strict: true, nan_to_null: false, dtype_if_empty: nil)
  # Handle case where values are passed as the first argument
  if !name.nil? && !name.is_a?(String)
    if values.nil?
      values = name
      name = nil
    else
      raise ArgumentError, "Series name must be a string."
    end
  end

  name = "" if name.nil?

  # TODO improve
  if values.is_a?(Range) && values.begin.is_a?(String)
    values = values.to_a
  end

  if values.nil?
    self._s = sequence_to_rbseries(name, [], dtype: dtype, dtype_if_empty: dtype_if_empty)
  elsif values.is_a?(Series)
    self._s = series_to_rbseries(name, values)
  elsif values.is_a?(Range)
    self._s =
      Polars.arange(
        values.first,
        values.last + (values.exclude_end? ? 0 : 1),
        step: 1,
        eager: true,
        dtype: dtype
      )
      .rename(name, in_place: true)
      ._s
  elsif values.is_a?(Array)
    self._s = sequence_to_rbseries(name, values, dtype: dtype, strict: strict, dtype_if_empty: dtype_if_empty)
  else
    raise ArgumentError, "Series constructor called with unsupported type; got #{values.class.name}"
  end
end

Dynamic Method Handling

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

Instance Method Details

#!=(other) ⇒ Series

Not equal.

Returns:



175
176
177
# File 'lib/polars/series.rb', line 175

def !=(other)
  _comp(other, :neq)
end

#%(other) ⇒ Series

Returns the modulo.

Returns:



238
239
240
241
242
243
# File 'lib/polars/series.rb', line 238

def %(other)
  if is_datelike
    raise ArgumentError, "first cast to integer before applying modulo on datelike dtypes"
  end
  _arithmetic(other, :rem)
end

#&(other) ⇒ Series

Bitwise AND.

Returns:



138
139
140
141
142
143
# File 'lib/polars/series.rb', line 138

def &(other)
  if !other.is_a?(Series)
    other = Series.new([other])
  end
  Utils.wrap_s(_s.bitand(other._s))
end

#*(other) ⇒ Series

Performs multiplication.

Returns:



224
225
226
# File 'lib/polars/series.rb', line 224

def *(other)
  _arithmetic(other, :mul)
end

#**(power) ⇒ Series

Raises to the power of exponent.

Returns:



248
249
250
251
252
253
# File 'lib/polars/series.rb', line 248

def **(power)
  if is_datelike
    raise ArgumentError, "first cast to integer before raising datelike dtypes to a power"
  end
  to_frame.select(Polars.col(name).pow(power)).to_series
end

#+(other) ⇒ Series

Performs addition.

Returns:



210
211
212
# File 'lib/polars/series.rb', line 210

def +(other)
  _arithmetic(other, :add)
end

#-(other) ⇒ Series

Performs subtraction.

Returns:



217
218
219
# File 'lib/polars/series.rb', line 217

def -(other)
  _arithmetic(other, :sub)
end

#-@Series

Performs negation.

Returns:



258
259
260
# File 'lib/polars/series.rb', line 258

def -@
  0 - self
end

#/(other) ⇒ Series

Performs division.

Returns:



231
232
233
# File 'lib/polars/series.rb', line 231

def /(other)
  _arithmetic(other, :div)
end

#<(other) ⇒ Series

Less than.

Returns:



189
190
191
# File 'lib/polars/series.rb', line 189

def <(other)
  _comp(other, :lt)
end

#<=(other) ⇒ Series

Less than or equal.

Returns:



203
204
205
# File 'lib/polars/series.rb', line 203

def <=(other)
  _comp(other, :lt_eq)
end

#==(other) ⇒ Series

Equal.

Returns:



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

def ==(other)
  _comp(other, :eq)
end

#>(other) ⇒ Series

Greater than.

Returns:



182
183
184
# File 'lib/polars/series.rb', line 182

def >(other)
  _comp(other, :gt)
end

#>=(other) ⇒ Series

Greater than or equal.

Returns:



196
197
198
# File 'lib/polars/series.rb', line 196

def >=(other)
  _comp(other, :gt_eq)
end

#[](item) ⇒ Object

Returns elements of the Series.

Returns:

Raises:

  • (ArgumentError)


265
266
267
268
269
270
271
272
273
274
275
# File 'lib/polars/series.rb', line 265

def [](item)
  if item.is_a?(Integer)
    return _s.get_idx(item)
  end

  if item.is_a?(Range)
    return Slice.new(self).apply(item)
  end

  raise ArgumentError, "Cannot get item of type: #{item.class.name}"
end

#[]=(key, value) ⇒ Object

Sets an element of the Series.

Returns:



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/polars/series.rb', line 280

def []=(key, value)
  if value.is_a?(Array)
    if is_numeric || is_datelike
      set_at_idx(key, value)
      return
    end
    raise ArgumentError, "cannot set Series of dtype: #{dtype} with list/tuple as value; use a scalar value"
  end

  if key.is_a?(Series)
    if key.dtype == :bool
      self._s = set(key, value)._s
    elsif key.dtype == :u64
      self._s = set_at_idx(key.cast(:u32), value)._s
    elsif key.dtype == :u32
      self._s = set_at_idx(key, value)._s
    else
      raise Todo
    end
  end

  if key.is_a?(Array)
    s = Utils.wrap_s(sequence_to_rbseries("", key, dtype: :u32))
    self[s] = value
  elsif key.is_a?(Integer)
    # TODO fix
    # self[[key]] = value
    set_at_idx(key, value)
  else
    raise ArgumentError, "cannot use #{key} for indexing"
  end
end

#^(other) ⇒ Series

Bitwise XOR.

Returns:



158
159
160
161
162
163
# File 'lib/polars/series.rb', line 158

def ^(other)
  if !other.is_a?(Series)
    other = Series.new([other])
  end
  Utils.wrap_s(_s.bitxor(other._s))
end

#_hash(seed = 0, seed_1 = nil, seed_2 = nil, seed_3 = nil) ⇒ Series

Hash the Series.

The hash value is of type :u64.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s._hash(42)
# =>
# shape: (3,)
# Series: 'a' [u64]
# [
#         2374023516666777365
#         10386026231460783898
#         17796317186427479491
# ]

Parameters:

  • seed (Integer) (defaults to: 0)

    Random seed parameter. Defaults to 0.

  • seed_1 (Integer) (defaults to: nil)

    Random seed parameter. Defaults to seed if not set.

  • seed_2 (Integer) (defaults to: nil)

    Random seed parameter. Defaults to seed if not set.

  • seed_3 (Integer) (defaults to: nil)

    Random seed parameter. Defaults to seed if not set.

Returns:



3070
3071
3072
# File 'lib/polars/series.rb', line 3070

def _hash(seed = 0, seed_1 = nil, seed_2 = nil, seed_3 = nil)
  super
end

#absSeries

Compute absolute values.

Returns:



3111
3112
3113
# File 'lib/polars/series.rb', line 3111

def abs
  super
end

#alias(name) ⇒ Series

Return a copy of the Series with a new alias/name.

Examples:

s = Polars::Series.new("x", [1, 2, 3])
s.alias("y")

Parameters:

  • name (String)

    New name.

Returns:



783
784
785
786
787
# File 'lib/polars/series.rb', line 783

def alias(name)
  s = dup
  s._s.rename(name)
  s
end

#allBoolean

Check if all boolean values in the column are true.

Returns:



361
362
363
# File 'lib/polars/series.rb', line 361

def all
  to_frame.select(Polars.col(name).all).to_series[0]
end

#anyBoolean

Check if any boolean value in the column is true.

Returns:



354
355
356
# File 'lib/polars/series.rb', line 354

def any
  to_frame.select(Polars.col(name).any).to_series[0]
end

#append(other, append_chunks: true) ⇒ Series

Append a Series to this one.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s2 = Polars::Series.new("b", [4, 5, 6])
s.append(s2)
# =>
# shape: (6,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         4
#         5
#         6
# ]

Parameters:

  • other (Series)

    Series to append.

  • append_chunks (Boolean) (defaults to: true)

    If set to true the append operation will add the chunks from other to self. This is super cheap.

    If set to false the append operation will do the same as DataFrame#extend which extends the memory backed by this Series with the values from other.

    Different from append_chunks, extend appends the data from other to the underlying memory locations and thus may cause a reallocation (which is expensive).

    If this does not cause a reallocation, the resulting data structure will not have any extra chunks and thus will yield faster queries.

    Prefer extend over append_chunks when you want to do a query after a single append. For instance during online operations where you add n rows and rerun a query.

    Prefer append_chunks over extend when you want to append many times before doing a query. For instance, when you read in multiple files and when to store them in a single Series. In the latter case, finish the sequence of append_chunks operations with a rechunk.

Returns:



1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
# File 'lib/polars/series.rb', line 1036

def append(other, append_chunks: true)
  begin
    if append_chunks
      _s.append(other._s)
    else
      _s.extend(other._s)
    end
  rescue => e
    if e.message == "Already mutably borrowed"
      append(other.clone, append_chunks)
    else
      raise e
    end
  end
  self
end

#apply(return_dtype: nil, skip_nulls: true, &func) ⇒ Series

Apply a custom/user-defined function (UDF) over elements in this Series and return a new Series.

If the function returns another datatype, the return_dtype arg should be set, otherwise the method will fail.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.apply { |x| x + 10 }
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         11
#         12
#         13
# ]

Parameters:

  • return_dtype (Symbol) (defaults to: nil)

    Output datatype. If none is given, the same datatype as this Series will be used.

  • skip_nulls (Boolean) (defaults to: true)

    Nulls will be skipped and not passed to the Ruby function. This is faster because Ruby can be skipped and because we call more specialized functions.

Returns:



2371
2372
2373
2374
2375
2376
2377
2378
# File 'lib/polars/series.rb', line 2371

def apply(return_dtype: nil, skip_nulls: true, &func)
  if return_dtype.nil?
    pl_return_dtype = nil
  else
    pl_return_dtype = Utils.rb_type_to_dtype(return_dtype)
  end
  Utils.wrap_s(_s.apply_lambda(func, pl_return_dtype, skip_nulls))
end

#arccosSeries

Compute the element-wise value for the inverse cosine.

Examples:

s = Polars::Series.new("a", [1.0, 0.0, -1.0])
s.arccos
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         0.0
#         1.570796
#         3.141593
# ]

Returns:



2202
2203
2204
# File 'lib/polars/series.rb', line 2202

def arccos
  super
end

#arccoshSeries

Compute the element-wise value for the inverse hyperbolic cosine.

Examples:

s = Polars::Series.new("a", [5.0, 1.0, 0.0, -1.0])
s.arccosh
# =>
# shape: (4,)
# Series: 'a' [f64]
# [
#         2.292432
#         0.0
#         NaN
#         NaN
# ]

Returns:



2260
2261
2262
# File 'lib/polars/series.rb', line 2260

def arccosh
  super
end

#arcsinSeries

Compute the element-wise value for the inverse sine.

Examples:

s = Polars::Series.new("a", [1.0, 0.0, -1.0])
s.arcsin
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         1.570796
#         0.0
#         -1.570796
# ]

Returns:



2183
2184
2185
# File 'lib/polars/series.rb', line 2183

def arcsin
  super
end

#arcsinhSeries

Compute the element-wise value for the inverse hyperbolic sine.

Examples:

s = Polars::Series.new("a", [1.0, 0.0, -1.0])
s.arcsinh
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         0.881374
#         0.0
#         -0.881374
# ]

Returns:



2240
2241
2242
# File 'lib/polars/series.rb', line 2240

def arcsinh
  super
end

#arctanSeries

Compute the element-wise value for the inverse tangent.

Examples:

s = Polars::Series.new("a", [1.0, 0.0, -1.0])
s.arctan
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         0.785398
#         0.0
#         -0.785398
# ]

Returns:



2221
2222
2223
# File 'lib/polars/series.rb', line 2221

def arctan
  super
end

#arctanhSeries

Compute the element-wise value for the inverse hyperbolic tangent.

Examples:

s = Polars::Series.new("a", [2.0, 1.0, 0.5, 0.0, -0.5, -1.0, -1.1])
s.arctanh
# =>
# shape: (7,)
# Series: 'a' [f64]
# [
#         NaN
#         inf
#         0.549306
#         0.0
#         -0.549306
#         -inf
#         NaN
# ]

Returns:



2283
2284
2285
# File 'lib/polars/series.rb', line 2283

def arctanh
  super
end

#arg_maxInteger?

Get the index of the maximal value.

Examples:

s = Polars::Series.new("a", [3, 2, 1])
s.arg_max
# => 0

Returns:

  • (Integer, nil)


1271
1272
1273
# File 'lib/polars/series.rb', line 1271

def arg_max
  _s.arg_max
end

#arg_minInteger?

Get the index of the minimal value.

Examples:

s = Polars::Series.new("a", [3, 2, 1])
s.arg_min
# => 2

Returns:

  • (Integer, nil)


1259
1260
1261
# File 'lib/polars/series.rb', line 1259

def arg_min
  _s.arg_min
end

#arg_sort(reverse: false, nulls_last: false) ⇒ Series

Get the index values that would sort this Series.

Examples:

s = Polars::Series.new("a", [5, 3, 4, 1, 2])
s.arg_sort
# =>
# shape: (5,)
# Series: 'a' [u32]
# [
#         3
#         4
#         1
#         2
#         0
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    Sort in reverse (descending) order.

  • nulls_last (Boolean) (defaults to: false)

    Place null values last instead of first.

Returns:



1214
1215
1216
# File 'lib/polars/series.rb', line 1214

def arg_sort(reverse: false, nulls_last: false)
  super
end

#arg_trueSeries

Get index values where Boolean Series evaluate true.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
(s == 2).arg_true
# =>
# shape: (1,)
# Series: 'a' [u32]
# [
#         1
# ]

Returns:



1541
1542
1543
# File 'lib/polars/series.rb', line 1541

def arg_true
  Polars.arg_where(self, eager: true)
end

#arg_uniqueSeries

Get unique index as Series.

Examples:

s = Polars::Series.new("a", [1, 2, 2, 3])
s.arg_unique
# =>
# shape: (3,)
# Series: 'a' [u32]
# [
#         0
#         1
#         3
# ]

Returns:



1247
1248
1249
# File 'lib/polars/series.rb', line 1247

def arg_unique
  super
end

#argsort(reverse: false, nulls_last: false) ⇒ Series

Get the index values that would sort this Series.

Alias for #arg_sort.

Parameters:

  • reverse (Boolean) (defaults to: false)

    Sort in reverse (descending) order.

  • nulls_last (Boolean) (defaults to: false)

    Place null values last instead of first.

Returns:



1228
1229
1230
# File 'lib/polars/series.rb', line 1228

def argsort(reverse: false, nulls_last: false)
  super
end

#arrListNameSpace

Create an object namespace of all list related methods.

Returns:



3474
3475
3476
# File 'lib/polars/series.rb', line 3474

def arr
  ListNameSpace.new(self)
end

#cast(dtype, strict: true) ⇒ Series

Cast between data types.

Examples:

s = Polars::Series.new("a", [true, false, true])
s.cast(:u32)
# =>
# shape: (3,)
# Series: 'a' [u32]
# [
#         1
#         0
#         1
# ]

Parameters:

  • dtype (Symbol)

    DataType to cast to

  • strict (Boolean) (defaults to: true)

    Throw an error if a cast could not be done for instance due to an overflow

Returns:



1672
1673
1674
# File 'lib/polars/series.rb', line 1672

def cast(dtype, strict: true)
  super
end

#catCatNameSpace

Create an object namespace of all categorical related methods.

Returns:



3481
3482
3483
# File 'lib/polars/series.rb', line 3481

def cat
  CatNameSpace.new(self)
end

#ceilSeries

Rounds up to the nearest integer value.

Only works on floating point Series.

Examples:

s = Polars::Series.new("a", [1.12345, 2.56789, 3.901234])
s.ceil
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         2.0
#         3.0
#         4.0
# ]

Returns:



2022
2023
2024
# File 'lib/polars/series.rb', line 2022

def ceil
  super
end

#chunk_lengthsArray

Get the length of each individual chunk.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s2 = Polars::Series.new("b", [4, 5, 6])

Concatenate Series with rechunk: true

Polars.concat([s, s2]).chunk_lengths
# => [6]

Concatenate Series with rechunk: false

Polars.concat([s, s2], rechunk: false).chunk_lengths
# => [3, 3]

Returns:

  • (Array)


825
826
827
# File 'lib/polars/series.rb', line 825

def chunk_lengths
  _s.chunk_lengths
end

#clearedSeries

Create an empty copy of the current Series.

The copy has identical name/dtype but no data.

Examples:

s = Polars::Series.new("a", [nil, true, false])
s.cleared
# =>
# shape: (0,)
# Series: 'a' [bool]
# [
# ]

Returns:



1903
1904
1905
# File 'lib/polars/series.rb', line 1903

def cleared
  len > 0 ? limit(0) : clone
end

#clip(min_val, max_val) ⇒ Series

Clip (limit) the values in an array to a min and max boundary.

Only works for numerical types.

If you want to clip other dtypes, consider writing a "when, then, otherwise" expression. See LazyFunctions#when for more information.

Examples:

s = Polars::Series.new("foo", [-50, 5, nil, 50])
s.clip(1, 10)
# =>
# shape: (4,)
# Series: 'foo' [i64]
# [
#         1
#         5
#         null
#         10
# ]

Parameters:

  • min_val (Numeric)

    Minimum value.

  • max_val (Numeric)

    Maximum value.

Returns:



3295
3296
3297
# File 'lib/polars/series.rb', line 3295

def clip(min_val, max_val)
  super
end

#clip_max(max_val) ⇒ Series

Clip (limit) the values in an array to a max boundary.

Only works for numerical types.

If you want to clip other dtypes, consider writing a "when, then, otherwise" expression. See LazyFunctions#when for more information.

Parameters:

  • max_val (Numeric)

    Maximum value.

Returns:



3325
3326
3327
# File 'lib/polars/series.rb', line 3325

def clip_max(max_val)
  super
end

#clip_min(min_val) ⇒ Series

Clip (limit) the values in an array to a min boundary.

Only works for numerical types.

If you want to clip other dtypes, consider writing a "when, then, otherwise" expression. See LazyFunctions#when for more information.

Parameters:

  • min_val (Numeric)

    Minimum value.

Returns:



3310
3311
3312
# File 'lib/polars/series.rb', line 3310

def clip_min(min_val)
  super
end

#cosSeries

Compute the element-wise value for the cosine.

Examples:

s = Polars::Series.new("a", [0.0, Math::PI / 2.0, Math::PI])
s.cos
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         1.0
#         6.1232e-17
#         -1.0
# ]

Returns:



2145
2146
2147
# File 'lib/polars/series.rb', line 2145

def cos
  super
end

#coshSeries

Compute the element-wise value for the hyperbolic cosine.

Examples:

s = Polars::Series.new("a", [1.0, 0.0, -1.0])
s.cosh
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         1.543081
#         1.0
#         1.543081
# ]

Returns:



2321
2322
2323
# File 'lib/polars/series.rb', line 2321

def cosh
  super
end

#cummax(reverse: false) ⇒ Series

Get an array with the cumulative max computed at every element.

Examples:

s = Polars::Series.new("a", [3, 5, 1])
s.cummax
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         3
#         5
#         5
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    reverse the operation.

Returns:



914
915
916
# File 'lib/polars/series.rb', line 914

def cummax(reverse: false)
  super
end

#cummin(reverse: false) ⇒ Series

Get an array with the cumulative min computed at every element.

Examples:

s = Polars::Series.new("a", [3, 5, 1])
s.cummin
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         3
#         3
#         1
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    reverse the operation.

Returns:



892
893
894
# File 'lib/polars/series.rb', line 892

def cummin(reverse: false)
  super
end

#cumprod(reverse: false) ⇒ Series

Note:

Dtypes :i8, :u8, :i16, and :u16 are cast to :i64 before multiplying to prevent overflow issues.

Get an array with the cumulative product computed at every element.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.cumprod
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         1
#         2
#         6
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    reverse the operation.

Returns:



940
941
942
# File 'lib/polars/series.rb', line 940

def cumprod(reverse: false)
  super
end

#cumsum(reverse: false) ⇒ Series

Note:

Dtypes :i8, :u8, :i16, and :u16 are cast to :i64 before summing to prevent overflow issues.

Get an array with the cumulative sum computed at every element.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.cumsum
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         1
#         3
#         6
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    reverse the operation.

Returns:



870
871
872
# File 'lib/polars/series.rb', line 870

def cumsum(reverse: false)
  super
end

#cumulative_eval(expr, min_periods: 1, parallel: false) ⇒ Series

Note:

This functionality is experimental and may change without it being considered a breaking change.

Note:

This can be really slow as it can have O(n^2) complexity. Don't use this for operations that visit all elements.

Run an expression over a sliding window that increases 1 slot every iteration.

Examples:

s = Polars::Series.new("values", [1, 2, 3, 4, 5])
s.cumulative_eval(Polars.element.first - Polars.element.last ** 2)
# =>
# shape: (5,)
# Series: 'values' [f64]
# [
#         0.0
#         -3.0
#         -8.0
#         -15.0
#         -24.0
# ]

Parameters:

  • expr (Expr)

    Expression to evaluate

  • min_periods (Integer) (defaults to: 1)

    Number of valid values there should be in the window before the expression is evaluated. valid values = length - null_count

  • parallel (Boolean) (defaults to: false)

    Run in parallel. Don't do this in a groupby or another operation that already has much parallelization.

Returns:



769
770
771
# File 'lib/polars/series.rb', line 769

def cumulative_eval(expr, min_periods: 1, parallel: false)
  super
end

#describeDataFrame

Quick summary statistics of a series.

Series with mixed datatypes will return summary statistics for the datatype of the first value.

Examples:

series_num = Polars::Series.new([1, 2, 3, 4, 5])
series_num.describe
# =>
# shape: (6, 2)
# ┌────────────┬──────────┐
# │ statistic  ┆ value    │
# │ ---        ┆ ---      │
# │ str        ┆ f64      │
# ╞════════════╪══════════╡
# │ min        ┆ 1.0      │
# ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ max        ┆ 5.0      │
# ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ null_count ┆ 0.0      │
# ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ mean       ┆ 3.0      │
# ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ std        ┆ 1.581139 │
# ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
# │ count      ┆ 5.0      │
# └────────────┴──────────┘
series_str = Polars::Series.new(["a", "a", nil, "b", "c"])
series_str.describe
# =>
# shape: (3, 2)
# ┌────────────┬───────┐
# │ statistic  ┆ value │
# │ ---        ┆ ---   │
# │ str        ┆ i64   │
# ╞════════════╪═══════╡
# │ unique     ┆ 4     │
# ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
# │ null_count ┆ 1     │
# ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
# │ count      ┆ 5     │
# └────────────┴───────┘

Returns:



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/polars/series.rb', line 456

def describe
  if len == 0
    raise ArgumentError, "Series must contain at least one value"
  elsif is_numeric
    s = cast(:f64)
    stats = {
      "min" => s.min,
      "max" => s.max,
      "null_count" => s.null_count,
      "mean" => s.mean,
      "std" => s.std,
      "count" => s.len
    }
  elsif is_boolean
    stats = {
      "sum" => sum,
      "null_count" => null_count,
      "count" => len
    }
  elsif is_utf8
    stats = {
      "unique" => unique.length,
      "null_count" => null_count,
      "count" => len
    }
  elsif is_datelike
    # we coerce all to string, because a polars column
    # only has a single dtype and dates: datetime and count: int don't match
    stats = {
      "min" => dt.min.to_s,
      "max" => dt.max.to_s,
      "null_count" => null_count.to_s,
      "count" => len.to_s
    }
  else
    raise TypeError, "This type is not supported"
  end

  Polars::DataFrame.new(
    {"statistic" => stats.keys, "value" => stats.values}
  )
end

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

Calculate the n-th discrete difference.

Parameters:

  • n (Integer) (defaults to: 1)

    Number of slots to shift.

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

    How to handle null values.

Returns:



3179
3180
3181
# File 'lib/polars/series.rb', line 3179

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

#dot(other) ⇒ Numeric

Compute the dot/inner product between two Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s2 = Polars::Series.new("b", [4.0, 5.0, 6.0])
s.dot(s2)
# => 32.0

Parameters:

  • other (Object)

    Series (or array) to compute dot product with.

Returns:

  • (Numeric)


2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
# File 'lib/polars/series.rb', line 2060

def dot(other)
  if !other.is_a?(Series)
    other = Series.new(other)
  end
  if len != other.len
    n, m = len, other.len
    raise ArgumentError, "Series length mismatch: expected #{n}, found #{m}"
  end
  _s.dot(other._s)
end

#drop_nansSeries

Drop NaN values.

Returns:



399
400
401
# File 'lib/polars/series.rb', line 399

def drop_nans
  super
end

#drop_nullsSeries

Create a new Series that copies data from this Series without null values.

Returns:



392
393
394
# File 'lib/polars/series.rb', line 392

def drop_nulls
  super
end

#dtDateTimeNameSpace

Create an object namespace of all datetime related methods.

Returns:



3488
3489
3490
# File 'lib/polars/series.rb', line 3488

def dt
  DateTimeNameSpace.new(self)
end

#dtypeSymbol

Get the data type of this Series.

Returns:

  • (Symbol)


85
86
87
# File 'lib/polars/series.rb', line 85

def dtype
  _s.dtype
end

#entropy(base: Math::E, normalize: false) ⇒ Float?

Computes the entropy.

Uses the formula -sum(pk * log(pk) where pk are discrete probabilities.

Examples:

a = Polars::Series.new([0.99, 0.005, 0.005])
a.entropy(normalize: true)
# => 0.06293300616044681
b = Polars::Series.new([0.65, 0.10, 0.25])
b.entropy(normalize: true)
# => 0.8568409950394724

Parameters:

  • base (Float) (defaults to: Math::E)

    Given base, defaults to e

  • normalize (Boolean) (defaults to: false)

    Normalize pk if it doesn't sum to 1.

Returns:

  • (Float, nil)


731
732
733
# File 'lib/polars/series.rb', line 731

def entropy(base: Math::E, normalize: false)
  Polars.select(Polars.lit(self).entropy(base: base, normalize: normalize)).to_series[0]
end

#estimated_size(unit = "b") ⇒ Numeric

Return an estimation of the total (heap) allocated size of the Series.

Estimated size is given in the specified unit (bytes by default).

This estimation is the sum of the size of its buffers, validity, including nested arrays. Multiple arrays may share buffers and bitmaps. Therefore, the size of 2 arrays is not the sum of the sizes computed from this function. In particular, StructArray's size is an upper bound.

When an array is sliced, its allocated size remains constant because the buffer unchanged. However, this function will yield a smaller number. This is because this function returns the visible size of the buffer, not its total capacity.

FFI buffers are included in this estimation.

Examples:

s = Polars::Series.new("values", 1..1_000_000, dtype: :u32)
s.estimated_size
# => 4000000
s.estimated_size("mb")
# => 3.814697265625

Parameters:

  • unit ("b", "kb", "mb", "gb", "tb") (defaults to: "b")

    Scale the returned size to the given unit.

Returns:

  • (Numeric)


339
340
341
342
# File 'lib/polars/series.rb', line 339

def estimated_size(unit = "b")
  sz = _s.estimated_size
  Utils.scale_bytes(sz, to: unit)
end

#ewm_mean(com: nil, span: nil, half_life: nil, alpha: nil, adjust: true, min_periods: 1) ⇒ Series

Exponentially-weighted moving average.

Returns:



3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
# File 'lib/polars/series.rb', line 3365

def ewm_mean(
  com: nil,
  span: nil,
  half_life: nil,
  alpha: nil,
  adjust: true,
  min_periods: 1
)
  super
end

#ewm_std(com: nil, span: nil, half_life: nil, alpha: nil, adjust: true, bias: false, min_periods: 1) ⇒ Series

Exponentially-weighted moving standard deviation.

Returns:



3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
# File 'lib/polars/series.rb', line 3379

def ewm_std(
  com: nil,
  span: nil,
  half_life: nil,
  alpha: nil,
  adjust: true,
  bias: false,
  min_periods: 1
)
  super
end

#ewm_var(com: nil, span: nil, half_life: nil, alpha: nil, adjust: true, bias: false, min_periods: 1) ⇒ Series

Exponentially-weighted moving variance.

Returns:



3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
# File 'lib/polars/series.rb', line 3394

def ewm_var(
  com: nil,
  span: nil,
  half_life: nil,
  alpha: nil,
  adjust: true,
  bias: false,
  min_periods: 1
)
  super
end

#expSeries

Compute the exponential, element-wise.

Returns:



385
386
387
# File 'lib/polars/series.rb', line 385

def exp
  super
end

#explodeSeries

Explode a list or utf8 Series.

This means that every item is expanded to a new row.

Examples:

s = Polars::Series.new("a", [[1, 2], [3, 4], [9, 10]])
s.explode
# =>
# shape: (6,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         4
#         9
#         10
# ]

Returns:



1612
1613
1614
# File 'lib/polars/series.rb', line 1612

def explode
  super
end

#extend_constant(value, n) ⇒ Series

Extend the Series with given number of values.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.extend_constant(99, 2)
# =>
# shape: (5,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         99
#         99
# ]

Parameters:

  • value (Object)

    The value to extend the Series with. This value may be nil to fill with nulls.

  • n (Integer)

    The number of values to extend.

Returns:



3429
3430
3431
# File 'lib/polars/series.rb', line 3429

def extend_constant(value, n)
  super
end

#fill_nan(fill_value) ⇒ Series

Fill floating point NaN value with a fill value.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, Float::NAN])
s.fill_nan(0)
# =>
# shape: (4,)
# Series: 'a' [f64]
# [
#         1.0
#         2.0
#         3.0
#         0.0
# ]

Parameters:

  • fill_value (Object)

    Value used to fill nan values.

Returns:



1928
1929
1930
# File 'lib/polars/series.rb', line 1928

def fill_nan(fill_value)
  super
end

#fill_null(value = nil, strategy: nil, limit: nil) ⇒ Series

Fill null values using the specified value or strategy.

Examples:

s = Polars::Series.new("a", [1, 2, 3, nil])
s.fill_null(strategy: "forward")
# =>
# shape: (4,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         3
# ]
s.fill_null(strategy: "min")
# =>
# shape: (4,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         1
# ]
s = Polars::Series.new("b", ["x", nil, "z"])
s.fill_null(Polars.lit(""))
# =>
# shape: (3,)
# Series: 'b' [str]
# [
#         "x"
#         ""
#         "z"
# ]

Parameters:

  • value (Object) (defaults to: nil)

    Value used to fill null values.

  • strategy (nil, "forward", "backward", "min", "max", "mean", "zero", "one") (defaults to: nil)

    Strategy used to fill null values.

  • limit (defaults to: nil)

    Number of consecutive null values to fill when using the "forward" or "backward" strategy.

Returns:



1980
1981
1982
# File 'lib/polars/series.rb', line 1980

def fill_null(value = nil, strategy: nil, limit: nil)
  super
end

#filter(predicate) ⇒ Series

Filter elements by a boolean mask.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
mask = Polars::Series.new("", [true, false, true])
s.filter(mask)
# =>
# shape: (2,)
# Series: 'a' [i64]
# [
#         1
#         3
# ]

Parameters:

  • predicate (Series, Array)

    Boolean mask.

Returns:



1071
1072
1073
1074
1075
1076
# File 'lib/polars/series.rb', line 1071

def filter(predicate)
  if predicate.is_a?(Array)
    predicate = Series.new("", predicate)
  end
  Utils.wrap_s(_s.filter(predicate._s))
end

#flagsHash

Get flags that are set on the Series.

Returns:

  • (Hash)


92
93
94
95
96
97
# File 'lib/polars/series.rb', line 92

def flags
  {
    "SORTED_ASC" => _s.is_sorted_flag,
    "SORTED_DESC" => _s.is_sorted_reverse_flag
  }
end

#floorSeries

Rounds down to the nearest integer value.

Only works on floating point Series.

Examples:

s = Polars::Series.new("a", [1.12345, 2.56789, 3.901234])
s.floor
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         1.0
#         2.0
#         3.0
# ]

Returns:



2001
2002
2003
# File 'lib/polars/series.rb', line 2001

def floor
  Utils.wrap_s(_s.floor)
end

#has_validityBoolean

Return true if the Series has a validity bitmask.

If there is none, it means that there are no null values. Use this to swiftly assert a Series does not have null values.

Returns:



1341
1342
1343
# File 'lib/polars/series.rb', line 1341

def has_validity
  _s.has_validity
end

#head(n = 10) ⇒ Series

Get the first n rows.

Examples:

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

Parameters:

  • n (Integer) (defaults to: 10)

    Number of rows to return.

Returns:



1095
1096
1097
# File 'lib/polars/series.rb', line 1095

def head(n = 10)
  to_frame.select(Utils.col(name).head(n)).to_series
end

#inner_dtypeSymbol

Get the inner dtype in of a List typed Series.

Returns:

  • (Symbol)


102
103
104
# File 'lib/polars/series.rb', line 102

def inner_dtype
  _s.inner_dtype
end

#interpolate(method: "linear") ⇒ Series

Interpolate intermediate values. The interpolation method is linear.

Examples:

s = Polars::Series.new("a", [1, 2, nil, nil, 5])
s.interpolate
# =>
# shape: (5,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         4
#         5
# ]

Returns:



3104
3105
3106
# File 'lib/polars/series.rb', line 3104

def interpolate(method: "linear")
  super
end

#is_booleanBoolean Also known as: boolean?, is_bool, bool?

Check if this Series is a Boolean.

Examples:

s = Polars::Series.new("a", [true, false, true])
s.is_boolean
# => true

Returns:



1791
1792
1793
# File 'lib/polars/series.rb', line 1791

def is_boolean
  dtype == Boolean
end

#is_datelikeBoolean

Check if this Series datatype is datelike.

Examples:

s = Polars::Series.new([Date.new(2021, 1, 1), Date.new(2021, 1, 2), Date.new(2021, 1, 3)])
s.is_datelike
# => true

Returns:



1766
1767
1768
# File 'lib/polars/series.rb', line 1766

def is_datelike
  [Date, Datetime, Duration, Time].include?(dtype)
end

#is_duplicatedSeries

Get mask of all duplicated values.

Examples:

s = Polars::Series.new("a", [1, 2, 2, 3])
s.is_duplicated
# =>
# shape: (4,)
# Series: 'a' [bool]
# [
#         false
#         true
#         true
#         false
# ]

Returns:



1588
1589
1590
# File 'lib/polars/series.rb', line 1588

def is_duplicated
  super
end

#is_emptyBoolean Also known as: empty?

Check if the Series is empty.

Examples:

s = Polars::Series.new("a", [])
s.is_empty
# => true

Returns:



1353
1354
1355
# File 'lib/polars/series.rb', line 1353

def is_empty
  len == 0
end

#is_finiteSeries

Returns a boolean Series indicating which values are finite.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, Float::INFINITY])
s.is_finite
# =>
# shape: (3,)
# Series: 'a' [bool]
# [
#         true
#         true
#         false
# ]

Returns:



1413
1414
1415
# File 'lib/polars/series.rb', line 1413

def is_finite
  super
end

#is_firstSeries

Get a mask of the first unique value.

Returns:



1568
1569
1570
# File 'lib/polars/series.rb', line 1568

def is_first
  super
end

#is_floatBoolean Also known as: float?

Check if this Series has floating point numbers.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0])
s.is_float
# => true

Returns:



1778
1779
1780
# File 'lib/polars/series.rb', line 1778

def is_float
  [Float32, Float64].include?(dtype)
end

#is_in(other) ⇒ Series

Check if elements of this Series are in the other Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s2 = Polars::Series.new("b", [2, 4])
s2.is_in(s)
# =>
# shape: (2,)
# Series: 'b' [bool]
# [
#         true
#         false
# ]
sets = Polars::Series.new("sets", [[1, 2, 3], [1, 2], [9, 10]])
# =>
# shape: (3,)
# Series: 'sets' [list]
# [
#         [1, 2, 3]
#         [1, 2]
#         [9, 10]
# ]
optional_members = Polars::Series.new("optional_members", [1, 2, 3])
# =>
# shape: (3,)
# Series: 'optional_members' [i64]
# [
#         1
#         2
#         3
# ]
optional_members.is_in(sets)
# =>
# shape: (3,)
# Series: 'optional_members' [bool]
# [
#         true
#         true
#         false
# ]

Returns:



1524
1525
1526
# File 'lib/polars/series.rb', line 1524

def is_in(other)
  super
end

#is_infiniteSeries

Returns a boolean Series indicating which values are infinite.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, Float::INFINITY])
s.is_infinite
# =>
# shape: (3,)
# Series: 'a' [bool]
# [
#         false
#         false
#         true
# ]

Returns:



1432
1433
1434
# File 'lib/polars/series.rb', line 1432

def is_infinite
  super
end

#is_nanSeries

Returns a boolean Series indicating which values are NaN.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, Float::NAN])
s.is_nan
# =>
# shape: (4,)
# Series: 'a' [bool]
# [
#         false
#         false
#         false
#         true
# ]

Returns:



1452
1453
1454
# File 'lib/polars/series.rb', line 1452

def is_nan
  super
end

#is_not_nanSeries

Returns a boolean Series indicating which values are not NaN.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, Float::NAN])
s.is_not_nan
# =>
# shape: (4,)
# Series: 'a' [bool]
# [
#         true
#         true
#         true
#         false
# ]

Returns:



1472
1473
1474
# File 'lib/polars/series.rb', line 1472

def is_not_nan
  super
end

#is_not_nullSeries

Returns a boolean Series indicating which values are not null.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, nil])
s.is_not_null
# =>
# shape: (4,)
# Series: 'a' [bool]
# [
#         true
#         true
#         true
#         false
# ]

Returns:



1394
1395
1396
# File 'lib/polars/series.rb', line 1394

def is_not_null
  super
end

#is_nullSeries

Returns a boolean Series indicating which values are null.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, nil])
s.is_null
# =>
# shape: (4,)
# Series: 'a' [bool]
# [
#         false
#         false
#         false
#         true
# ]

Returns:



1374
1375
1376
# File 'lib/polars/series.rb', line 1374

def is_null
  super
end

#is_numericBoolean Also known as: numeric?

Check if this Series datatype is numeric.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.is_numeric
# => true

Returns:



1753
1754
1755
# File 'lib/polars/series.rb', line 1753

def is_numeric
  [Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Float32, Float64].include?(dtype)
end

#is_uniqueSeries

Get mask of all unique values.

Examples:

s = Polars::Series.new("a", [1, 2, 2, 3])
s.is_unique
# =>
# shape: (4,)
# Series: 'a' [bool]
# [
#         true
#         false
#         false
#         true
# ]

Returns:



1561
1562
1563
# File 'lib/polars/series.rb', line 1561

def is_unique
  super
end

#is_utf8Boolean Also known as: utf8?

Check if this Series datatype is a Utf8.

Examples:

s = Polars::Series.new("x", ["a", "b", "c"])
s.is_utf8
# => true

Returns:



1806
1807
1808
# File 'lib/polars/series.rb', line 1806

def is_utf8
  dtype == Utf8
end

#kurtosis(fisher: true, bias: true) ⇒ Float?

Compute the kurtosis (Fisher or Pearson) of a dataset.

Kurtosis is the fourth central moment divided by the square of the variance. If Fisher's definition is used, then 3.0 is subtracted from the result to give 0.0 for a normal distribution. If bias is false, then the kurtosis is calculated using k statistics to eliminate bias coming from biased moment estimators

Parameters:

  • fisher (Boolean) (defaults to: true)

    If true, Fisher's definition is used (normal ==> 0.0). If false, Pearson's definition is used (normal ==> 3.0).

  • bias (Boolean) (defaults to: true)

    If false, the calculations are corrected for statistical bias.

Returns:

  • (Float, nil)


3265
3266
3267
# File 'lib/polars/series.rb', line 3265

def kurtosis(fisher: true, bias: true)
  _s.kurtosis(fisher, bias)
end

#lenInteger Also known as: length

Length of this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.len
# => 3

Returns:

  • (Integer)


1647
1648
1649
# File 'lib/polars/series.rb', line 1647

def len
  _s.len
end

#limit(n = 10) ⇒ Series

Get the first n rows.

Alias for #head.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.limit(2)
# =>
# shape: (2,)
# Series: 'a' [i64]
# [
#         1
#         2
# ]

Parameters:

  • n (Integer) (defaults to: 10)

    Number of rows to return.

Returns:



963
964
965
# File 'lib/polars/series.rb', line 963

def limit(n = 10)
  to_frame.select(Utils.col(name).limit(n)).to_series
end

#log(base = Math::E) ⇒ Series

Compute the logarithm to a given base.

Parameters:

  • base (Float) (defaults to: Math::E)

    Given base, defaults to Math::E.

Returns:



371
372
373
# File 'lib/polars/series.rb', line 371

def log(base = Math::E)
  super
end

#log10Series

Compute the base 10 logarithm of the input array, element-wise.

Returns:



378
379
380
# File 'lib/polars/series.rb', line 378

def log10
  super
end

#maxObject

Get the maximum value in this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.max
# => 3

Returns:



554
555
556
# File 'lib/polars/series.rb', line 554

def max
  _s.max
end

#meanFloat?

Reduce this Series to the mean value.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.mean
# => 2.0

Returns:

  • (Float, nil)


523
524
525
# File 'lib/polars/series.rb', line 523

def mean
  _s.mean
end

#medianFloat?

Get the median of this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.median
# => 2.0

Returns:

  • (Float, nil)


620
621
622
# File 'lib/polars/series.rb', line 620

def median
  _s.median
end

#minObject

Get the minimal value in this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.min
# => 1

Returns:



542
543
544
# File 'lib/polars/series.rb', line 542

def min
  _s.min
end

#modeSeries

Compute the most occurring value(s).

Can return multiple Values.

Examples:

s = Polars::Series.new("a", [1, 2, 2, 3])
s.mode
# =>
# shape: (1,)
# Series: 'a' [i64]
# [
#         2
# ]

Returns:



2086
2087
2088
# File 'lib/polars/series.rb', line 2086

def mode
  super
end

#n_chunksInteger

Get the number of chunks that this Series contains.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s2 = Polars::Series.new("b", [4, 5, 6])

Concatenate Series with rechunk: true

Polars.concat([s, s2]).n_chunks
# => 1

Concatenate Series with rechunk: false

Polars.concat([s, s2], rechunk: false).n_chunks
# => 2

Returns:

  • (Integer)


844
845
846
# File 'lib/polars/series.rb', line 844

def n_chunks
  _s.n_chunks
end

#n_uniqueInteger

Count the number of unique values in this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 2, 3])
s.n_unique
# => 3

Returns:

  • (Integer)


3023
3024
3025
# File 'lib/polars/series.rb', line 3023

def n_unique
  _s.n_unique
end

#nameString

Get the name of this Series.

Returns:

  • (String)


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

def name
  _s.name
end

#nan_maxObject

Get maximum value, but propagate/poison encountered NaN values.

Returns:



561
562
563
# File 'lib/polars/series.rb', line 561

def nan_max
  to_frame.select(Polars.col(name).nan_max)[0, 0]
end

#nan_minObject

Get minimum value, but propagate/poison encountered NaN values.

Returns:



568
569
570
# File 'lib/polars/series.rb', line 568

def nan_min
  to_frame.select(Polars.col(name).nan_min)[0, 0]
end

#new_from_index(index, length) ⇒ Series

Create a new Series filled with values from the given index.

Returns:



3457
3458
3459
# File 'lib/polars/series.rb', line 3457

def new_from_index(index, length)
  Utils.wrap_s(_s.new_from_index(index, length))
end

#null_countInteger

Count the null values in this Series.

Returns:

  • (Integer)


1331
1332
1333
# File 'lib/polars/series.rb', line 1331

def null_count
  _s.null_count
end

#pct_change(n: 1) ⇒ Series

Computes percentage change between values.

Percentage change (as fraction) between current element and most-recent non-null element at least n period(s) before the current element.

Computes the change from the previous row by default.

Examples:

Polars::Series.new(0..9).pct_change
# =>
# shape: (10,)
# Series: '' [f64]
# [
#         null
#         inf
#         1.0
#         0.5
#         0.333333
#         0.25
#         0.2
#         0.166667
#         0.142857
#         0.125
# ]
Polars::Series.new([1, 2, 4, 8, 16, 32, 64, 128, 256, 512]).pct_change(n: 2)
# =>
# shape: (10,)
# Series: '' [f64]
# [
#         null
#         null
#         3.0
#         3.0
#         3.0
#         3.0
#         3.0
#         3.0
#         3.0
#         3.0
# ]

Parameters:

  • n (Integer) (defaults to: 1)

    periods to shift for forming percent change.

Returns:



3230
3231
3232
# File 'lib/polars/series.rb', line 3230

def pct_change(n: 1)
  super
end

#peak_maxSeries

Get a boolean mask of the local maximum peaks.

Examples:

s = Polars::Series.new("a", [1, 2, 3, 4, 5])
s.peak_max
# =>
# shape: (5,)
# Series: '' [bool]
# [
#         false
#         false
#         false
#         false
#         true
# ]

Returns:



2990
2991
2992
# File 'lib/polars/series.rb', line 2990

def peak_max
  Utils.wrap_s(_s.peak_max)
end

#peak_minSeries

Get a boolean mask of the local minimum peaks.

Examples:

s = Polars::Series.new("a", [4, 1, 3, 2, 5])
s.peak_min
# =>
# shape: (5,)
# Series: '' [bool]
# [
#         false
#         true
#         false
#         true
#         false
# ]

Returns:



3011
3012
3013
# File 'lib/polars/series.rb', line 3011

def peak_min
  Utils.wrap_s(_s.peak_min)
end

#productNumeric

Reduce this Series to the product value.

Returns:

  • (Numeric)


530
531
532
# File 'lib/polars/series.rb', line 530

def product
  to_frame.select(Polars.col(name).product).to_series[0]
end

#quantile(quantile, interpolation: "nearest") ⇒ Float?

Get the quantile value of this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.quantile(0.5)
# => 2.0

Parameters:

  • quantile (Float, nil)

    Quantile between 0.0 and 1.0.

  • interpolation ("nearest", "higher", "lower", "midpoint", "linear") (defaults to: "nearest")

    Interpolation method.

Returns:

  • (Float, nil)


637
638
639
# File 'lib/polars/series.rb', line 637

def quantile(quantile, interpolation: "nearest")
  _s.quantile(quantile, interpolation)
end

#rank(method: "average", reverse: false) ⇒ Series

Assign ranks to data, dealing with ties appropriately.

Examples:

The 'average' method:

s = Polars::Series.new("a", [3, 6, 1, 1, 6])
s.rank
# =>
# shape: (5,)
# Series: 'a' [f32]
# [
#         3.0
#         4.5
#         1.5
#         1.5
#         4.5
# ]

The 'ordinal' method:

s = Polars::Series.new("a", [3, 6, 1, 1, 6])
s.rank(method: "ordinal")
# =>
# shape: (5,)
# Series: 'a' [u32]
# [
#         3
#         4
#         1
#         2
#         5
# ]

Parameters:

  • method ("average", "min", "max", "dense", "ordinal", "random") (defaults to: "average")

    The method used to assign ranks to tied elements. The following methods are available (default is 'average'):

    • 'average' : The average of the ranks that would have been assigned to all the tied values is assigned to each value.
    • 'min' : The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as "competition" ranking.)
    • 'max' : The maximum of the ranks that would have been assigned to all the tied values is assigned to each value.
    • 'dense' : Like 'min', but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements.
    • 'ordinal' : All values are given a distinct rank, corresponding to the order that the values occur in the Series.
    • 'random' : Like 'ordinal', but the rank for ties is not dependent on the order that the values occur in the Series.
  • reverse (Boolean) (defaults to: false)

    Reverse the operation.

Returns:



3167
3168
3169
# File 'lib/polars/series.rb', line 3167

def rank(method: "average", reverse: false)
  super
end

#rechunk(in_place: false) ⇒ Series

Create a single chunk of memory for this Series.

Parameters:

  • in_place (Boolean) (defaults to: false)

    In place or not.

Returns:



1721
1722
1723
1724
# File 'lib/polars/series.rb', line 1721

def rechunk(in_place: false)
  opt_s = _s.rechunk(in_place)
  in_place ? self : Utils.wrap_s(opt_s)
end

#reinterpret(signed: true) ⇒ Series

Reinterpret the underlying bits as a signed/unsigned integer.

This operation is only allowed for 64bit integers. For lower bits integers, you can safely use that cast operation.

Parameters:

  • signed (Boolean) (defaults to: true)

    If true, reinterpret as :i64. Otherwise, reinterpret as :u64.

Returns:



3083
3084
3085
# File 'lib/polars/series.rb', line 3083

def reinterpret(signed: true)
  super
end

#rename(name, in_place: false) ⇒ Series

Rename this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.rename("b")

Parameters:

  • name (String)

    New name.

  • in_place (Boolean) (defaults to: false)

    Modify the Series in-place.

Returns:



801
802
803
804
805
806
807
808
# File 'lib/polars/series.rb', line 801

def rename(name, in_place: false)
  if in_place
    _s.rename(name)
    self
  else
    self.alias(name)
  end
end

#reshape(dims) ⇒ Series

Reshape this Series to a flat Series or a Series of Lists.

Parameters:

  • dims (Array)

    Tuple of the dimension sizes. If a -1 is used in any of the dimensions, that dimension is inferred.

Returns:



3336
3337
3338
# File 'lib/polars/series.rb', line 3336

def reshape(dims)
  super
end

#reverseSeries

Return Series in reverse order.

Examples:

s = Polars::Series.new("a", [1, 2, 3], dtype: :i8)
s.reverse
# =>
# shape: (3,)
# Series: 'a' [i8]
# [
#         3
#         2
#         1
# ]

Returns:



1741
1742
1743
# File 'lib/polars/series.rb', line 1741

def reverse
  super
end

#rolling_max(window_size, weights: nil, min_periods: nil, center: false) ⇒ Series

Apply a rolling max (moving max) over the values in this array.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

s = Polars::Series.new("a", [100, 200, 300, 400, 500])
s.rolling_max(2)
# =>
# shape: (5,)
# Series: 'a' [i64]
# [
#         null
#         200
#         300
#         400
#         500
# ]

Parameters:

  • window_size (Integer)

    The length of the window.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
# File 'lib/polars/series.rb', line 2551

def rolling_max(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false
)
  to_frame
    .select(
      Polars.col(name).rolling_max(
        window_size,
        weights: weights,
        min_periods: min_periods,
        center: center
      )
    )
    .to_series
end

#rolling_mean(window_size, weights: nil, min_periods: nil, center: false) ⇒ Series

Apply a rolling mean (moving mean) over the values in this array.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

s = Polars::Series.new("a", [100, 200, 300, 400, 500])
s.rolling_mean(2)
# =>
# shape: (5,)
# Series: 'a' [f64]
# [
#         null
#         150.0
#         250.0
#         350.0
#         450.0
# ]

Parameters:

  • window_size (Integer)

    The length of the window.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
# File 'lib/polars/series.rb', line 2601

def rolling_mean(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false
)
  to_frame
    .select(
      Polars.col(name).rolling_mean(
        window_size,
        weights: weights,
        min_periods: min_periods,
        center: center
      )
    )
    .to_series
end

#rolling_median(window_size, weights: nil, min_periods: nil, center: false) ⇒ Series

Compute a rolling median.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, 4.0, 6.0, 8.0])
s.rolling_median(3)
# =>
# shape: (6,)
# Series: 'a' [f64]
# [
#         null
#         null
#         2.0
#         3.0
#         4.0
#         6.0
# ]

Parameters:

  • window_size (Integer)

    The length of the window.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
# File 'lib/polars/series.rb', line 2803

def rolling_median(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false
)
  if min_periods.nil?
    min_periods = window_size
  end

  to_frame
    .select(
      Polars.col(name).rolling_median(
        window_size,
        weights: weights,
        min_periods: min_periods,
        center: center
      )
    )
    .to_series
end

#rolling_min(window_size, weights: nil, min_periods: nil, center: false) ⇒ Series

Apply a rolling min (moving min) over the values in this array.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

s = Polars::Series.new("a", [100, 200, 300, 400, 500])
s.rolling_min(3)
# =>
# shape: (5,)
# Series: 'a' [i64]
# [
#         null
#         null
#         100
#         200
#         300
# ]

Parameters:

  • window_size (Integer)

    The length of the window.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
# File 'lib/polars/series.rb', line 2501

def rolling_min(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false
)
  to_frame
    .select(
      Polars.col(name).rolling_min(
        window_size,
        weights: weights,
        min_periods: min_periods,
        center: center
      )
    )
    .to_series
end

#rolling_quantile(quantile, interpolation: "nearest", window_size: 2, weights: nil, min_periods: nil, center: false) ⇒ Series

Compute a rolling quantile.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, 4.0, 6.0, 8.0])
s.rolling_quantile(0.33, window_size: 3)
# =>
# shape: (6,)
# Series: 'a' [f64]
# [
#         null
#         null
#         1.0
#         2.0
#         3.0
#         4.0
# ]
s.rolling_quantile(0.33, interpolation: "linear", window_size: 3)
# =>
# shape: (6,)
# Series: 'a' [f64]
# [
#         null
#         null
#         1.66
#         2.66
#         3.66
#         5.32
# ]

Parameters:

  • quantile (Float)

    Quantile between 0.0 and 1.0.

  • interpolation ("nearest", "higher", "lower", "midpoint", "linear") (defaults to: "nearest")

    Interpolation method.

  • window_size (Integer) (defaults to: 2)

    The length of the window.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
# File 'lib/polars/series.rb', line 2872

def rolling_quantile(
  quantile,
  interpolation: "nearest",
  window_size: 2,
  weights: nil,
  min_periods: nil,
  center: false
)
  if min_periods.nil?
    min_periods = window_size
  end

  to_frame
    .select(
      Polars.col(name).rolling_quantile(
        quantile,
        interpolation: interpolation,
        window_size: window_size,
        weights: weights,
        min_periods: min_periods,
        center: center
      )
    )
    .to_series
end

#rolling_skew(window_size, bias: true) ⇒ Series

Compute a rolling skew.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, 4.0, 6.0, 8.0])
s.rolling_skew(3)
# =>
# shape: (6,)
# Series: 'a' [f64]
# [
#         null
#         null
#         0.0
#         0.0
#         0.381802
#         0.0
# ]

Parameters:

  • window_size (Integer)

    Integer size of the rolling window.

  • bias (Boolean) (defaults to: true)

    If false, the calculations are corrected for statistical bias.

Returns:



2921
2922
2923
# File 'lib/polars/series.rb', line 2921

def rolling_skew(window_size, bias: true)
  super
end

#rolling_std(window_size, weights: nil, min_periods: nil, center: false) ⇒ Series

Compute a rolling std dev.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, 4.0, 6.0, 8.0])
s.rolling_std(3)
# =>
# shape: (6,)
# Series: 'a' [f64]
# [
#         null
#         null
#         1.0
#         1.0
#         1.527525
#         2.0
# ]

Parameters:

  • window_size (Integer)

    The length of the window.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
# File 'lib/polars/series.rb', line 2702

def rolling_std(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false
)
  to_frame
    .select(
      Polars.col(name).rolling_std(
        window_size,
        weights: weights,
        min_periods: min_periods,
        center: center
      )
    )
    .to_series
end

#rolling_sum(window_size, weights: nil, min_periods: nil, center: false) ⇒ Series

Apply a rolling sum (moving sum) over the values in this array.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

s = Polars::Series.new("a", [1, 2, 3, 4, 5])
s.rolling_sum(2)
# =>
# shape: (5,)
# Series: 'a' [i64]
# [
#         null
#         3
#         5
#         7
#         9
# ]

Parameters:

  • window_size (Integer)

    The length of the window.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
# File 'lib/polars/series.rb', line 2651

def rolling_sum(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false
)
  to_frame
    .select(
      Polars.col(name).rolling_sum(
        window_size,
        weights: weights,
        min_periods: min_periods,
        center: center
      )
    )
    .to_series
end

#rolling_var(window_size, weights: nil, min_periods: nil, center: false) ⇒ Series

Compute a rolling variance.

A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector. The resulting values will be aggregated to their sum.

Examples:

s = Polars::Series.new("a", [1.0, 2.0, 3.0, 4.0, 6.0, 8.0])
s.rolling_var(3)
# =>
# shape: (6,)
# Series: 'a' [f64]
# [
#         null
#         null
#         1.0
#         1.0
#         2.333333
#         4.0
# ]

Parameters:

  • window_size (Integer)

    The length of the window.

  • weights (Array) (defaults to: nil)

    An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.

  • min_periods (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If None, it will be set equal to window size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
# File 'lib/polars/series.rb', line 2753

def rolling_var(
  window_size,
  weights: nil,
  min_periods: nil,
  center: false
)
  to_frame
    .select(
      Polars.col(name).rolling_var(
        window_size,
        weights: weights,
        min_periods: min_periods,
        center: center
      )
    )
    .to_series
end

#round(decimals = 0) ⇒ Series

Round underlying floating point data by decimals digits.

Examples:

s = Polars::Series.new("a", [1.12345, 2.56789, 3.901234])
s.round(2)
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         1.12
#         2.57
#         3.9
# ]

Parameters:

  • decimals (Integer) (defaults to: 0)

    number of decimals to round by.

Returns:



2044
2045
2046
# File 'lib/polars/series.rb', line 2044

def round(decimals = 0)
  super
end

#sample(n: nil, frac: nil, with_replacement: false, shuffle: false, seed: nil) ⇒ Series

Sample from this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3, 4, 5])
s.sample(n: 2, seed: 0)
# =>
# shape: (2,)
# Series: 'a' [i64]
# [
#     1
#     5
# ]

Parameters:

  • n (Integer) (defaults to: nil)

    Number of items to return. Cannot be used with frac. Defaults to 1 if frac is None.

  • frac (Float) (defaults to: nil)

    Fraction of items to return. Cannot be used with n.

  • with_replacement (Boolean) (defaults to: false)

    Allow values to be sampled more than once.

  • shuffle (Boolean) (defaults to: false)

    Shuffle the order of sampled data points.

  • seed (Integer) (defaults to: nil)

    Seed for the random number generator. If set to None (default), a random seed is used.

Returns:



2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
# File 'lib/polars/series.rb', line 2952

def sample(
  n: nil,
  frac: nil,
  with_replacement: false,
  shuffle: false,
  seed: nil
)
  if !n.nil? && !frac.nil?
    raise ArgumentError, "cannot specify both `n` and `frac`"
  end

  if n.nil? && !frac.nil?
    return Utils.wrap_s(_s.sample_frac(frac, with_replacement, shuffle, seed))
  end

  if n.nil?
    n = 1
  end
  Utils.wrap_s(_s.sample_n(n, with_replacement, shuffle, seed))
end

#search_sorted(element) ⇒ Integer

Find indices where elements should be inserted to maintain order.

Parameters:

  • element (Object)

    Expression or scalar value.

Returns:

  • (Integer)


1281
1282
1283
# File 'lib/polars/series.rb', line 1281

def search_sorted(element)
  Polars.select(Polars.lit(self).search_sorted(element))[0, 0]
end

#series_equal(other, null_equal: false, strict: false) ⇒ Boolean

Check if series is equal with another Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s2 = Polars::Series.new("b", [4, 5, 6])
s.series_equal(s)
# => true
s.series_equal(s2)
# => false

Parameters:

  • other (Series)

    Series to compare with.

  • null_equal (Boolean) (defaults to: false)

    Consider null values as equal.

  • strict (Boolean) (defaults to: false)

    Don't allow different numerical dtypes, e.g. comparing :u32 with a :i64 will return false.

Returns:



1635
1636
1637
# File 'lib/polars/series.rb', line 1635

def series_equal(other, null_equal: false, strict: false)
  _s.series_equal(other._s, null_equal, strict)
end

#set(filter, value) ⇒ Series

Note:

Use of this function is frequently an anti-pattern, as it can block optimization (predicate pushdown, etc). Consider using Polars.when(predicate).then(value).otherwise(self) instead.

Set masked values.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.set(s == 2, 10)
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         1
#         10
#         3
# ]

Parameters:

  • filter (Series)

    Boolean mask.

  • value (Object)

    Value with which to replace the masked values.

Returns:



1842
1843
1844
# File 'lib/polars/series.rb', line 1842

def set(filter, value)
  Utils.wrap_s(_s.send("set_with_mask_#{DTYPE_TO_FFINAME.fetch(dtype)}", filter._s, value))
end

#set_at_idx(idx, value) ⇒ Series

Set values at the index locations.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.set_at_idx(1, 10)
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         1
#         10
#         3
# ]

Parameters:

  • idx (Object)

    Integers representing the index locations.

  • value (Object)

    Replacement values.

Returns:



1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
# File 'lib/polars/series.rb', line 1866

def set_at_idx(idx, value)
  if idx.is_a?(Integer)
    idx = [idx]
  end
  if idx.length == 0
    return self
  end

  idx = Series.new("", idx)
  if value.is_a?(Integer) || value.is_a?(Float) || Utils.bool?(value) || value.is_a?(String) || value.nil?
    value = Series.new("", [value])

    # if we need to set more than a single value, we extend it
    if idx.length > 0
      value = value.extend_constant(value[0], idx.length - 1)
    end
  elsif !value.is_a?(Series)
    value = Series.new("", value)
  end
  _s.set_at_idx(idx._s, value._s)
  self
end

#set_sorted(reverse: false) ⇒ Series

Note:

This can lead to incorrect results if this Series is not sorted!! Use with care!

Flags the Series as sorted.

Enables downstream code to user fast paths for sorted arrays.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.set_sorted.max
# => 3

Parameters:

  • reverse (Boolean) (defaults to: false)

    If the Series order is reversed, e.g. descending.

Returns:



3450
3451
3452
# File 'lib/polars/series.rb', line 3450

def set_sorted(reverse: false)
  Utils.wrap_s(_s.set_sorted(reverse))
end

#shapeArray

Shape of this Series.

Returns:

  • (Array)


116
117
118
# File 'lib/polars/series.rb', line 116

def shape
  [_s.len]
end

#shift(periods = 1) ⇒ Series

Shift the values by a given period.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.shift(1)
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         null
#         1
#         2
# ]
s.shift(-1)
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         2
#         3
#         null
# ]

Parameters:

  • periods (Integer) (defaults to: 1)

    Number of places to shift (may be negative).

Returns:



2409
2410
2411
# File 'lib/polars/series.rb', line 2409

def shift(periods = 1)
  super
end

#shift_and_fill(periods, fill_value) ⇒ Series

Shift the values by a given period and fill the resulting null values.

Parameters:

  • periods (Integer)

    Number of places to shift (may be negative).

  • fill_value (Object)

    Fill None values with the result of this expression.

Returns:



2421
2422
2423
# File 'lib/polars/series.rb', line 2421

def shift_and_fill(periods, fill_value)
  super
end

#shrink_dtypeSeries

Shrink numeric columns to the minimal required datatype.

Shrink to the dtype needed to fit the extrema of this Series. This can be used to reduce memory pressure.

Returns:



3467
3468
3469
# File 'lib/polars/series.rb', line 3467

def shrink_dtype
  super
end

#shrink_to_fit(in_place: false) ⇒ Series

Shrink Series memory usage.

Shrinks the underlying array capacity to exactly fit the actual data. (Note that this function does not change the Series data type).

Returns:



3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
# File 'lib/polars/series.rb', line 3033

def shrink_to_fit(in_place: false)
  if in_place
    _s.shrink_to_fit
    self
  else
    series = clone
    series._s.shrink_to_fit
    series
  end
end

#shuffle(seed: nil) ⇒ Series

Shuffle the contents of this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.shuffle(seed: 1)
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         2
#         1
#         3
# ]

Parameters:

  • seed (Integer, nil) (defaults to: nil)

    Seed for the random number generator.

Returns:



3358
3359
3360
# File 'lib/polars/series.rb', line 3358

def shuffle(seed: nil)
  super
end

#signSeries

Compute the element-wise indication of the sign.

Examples:

s = Polars::Series.new("a", [-9.0, -0.0, 0.0, 4.0, nil])
s.sign
# =>
# shape: (5,)
# Series: 'a' [i64]
# [
#         -1
#         0
#         0
#         1
#         null
# ]

Returns:



2107
2108
2109
# File 'lib/polars/series.rb', line 2107

def sign
  super
end

#sinSeries

Compute the element-wise value for the sine.

Examples:

s = Polars::Series.new("a", [0.0, Math::PI / 2.0, Math::PI])
s.sin
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         0.0
#         1.0
#         1.2246e-16
# ]

Returns:



2126
2127
2128
# File 'lib/polars/series.rb', line 2126

def sin
  super
end

#sinhSeries

Compute the element-wise value for the hyperbolic sine.

Examples:

s = Polars::Series.new("a", [1.0, 0.0, -1.0])
s.sinh
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         1.175201
#         0.0
#         -1.175201
# ]

Returns:



2302
2303
2304
# File 'lib/polars/series.rb', line 2302

def sinh
  super
end

#skew(bias: true) ⇒ Float?

Compute the sample skewness of a data set.

For normally distributed data, the skewness should be about zero. For unimodal continuous distributions, a skewness value greater than zero means that there is more weight in the right tail of the distribution. The function skewtest can be used to determine if the skewness value is close enough to zero, statistically speaking.

Parameters:

  • bias (Boolean) (defaults to: true)

    If false, the calculations are corrected for statistical bias.

Returns:

  • (Float, nil)


3246
3247
3248
# File 'lib/polars/series.rb', line 3246

def skew(bias: true)
  _s.skew(bias)
end

#slice(offset, length = nil) ⇒ Series

Get a slice of this Series.

Examples:

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

Parameters:

  • offset (Integer)

    Start index. Negative indexing is supported.

  • length (Integer, nil) (defaults to: nil)

    Length of the slice. If set to nil, all rows starting at the offset will be selected.

Returns:



987
988
989
# File 'lib/polars/series.rb', line 987

def slice(offset, length = nil)
  super
end

#sort(reverse: false, in_place: false) ⇒ Series

Sort this Series.

Examples:

s = Polars::Series.new("a", [1, 3, 4, 2])
s.sort
# =>
# shape: (4,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         4
# ]
s.sort(reverse: true)
# =>
# shape: (4,)
# Series: 'a' [i64]
# [
#         4
#         3
#         2
#         1
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    Reverse sort.

  • in_place (Boolean) (defaults to: false)

    Sort in place.

Returns:



1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/polars/series.rb', line 1169

def sort(reverse: false, in_place: false)
  if in_place
    self._s = _s.sort(reverse)
    self
  else
    Utils.wrap_s(_s.sort(reverse))
  end
end

#sqrtSeries

Compute the square root of the elements.

Returns:



347
348
349
# File 'lib/polars/series.rb', line 347

def sqrt
  self**0.5
end

#std(ddof: 1) ⇒ Float?

Get the standard deviation of this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.std
# => 1.0

Parameters:

  • ddof (Integer) (defaults to: 1)

    “Delta Degrees of Freedom”: the divisor used in the calculation is N - ddof, where N represents the number of elements.

Returns:

  • (Float, nil)


584
585
586
587
588
589
590
# File 'lib/polars/series.rb', line 584

def std(ddof: 1)
  if !is_numeric
    nil
  else
    to_frame.select(Polars.col(name).std(ddof: ddof)).to_series[0]
  end
end

#strStringNameSpace

Create an object namespace of all string related methods.

Returns:



3495
3496
3497
# File 'lib/polars/series.rb', line 3495

def str
  StringNameSpace.new(self)
end

#structStructNameSpace

Create an object namespace of all struct related methods.

Returns:



3502
3503
3504
# File 'lib/polars/series.rb', line 3502

def struct
  StructNameSpace.new(self)
end

#sumNumeric

Note:

Dtypes :i8, :u8, :i16, and :u16 are cast to :i64 before summing to prevent overflow issues.

Reduce this Series to the sum value.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.sum
# => 6

Returns:

  • (Numeric)


511
512
513
# File 'lib/polars/series.rb', line 511

def sum
  _s.sum
end

#tail(n = 10) ⇒ Series

Get the last n rows.

Examples:

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

Parameters:

  • n (Integer) (defaults to: 10)

    Number of rows to return.

Returns:



1116
1117
1118
# File 'lib/polars/series.rb', line 1116

def tail(n = 10)
  to_frame.select(Utils.col(name).tail(n)).to_series
end

#take(indices) ⇒ Series

Take values by index.

Examples:

s = Polars::Series.new("a", [1, 2, 3, 4])
s.take([1, 3])
# =>
# shape: (2,)
# Series: 'a' [i64]
# [
#         2
#         4
# ]

Parameters:

  • indices (Array)

    Index location used for selection.

Returns:



1324
1325
1326
# File 'lib/polars/series.rb', line 1324

def take(indices)
  to_frame.select(Polars.col(name).take(indices)).to_series
end

#take_every(n) ⇒ Series

Take every nth value in the Series and return as new Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3, 4])
s.take_every(2)
# =>
# shape: (2,)
# Series: 'a' [i64]
# [
#         1
#         3
# ]

Returns:



1134
1135
1136
# File 'lib/polars/series.rb', line 1134

def take_every(n)
  super
end

#tanSeries

Compute the element-wise value for the tangent.

Examples:

s = Polars::Series.new("a", [0.0, Math::PI / 2.0, Math::PI])
s.tan
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         0.0
#         1.6331e16
#         -1.2246e-16
# ]

Returns:



2164
2165
2166
# File 'lib/polars/series.rb', line 2164

def tan
  super
end

#tanhSeries

Compute the element-wise value for the hyperbolic tangent.

Examples:

s = Polars::Series.new("a", [1.0, 0.0, -1.0])
s.tanh
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         0.761594
#         0.0
#         -0.761594
# ]

Returns:



2340
2341
2342
# File 'lib/polars/series.rb', line 2340

def tanh
  super
end

#time_unitString

Get the time unit of underlying Datetime Series as "ns", "us", or "ms".

Returns:

  • (String)


123
124
125
# File 'lib/polars/series.rb', line 123

def time_unit
  _s.time_unit
end

#to_aArray

Convert this Series to a Ruby Array. This operation clones data.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.to_a
# => [1, 2, 3]

Returns:

  • (Array)


1711
1712
1713
# File 'lib/polars/series.rb', line 1711

def to_a
  _s.to_a
end

#to_dummiesDataFrame

Get dummy variables.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.to_dummies
# =>
# shape: (3, 3)
# ┌─────┬─────┬─────┐
# │ a_1 ┆ a_2 ┆ a_3 │
# │ --- ┆ --- ┆ --- │
# │ u8  ┆ u8  ┆ u8  │
# ╞═════╪═════╪═════╡
# │ 1   ┆ 0   ┆ 0   │
# ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
# │ 0   ┆ 1   ┆ 0   │
# ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
# │ 0   ┆ 0   ┆ 1   │
# └─────┴─────┴─────┘

Returns:



661
662
663
# File 'lib/polars/series.rb', line 661

def to_dummies
  Utils.wrap_df(_s.to_dummies)
end

#to_frameDataFrame

Cast this Series to a DataFrame.

Returns:



406
407
408
# File 'lib/polars/series.rb', line 406

def to_frame
  Utils.wrap_df(RbDataFrame.new([_s]))
end

#to_physicalSeries

Cast to physical representation of the logical dtype.

  • :date -> :i32
  • :datetime -> :i64
  • :time -> :i64
  • :duration -> :i64
  • :cat -> :u32
  • other data types will be left unchanged.

Examples:

s = Polars::Series.new("values", ["a", nil, "x", "a"])
s.cast(:cat).to_physical
# =>
# shape: (4,)
# Series: 'values' [u32]
# [
#         0
#         null
#         1
#         0
# ]

Returns:



1699
1700
1701
# File 'lib/polars/series.rb', line 1699

def to_physical
  super
end

#to_sString Also known as: inspect

Returns a string representing the Series.

Returns:

  • (String)


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

def to_s
  _s.to_s
end

#top_k(k: 5, reverse: false) ⇒ Boolean

Return the k largest elements.

If reverse: true, the smallest elements will be given.

Parameters:

  • k (Integer) (defaults to: 5)

    Number of elements to return.

  • reverse (Boolean) (defaults to: false)

    Return the smallest elements.

Returns:



1188
1189
1190
# File 'lib/polars/series.rb', line 1188

def top_k(k: 5, reverse: false)
  super
end

#unique(maintain_order: false) ⇒ Series

Get unique elements in series.

Examples:

s = Polars::Series.new("a", [1, 2, 2, 3])
s.unique.sort
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
# ]

Parameters:

  • maintain_order (Boolean) (defaults to: false)

    Maintain order of data. This requires more work.

Returns:



1303
1304
1305
# File 'lib/polars/series.rb', line 1303

def unique(maintain_order: false)
  super
end

#unique_countsSeries

Return a count of the unique values in the order of appearance.

Examples:

s = Polars::Series.new("id", ["a", "b", "b", "c", "c", "c"])
s.unique_counts
# =>
# shape: (3,)
# Series: 'id' [u32]
# [
#         1
#         2
#         3
# ]

Returns:



707
708
709
# File 'lib/polars/series.rb', line 707

def unique_counts
  super
end

#value_counts(sort: false) ⇒ DataFrame

Count the unique values in a Series.

Examples:

s = Polars::Series.new("a", [1, 2, 2, 3])
s.value_counts.sort("a")
# =>
# shape: (3, 2)
# ┌─────┬────────┐
# │ a   ┆ counts │
# │ --- ┆ ---    │
# │ i64 ┆ u32    │
# ╞═════╪════════╡
# │ 1   ┆ 1      │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
# │ 2   ┆ 2      │
# ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
# │ 3   ┆ 1      │
# └─────┴────────┘

Parameters:

  • sort (Boolean) (defaults to: false)

    Ensure the output is sorted from most values to least.

Returns:



688
689
690
# File 'lib/polars/series.rb', line 688

def value_counts(sort: false)
  Utils.wrap_df(_s.value_counts(sort))
end

#var(ddof: 1) ⇒ Float?

Get variance of this Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.var
# => 1.0

Parameters:

  • ddof (Integer) (defaults to: 1)

    “Delta Degrees of Freedom”: the divisor used in the calculation is N - ddof, where N represents the number of elements.

Returns:

  • (Float, nil)


604
605
606
607
608
609
610
# File 'lib/polars/series.rb', line 604

def var(ddof: 1)
  if !is_numeric
    nil
  else
    to_frame.select(Polars.col(name).var(ddof: ddof)).to_series[0]
  end
end

#zip_with(mask, other) ⇒ Series

Take values from self or other based on the given mask.

Where mask evaluates true, take values from self. Where mask evaluates false, take values from other.

Examples:

s1 = Polars::Series.new([1, 2, 3, 4, 5])
s2 = Polars::Series.new([5, 4, 3, 2, 1])
s1.zip_with(s1 < s2, s2)
# =>
# shape: (5,)
# Series: '' [i64]
# [
#         1
#         2
#         3
#         2
#         1
# ]
mask = Polars::Series.new([true, false, true, false, true])
s1.zip_with(mask, s2)
# =>
# shape: (5,)
# Series: '' [i64]
# [
#         1
#         4
#         3
#         2
#         5
# ]

Parameters:

  • mask (Series)

    Boolean Series.

  • other (Series)

    Series of same type.

Returns:



2465
2466
2467
# File 'lib/polars/series.rb', line 2465

def zip_with(mask, other)
  Utils.wrap_s(_s.zip_with(mask._s, other._s))
end

#|(other) ⇒ Series

Bitwise OR.

Returns:



148
149
150
151
152
153
# File 'lib/polars/series.rb', line 148

def |(other)
  if !other.is_a?(Series)
    other = Series.new([other])
  end
  Utils.wrap_s(_s.bitor(other._s))
end