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) ⇒ 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: Polars::Float32)

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.



32
33
34
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
74
75
76
77
78
79
80
81
# File 'lib/polars/series.rb', line 32

def initialize(name = nil, values = nil, dtype: nil, strict: true, nan_to_null: false)
  # If 'Unknown' treat as nil to trigger type inference
  if dtype == Unknown
    dtype = nil
  elsif !dtype.nil? && !Utils.is_polars_dtype(dtype)
    dtype = Utils.parse_into_dtype(dtype)
  end

  # Handle case where values are passed as the first argument
  original_name = nil
  if name.nil?
    name = ""
  elsif name.is_a?(::String)
    original_name = name
  else
    if values.nil?
      values = name
      name = ""
    else
      raise TypeError, "Series name must be a string"
    end
  end

  if values.is_a?(::Array) || values.is_a?(Range)
    self._s = Utils.sequence_to_rbseries(
      name,
      values,
      dtype: dtype,
      strict: strict
    )
  elsif values.nil?
    self._s = Utils.sequence_to_rbseries(name, [], dtype: dtype)
  elsif defined?(Numo::NArray) && values.is_a?(Numo::NArray)
    self._s = Utils.numo_to_rbseries(name, values, strict: strict, nan_to_null: nan_to_null)

    if !dtype.nil?
      self._s = cast(dtype, strict: strict)._s
    end
  elsif values.is_a?(Series)
    self._s = Utils.series_to_rbseries(original_name, values, dtype: dtype, strict: strict)
  elsif values.is_a?(DataFrame)
    self._s = Utils.dataframe_to_rbseries(
      original_name, values, dtype: dtype, strict: strict
    )
  elsif values.respond_to?(:arrow_c_stream)
    self._s = RbSeries.from_arrow_c_stream(values)
  else
    raise TypeError, "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

#!Series

Performs boolean not.

Returns:

Raises:

  • (NotImplementedError)


410
411
412
413
414
415
# File 'lib/polars/series.rb', line 410

def !
  if dtype == Boolean
    return Utils.wrap_s(_s.not_)
  end
  raise NotImplementedError
end

#!=(other) ⇒ Series

Not equal.

Returns:



193
194
195
# File 'lib/polars/series.rb', line 193

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

#%(other) ⇒ Series

Returns the modulo.

Returns:



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

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

#&(other) ⇒ Series

Bitwise AND.

Returns:



156
157
158
159
160
161
# File 'lib/polars/series.rb', line 156

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:



362
363
364
365
366
367
368
369
370
# File 'lib/polars/series.rb', line 362

def *(other)
  if dtype.temporal?
    raise ArgumentError, "first cast to integer before multiplying datelike dtypes"
  elsif other.is_a?(DataFrame)
    other * self
  else
    _arithmetic(other, :mul)
  end
end

#**(power) ⇒ Series

Raises to the power of exponent.

Returns:



400
401
402
403
404
405
# File 'lib/polars/series.rb', line 400

def **(power)
  if dtype.temporal?
    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:



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

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

#-(other) ⇒ Series

Performs subtraction.

Returns:



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

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

#-@Series

Performs negation.

Returns:



420
421
422
# File 'lib/polars/series.rb', line 420

def -@
  0 - self
end

#/(other) ⇒ Series

Performs division.

Returns:



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/polars/series.rb', line 375

def /(other)
  if dtype.temporal?
    raise ArgumentError, "first cast to integer before dividing datelike dtypes"
  end

  if dtype.float?
    return _arithmetic(other, :div)
  end

  cast(Float64) / other
end

#<(other) ⇒ Series

Less than.

Returns:



207
208
209
# File 'lib/polars/series.rb', line 207

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

#<=(other) ⇒ Series

Less than or equal.

Returns:



221
222
223
# File 'lib/polars/series.rb', line 221

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

#==(other) ⇒ Series

Equal.

Returns:



186
187
188
# File 'lib/polars/series.rb', line 186

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

#>(other) ⇒ Series

Greater than.

Returns:



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

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

#>=(other) ⇒ Series

Greater than or equal.

Returns:



214
215
216
# File 'lib/polars/series.rb', line 214

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

#[](item) ⇒ Object

Returns elements of the Series.

Returns:

Raises:

  • (ArgumentError)


438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/polars/series.rb', line 438

def [](item)
  if item.is_a?(Series) && [UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64].include?(item.dtype)
    return Utils.wrap_s(_s.gather_with_series(_pos_idxs(item)._s))
  end

  if item.is_a?(Series) && item.bool?
    return filter(item)
  end

  if item.is_a?(Integer)
    if item < 0
      item = len + item
    end

    return _s.get_index(item)
  end

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

  if Utils.is_int_sequence(item)
    return Utils.wrap_s(_s.gather_with_series(_pos_idxs(Series.new("", item))._s))
  end

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

#[]=(key, value) ⇒ Object

Sets an element of the Series.

Returns:



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
498
499
# File 'lib/polars/series.rb', line 469

def []=(key, value)
  if value.is_a?(::Array)
    if dtype.numeric? || dtype.temporal?
      scatter(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 == Boolean
      self._s = set(key, value)._s
    elsif key.dtype == UInt64
      self._s = scatter(key.cast(UInt32), value)._s
    elsif key.dtype == UInt32
      self._s = scatter(key, value)._s
    else
      raise Todo
    end
  elsif key.is_a?(::Array)
    s = Utils.wrap_s(Utils.sequence_to_rbseries("", key, dtype: UInt32))
    self[s] = value
  elsif key.is_a?(Range)
    s = Series.new("", key, dtype: UInt32)
    self[s] = value
  elsif key.is_a?(Integer)
    self[[key]] = value
  else
    raise ArgumentError, "cannot use #{key} for indexing"
  end
end

#^(other) ⇒ Series

Bitwise XOR.

Returns:



176
177
178
179
180
181
# File 'lib/polars/series.rb', line 176

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

#absSeries

Compute absolute values.

Examples:

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

Returns:



5474
5475
5476
# File 'lib/polars/series.rb', line 5474

def abs
  super
end

#alias(name) ⇒ Series

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

Examples:

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

Parameters:

Returns:



1582
1583
1584
1585
1586
# File 'lib/polars/series.rb', line 1582

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

#all?(ignore_nulls: true, &block) ⇒ Boolean Also known as: all

Check if all boolean values in the column are true.

Examples:

Polars::Series.new([true, true]).all?
# => true
Polars::Series.new([false, true]).all?
# => false
Polars::Series.new([nil, true]).all?
# => true

Returns:



645
646
647
648
649
650
651
# File 'lib/polars/series.rb', line 645

def all?(ignore_nulls: true, &block)
  if block_given?
    map_elements(return_dtype: Boolean, skip_nulls: ignore_nulls, &block).all?
  else
    _s.all(ignore_nulls)
  end
end

#any?(ignore_nulls: true, &block) ⇒ Boolean Also known as: any

Check if any boolean value in the column is true.

Examples:

Polars::Series.new([true, false]).any?
# => true
Polars::Series.new([false, false]).any?
# => false
Polars::Series.new([nil, false]).any?
# => false

Returns:



621
622
623
624
625
626
627
# File 'lib/polars/series.rb', line 621

def any?(ignore_nulls: true, &block)
  if block_given?
    map_elements(return_dtype: Boolean, skip_nulls: ignore_nulls, &block).any?
  else
    _s.any(ignore_nulls)
  end
end

#append(other) ⇒ 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.

Returns:



1813
1814
1815
1816
1817
# File 'lib/polars/series.rb', line 1813

def append(other)
  Utils.require_same_type(self, other)
  _s.append(other._s)
  self
end

#approx_n_uniqueObject

Approximate count of unique values.

This is done using the HyperLogLog++ algorithm for cardinality estimation.

Returns:



6398
6399
6400
# File 'lib/polars/series.rb', line 6398

def approx_n_unique
  _s.approx_n_unique
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:



3689
3690
3691
# File 'lib/polars/series.rb', line 3689

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:



3747
3748
3749
# File 'lib/polars/series.rb', line 3747

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:



3670
3671
3672
# File 'lib/polars/series.rb', line 3670

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:



3727
3728
3729
# File 'lib/polars/series.rb', line 3727

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:



3708
3709
3710
# File 'lib/polars/series.rb', line 3708

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:



3770
3771
3772
# File 'lib/polars/series.rb', line 3770

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)


2297
2298
2299
# File 'lib/polars/series.rb', line 2297

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)


2285
2286
2287
# File 'lib/polars/series.rb', line 2285

def arg_min
  _s.arg_min
end

#arg_sort(descending: 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:

  • descending (Boolean) (defaults to: false)

    Sort in reverse (descending) order.

  • nulls_last (Boolean) (defaults to: false)

    Place null values last instead of first.

Returns:



2254
2255
2256
# File 'lib/polars/series.rb', line 2254

def arg_sort(descending: 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:



2711
2712
2713
# File 'lib/polars/series.rb', line 2711

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:



2273
2274
2275
# File 'lib/polars/series.rb', line 2273

def arg_unique
  super
end

#arrArrayNameSpace

Create an object namespace of all array related methods.

Returns:



6412
6413
6414
# File 'lib/polars/series.rb', line 6412

def arr
  ArrayNameSpace.new(self)
end

#backward_fill(limit: nil) ⇒ Series

Fill missing values with the next non-null value.

This is an alias of .fill_null(strategy: "backward").

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of consecutive null values to backward fill.

Returns:



3362
3363
3364
# File 'lib/polars/series.rb', line 3362

def backward_fill(limit: nil)
  fill_null(strategy: "backward", limit: limit)
end

#binBinaryNameSpace

Create an object namespace of all binary related methods.

Returns:



6419
6420
6421
# File 'lib/polars/series.rb', line 6419

def bin
  BinaryNameSpace.new(self)
end

#bitwise_andObject

Perform an aggregation of bitwise ANDs.

Returns:



6347
6348
6349
# File 'lib/polars/series.rb', line 6347

def bitwise_and
  _s.bitwise_and
end

#bitwise_count_onesSeries

Evaluate the number of set bits.

Returns:



6305
6306
6307
# File 'lib/polars/series.rb', line 6305

def bitwise_count_ones
  super
end

#bitwise_count_zerosSeries

Evaluate the number of unset bits.

Returns:



6312
6313
6314
# File 'lib/polars/series.rb', line 6312

def bitwise_count_zeros
  super
end

#bitwise_leading_onesSeries

Evaluate the number most-significant set bits before seeing an unset bit.

Returns:



6319
6320
6321
# File 'lib/polars/series.rb', line 6319

def bitwise_leading_ones
  super
end

#bitwise_leading_zerosSeries

Evaluate the number most-significant unset bits before seeing a set bit.

Returns:



6326
6327
6328
# File 'lib/polars/series.rb', line 6326

def bitwise_leading_zeros
  super
end

#bitwise_orObject

Perform an aggregation of bitwise ORs.

Returns:



6354
6355
6356
# File 'lib/polars/series.rb', line 6354

def bitwise_or
  _s.bitwise_or
end

#bitwise_trailing_onesSeries

Evaluate the number least-significant set bits before seeing an unset bit.

Returns:



6333
6334
6335
# File 'lib/polars/series.rb', line 6333

def bitwise_trailing_ones
  super
end

#bitwise_trailing_zerosSeries

Evaluate the number least-significant unset bits before seeing a set bit.

Returns:



6340
6341
6342
# File 'lib/polars/series.rb', line 6340

def bitwise_trailing_zeros
  super
end

#bitwise_xorObject

Perform an aggregation of bitwise XORs.

Returns:



6361
6362
6363
# File 'lib/polars/series.rb', line 6361

def bitwise_xor
  _s.bitwise_xor
end

#bottom_k(k: 5) ⇒ Boolean

Return the k smallest elements.

Examples:

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

Parameters:

  • k (Integer) (defaults to: 5)

    Number of elements to return.

Returns:



2190
2191
2192
# File 'lib/polars/series.rb', line 2190

def bottom_k(k: 5)
  super
end

#bottom_k_by(by, k: 5, reverse: false) ⇒ Series

Return the k smallest elements of the by column.

Non-null elements are always preferred over null elements, regardless of the value of reverse. The output is not guaranteed to be in any particular order, call sort after this function if you wish the output to be sorted.

Examples:

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

Parameters:

  • by (Object)

    Column used to determine the smallest elements. Accepts expression input. Strings are parsed as column names.

  • k (Integer) (defaults to: 5)

    Number of elements to return.

  • reverse (Object) (defaults to: false)

    Consider the k largest elements of the by column( (instead of the k smallest). This can be specified per column by passing an array of booleans.

Returns:



2224
2225
2226
2227
2228
2229
2230
# File 'lib/polars/series.rb', line 2224

def bottom_k_by(
  by,
  k: 5,
  reverse: false
)
  super
end

#cast(dtype, strict: true, wrap_numerical: false) ⇒ Series

Cast between data types.

Examples:

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

Parameters:

  • dtype (Object)

    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

  • wrap_numerical (Boolean) (defaults to: false)

    If true numeric casts wrap overflowing values instead of marking the cast as invalid.

Returns:



2899
2900
2901
2902
# File 'lib/polars/series.rb', line 2899

def cast(dtype, strict: true, wrap_numerical: false)
  dtype = Utils.parse_into_dtype(dtype)
  self.class._from_rbseries(_s.cast(dtype, strict, wrap_numerical))
end

#catCatNameSpace

Create an object namespace of all categorical related methods.

Returns:



6426
6427
6428
# File 'lib/polars/series.rb', line 6426

def cat
  CatNameSpace.new(self)
end

#cbrtSeries

Compute the cube root of the elements.

Examples:

s = Polars::Series.new([1, 2, 3])
s.cbrt
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         1.0
#         1.259921
#         1.44225
# ]

Returns:



602
603
604
# File 'lib/polars/series.rb', line 602

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



3416
3417
3418
# File 'lib/polars/series.rb', line 3416

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], rechunk: true).chunk_lengths
# => [6]

Concatenate Series with rechunk: false

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

Returns:



1625
1626
1627
# File 'lib/polars/series.rb', line 1625

def chunk_lengths
  _s.chunk_lengths
end

#clear(n: 0) ⇒ Series

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.clear
# =>
# shape: (0,)
# Series: 'a' [bool]
# [
# ]
s.clear(n: 2)
# =>
# shape: (2,)
# Series: 'a' [bool]
# [
#         null
#         null
# ]

Parameters:

  • n (Integer) (defaults to: 0)

    Number of (empty) elements to return in the cleared frame.

Returns:



3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
# File 'lib/polars/series.rb', line 3264

def clear(n: 0)
  if n < 0
    msg = "`n` should be greater than or equal to 0, got #{n}"
    raise ArgumentError, msg
  end
  # faster path
  if n == 0
    return self.class._from_rbseries(_s.clear)
  end
  s = len > 0 ? self.class.new(name, [], dtype: dtype) : clone
  n > 0 ? s.extend_constant(nil, n) : s
end

#clip(lower_bound = nil, upper_bound = nil) ⇒ Series

Set values outside the given boundaries to the boundary value.

Examples:

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

Parameters:

  • lower_bound (Numeric) (defaults to: nil)

    Lower bound. Accepts expression input. Non-expression inputs are parsed as literals. If set to nil (default), no lower bound is applied.

  • upper_bound (Numeric) (defaults to: nil)

    Upper bound. Accepts expression input. Non-expression inputs are parsed as literals. If set to nil (default), no upper bound is applied.

Returns:



5715
5716
5717
# File 'lib/polars/series.rb', line 5715

def clip(lower_bound = nil, upper_bound = nil)
  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:



3613
3614
3615
# File 'lib/polars/series.rb', line 3613

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:



3808
3809
3810
# File 'lib/polars/series.rb', line 3808

def cosh
  super
end

#cotSeries

Compute the element-wise value for the cotangent.

Examples:

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

Returns:



3651
3652
3653
# File 'lib/polars/series.rb', line 3651

def cot
  super
end

#countInteger

Return the number of elements in the Series.

Examples:

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

Returns:

  • (Integer)


2858
2859
2860
# File 'lib/polars/series.rb', line 2858

def count
  len - null_count
end

#cum_count(reverse: false) ⇒ Series

Return the cumulative count of the non-null values in the column.

Examples:

s = Polars::Series.new(["x", "k", nil, "d"])
s.cum_count
# =>
# shape: (4,)
# Series: '' [u32]
# [
#         1
#         2
#         2
#         3
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    Reverse the operation.

Returns:



1693
1694
1695
# File 'lib/polars/series.rb', line 1693

def cum_count(reverse: false)
  super
end

#cum_max(reverse: false) ⇒ Series

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

Examples:

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

Parameters:

  • reverse (Boolean) (defaults to: false)

    reverse the operation.

Returns:



1737
1738
1739
# File 'lib/polars/series.rb', line 1737

def cum_max(reverse: false)
  super
end

#cum_min(reverse: false) ⇒ Series

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

Examples:

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

Parameters:

  • reverse (Boolean) (defaults to: false)

    reverse the operation.

Returns:



1715
1716
1717
# File 'lib/polars/series.rb', line 1715

def cum_min(reverse: false)
  super
end

#cum_prod(reverse: false) ⇒ Series

Note:

Dtypes in \{Int8, UInt8, Int16, UInt16} are cast to Int64 before summing 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.cum_prod
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         1
#         2
#         6
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    reverse the operation.

Returns:



1763
1764
1765
# File 'lib/polars/series.rb', line 1763

def cum_prod(reverse: false)
  super
end

#cum_sum(reverse: false) ⇒ Series

Note:

Dtypes in \{Int8, UInt8, Int16, UInt16} are cast to Int64 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.cum_sum
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         1
#         3
#         6
# ]

Parameters:

  • reverse (Boolean) (defaults to: false)

    reverse the operation.

Returns:



1670
1671
1672
# File 'lib/polars/series.rb', line 1670

def cum_sum(reverse: false)
  super
end

#cumulative_eval(expr, min_samples: 1) ⇒ 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' [i64]
# [
#         0
#         -3
#         -8
#         -15
#         -24
# ]

Parameters:

  • expr (Expr)

    Expression to evaluate

  • min_samples (Integer) (defaults to: 1)

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

Returns:



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

def cumulative_eval(expr, min_samples: 1)
  super
end

#cut(breaks, labels: nil, left_closed: false, include_breaks: false) ⇒ Series

Bin continuous values into discrete categories.

Examples:

Divide the column into three categories.

s = Polars::Series.new("foo", [-2, -1, 0, 1, 2])
s.cut([-1, 1], labels: ["a", "b", "c"])
# =>
# shape: (5,)
# Series: 'foo' [enum]
# [
#         "a"
#         "a"
#         "b"
#         "b"
#         "c"
# ]

Create a DataFrame with the breakpoint and category for each value.

cut = s.cut([-1, 1], include_breaks: true).alias("cut")
s.to_frame.with_columns(cut).unnest("cut")
# =>
# shape: (5, 3)
# ┌─────┬─────────────┬────────────┐
# │ foo ┆ break_point ┆ category   │
# │ --- ┆ ---         ┆ ---        │
# │ i64 ┆ f64         ┆ enum       │
# ╞═════╪═════════════╪════════════╡
# │ -2  ┆ -1.0        ┆ (-inf, -1] │
# │ -1  ┆ -1.0        ┆ (-inf, -1] │
# │ 0   ┆ 1.0         ┆ (-1, 1]    │
# │ 1   ┆ 1.0         ┆ (-1, 1]    │
# │ 2   ┆ inf         ┆ (1, inf]   │
# └─────┴─────────────┴────────────┘

Parameters:

  • breaks (Array)

    List of unique cut points.

  • labels (Array) (defaults to: nil)

    Names of the categories. The number of labels must be equal to the number of cut points plus one.

  • left_closed (Boolean) (defaults to: false)

    Set the intervals to be left-closed instead of right-closed.

  • include_breaks (Boolean) (defaults to: false)

    Include a column with the right endpoint of the bin each observation falls in. This will change the data type of the output from a Categorical to a Struct.

Returns:



1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
# File 'lib/polars/series.rb', line 1219

def cut(breaks, labels: nil, left_closed: false, include_breaks: false)
  result = (
    to_frame
    .select(
      Polars.col(name).cut(
        breaks,
        labels: labels,
        left_closed: left_closed,
        include_breaks: include_breaks
      )
    )
    .to_series
  )

  if include_breaks
    result = result.struct.rename_fields(["break_point", "category"])
  end

  result
end

#describe(percentiles: [0.25, 0.5, 0.75], interpolation: "nearest") ⇒ DataFrame

Quick summary statistics of a series.

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

Examples:

s = Polars::Series.new([1, 2, 3, 4, 5])
s.describe
# =>
# shape: (9, 2)
# ┌────────────┬──────────┐
# │ statistic  ┆ value    │
# │ ---        ┆ ---      │
# │ str        ┆ f64      │
# ╞════════════╪══════════╡
# │ count      ┆ 5.0      │
# │ null_count ┆ 0.0      │
# │ mean       ┆ 3.0      │
# │ std        ┆ 1.581139 │
# │ min        ┆ 1.0      │
# │ 25%        ┆ 2.0      │
# │ 50%        ┆ 3.0      │
# │ 75%        ┆ 4.0      │
# │ max        ┆ 5.0      │
# └────────────┴──────────┘

Non-numeric data types may not have all statistics available.

s = Polars::Series.new(["aa", "aa", nil, "bb", "cc"])
s.describe
# =>
# shape: (4, 2)
# ┌────────────┬───────┐
# │ statistic  ┆ value │
# │ ---        ┆ ---   │
# │ str        ┆ str   │
# ╞════════════╪═══════╡
# │ count      ┆ 4     │
# │ null_count ┆ 1     │
# │ min        ┆ aa    │
# │ max        ┆ cc    │
# └────────────┴───────┘

Parameters:

  • percentiles (Array) (defaults to: [0.25, 0.5, 0.75])

    One or more percentiles to include in the summary statistics (if the Series has a numeric dtype). All values must be in the range [0, 1].

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

    Interpolation method used when calculating percentiles.

Returns:



881
882
883
884
885
886
887
888
889
890
891
# File 'lib/polars/series.rb', line 881

def describe(
  percentiles: [0.25, 0.5, 0.75],
  interpolation: "nearest"
)
  stats = to_frame.describe(
    percentiles: percentiles,
    interpolation: interpolation
  )
  stats.columns = ["statistic", "value"]
  stats.filter(F.col("value").is_not_null)
end

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

Calculate the n-th discrete difference.

Examples:

s = Polars::Series.new("s", [20, 10, 30, 25, 35], dtype: Polars::Int8)
s.diff
# =>
# shape: (5,)
# Series: 's' [i8]
# [
#         null
#         -10
#         20
#         -5
#         10
# ]
s.diff(n: 2)
# =>
# shape: (5,)
# Series: 's' [i8]
# [
#         null
#         null
#         10
#         15
#         5
# ]
s.diff(n: 2, null_behavior: "drop")
# =>
# shape: (3,)
# Series: 's' [i8]
# [
#         10
#         15
#         5
# ]

Parameters:

  • n (Integer) (defaults to: 1)

    Number of slots to shift.

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

    How to handle null values.

Returns:



5582
5583
5584
# File 'lib/polars/series.rb', line 5582

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)


3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
# File 'lib/polars/series.rb', line 3525

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.

Examples:

s = Polars::Series.new([1.0, nil, 3.0, Float::NAN])
s.drop_nans
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         1.0
#         null
#         3.0
# ]

Returns:



791
792
793
# File 'lib/polars/series.rb', line 791

def drop_nans
  super
end

#drop_nullsSeries

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

Examples:

s = Polars::Series.new([1.0, nil, 3.0, Float::NAN])
s.drop_nulls
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         1.0
#         3.0
#         NaN
# ]

Returns:



772
773
774
# File 'lib/polars/series.rb', line 772

def drop_nulls
  super
end

#dtDateTimeNameSpace

Create an object namespace of all datetime related methods.

Returns:



6433
6434
6435
# File 'lib/polars/series.rb', line 6433

def dt
  DateTimeNameSpace.new(self)
end

#dtypeSymbol

Get the data type of this Series.

Examples:

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

Returns:

  • (Symbol)


98
99
100
# File 'lib/polars/series.rb', line 98

def dtype
  _s.dtype
end

#eachObject

Returns an enumerator.

Returns:



427
428
429
430
431
432
433
# File 'lib/polars/series.rb', line 427

def each
  return to_enum(:each) unless block_given?

  length.times do |i|
    yield self[i]
  end
end

#entropy(base: Math::E, normalize: true) ⇒ 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: true)

    Normalize pk if it doesn't sum to 1.

Returns:

  • (Float, nil)


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

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

#eq(other) ⇒ Series

Method equivalent of operator expression series == other.

Returns:



242
243
244
# File 'lib/polars/series.rb', line 242

def eq(other)
  self == other
end

#eq_missing(other) ⇒ Object

Method equivalent of equality operator series == other where nil == nil.

This differs from the standard ne where null values are propagated.

Examples:

s1 = Polars::Series.new("a", [333, 200, nil])
s2 = Polars::Series.new("a", [100, 200, nil])
s1.eq(s2)
# =>
# shape: (3,)
# Series: 'a' [bool]
# [
#         false
#         true
#         null
# ]
s1.eq_missing(s2)
# =>
# shape: (3,)
# Series: 'a' [bool]
# [
#         false
#         true
#         true
# ]

Parameters:

  • other (Object)

    A literal or expression value to compare with.

Returns:



278
279
280
281
282
283
# File 'lib/polars/series.rb', line 278

def eq_missing(other)
  if other.is_a?(Expr)
    return Polars.lit(self).eq_missing(other)
  end
  to_frame.select(Polars.col(name).eq_missing(other)).to_series
end

#equals(other, check_dtypes: false, check_names: false, null_equal: true) ⇒ 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.equals(s)
# => true
s.equals(s2)
# => false

Parameters:

  • other (Series)

    Series to compare with.

  • check_dtypes (Boolean) (defaults to: false)

    Require data types to match.

  • check_names (Boolean) (defaults to: false)

    Require names to match.

  • null_equal (Boolean) (defaults to: true)

    Consider null values as equal.

Returns:



2846
2847
2848
# File 'lib/polars/series.rb', line 2846

def equals(other, check_dtypes: false, check_names: false, null_equal: true)
  _s.equals(other._s, check_dtypes, check_names, null_equal)
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: Polars::UInt32)
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)


563
564
565
566
# File 'lib/polars/series.rb', line 563

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_samples: 1, ignore_nulls: false) ⇒ Series

Exponentially-weighted moving average.

Examples:

s = Polars::Series.new([1, 2, 3])
s.ewm_mean(com: 1, ignore_nulls: false)
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         1.0
#         1.666667
#         2.428571
# ]

Returns:



6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
# File 'lib/polars/series.rb', line 6036

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

#ewm_mean_by(by, half_life:) ⇒ Series

Compute time-based exponentially weighted moving average.

Examples:

df = Polars::DataFrame.new(
  {
    "values" => [0, 1, 2, nil, 4],
    "times" => [
      Date.new(2020, 1, 1),
      Date.new(2020, 1, 3),
      Date.new(2020, 1, 10),
      Date.new(2020, 1, 15),
      Date.new(2020, 1, 17)
    ]
  }
).sort("times")
df["values"].ewm_mean_by(df["times"], half_life: "4d")
# =>
# shape: (5,)
# Series: 'values' [f64]
# [
#         0.0
#         0.292893
#         1.492474
#         null
#         3.254508
# ]

Parameters:

  • by (Object)

    Times to calculate average by. Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type.

  • half_life (String)

    Unit over which observation decays to half its value.

    Can be created either from a timedelta, or by using the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 day)
    • 1w (1 week)
    • 1i (1 index count)

    Or combine them: "3d12h4m25s" # 3 days, 12 hours, 4 minutes, and 25 seconds

    Note that half_life is treated as a constant duration - calendar durations such as months (or even days in the time-zone-aware case) are not supported, please express your duration in an approximately equivalent number of hours (e.g. '370h' instead of '1mo').

Returns:



6103
6104
6105
6106
6107
6108
# File 'lib/polars/series.rb', line 6103

def ewm_mean_by(
  by,
  half_life:
)
  super
end

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

Exponentially-weighted moving standard deviation.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.ewm_std(com: 1, ignore_nulls: false)
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         0.0
#         0.707107
#         0.963624
# ]

Returns:



6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
# File 'lib/polars/series.rb', line 6125

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

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

Exponentially-weighted moving variance.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.ewm_var(com: 1, ignore_nulls: false)
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         0.0
#         0.5
#         0.928571
# ]

Returns:



6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
# File 'lib/polars/series.rb', line 6153

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

#expSeries

Compute the exponential, element-wise.

Examples:

s = Polars::Series.new([1, 2, 3])
s.exp
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         2.718282
#         7.389056
#         20.085537
# ]

Returns:



753
754
755
# File 'lib/polars/series.rb', line 753

def exp
  super
end

#explode(empty_as_null: true, keep_nulls: true) ⇒ Series

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

Parameters:

  • empty_as_null (Boolean) (defaults to: true)

    Explode an empty list into a null.

  • keep_nulls (Boolean) (defaults to: true)

    Explode a null list into a null.

Returns:



2822
2823
2824
# File 'lib/polars/series.rb', line 2822

def explode(empty_as_null: true, keep_nulls: true)
  super
end

#extExtensionNameSpace

Create an object namespace of all extension type related methods.

Returns:



6454
6455
6456
# File 'lib/polars/series.rb', line 6454

def ext
  ExtensionNameSpace.new(self)
end

#extend(other) ⇒ Series

Note:

This method modifies the series in-place. The series is returned for convenience only.

Extend the memory backed by this Series with the values from another.

Different from append, which adds the chunks from other to the chunks of this series, 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 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 over extend when you want to append many times before doing a query. For instance, when you read in multiple files and want to store them in a single Series. In the latter case, finish the sequence of append operations with a rechunk.

Examples:

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

The resulting series will consist of a single chunk.

a.n_chunks
# => 1

Parameters:

  • other (Series)

    Series to extend the series with.

Returns:



1864
1865
1866
1867
1868
# File 'lib/polars/series.rb', line 1864

def extend(other)
  Utils.require_same_type(self, other)
  _s.extend(other._s)
  self
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:



6189
6190
6191
# File 'lib/polars/series.rb', line 6189

def extend_constant(value, n)
  super
end

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

  • value (Object)

    Value used to fill nan values.

Returns:



3298
3299
3300
# File 'lib/polars/series.rb', line 3298

def fill_nan(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:



3350
3351
3352
# File 'lib/polars/series.rb', line 3350

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:

Returns:



1888
1889
1890
1891
1892
1893
# File 'lib/polars/series.rb', line 1888

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

#first(ignore_nulls: false) ⇒ Object

Get the first element of the Series.

Returns nil if the Series is empty.

Parameters:

  • ignore_nulls (Boolean) (defaults to: false)

    Ignore null values (default false). If set to true, the first non-null value is returned, otherwise nil is returned if no non-null value exists.

Returns:



6375
6376
6377
# File 'lib/polars/series.rb', line 6375

def first(ignore_nulls: false)
  _s.first(ignore_nulls)
end

#flagsHash

Get flags that are set on the Series.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.flags
# => {"SORTED_ASC"=>false, "SORTED_DESC"=>false}

Returns:

  • (Hash)


110
111
112
113
114
115
116
117
118
119
# File 'lib/polars/series.rb', line 110

def flags
  out = {
    "SORTED_ASC" => _s.is_sorted_flag,
    "SORTED_DESC" => _s.is_sorted_reverse_flag
  }
  if dtype.is_a?(List)
    out["FAST_EXPLODE"] = _s.can_fast_explode_flag
  end
  out
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:



3395
3396
3397
# File 'lib/polars/series.rb', line 3395

def floor
  Utils.wrap_s(_s.floor)
end

#forward_fill(limit: nil) ⇒ Series

Fill missing values with the next non-null value.

This is an alias of .fill_null(strategy: "forward").

Parameters:

  • limit (Integer) (defaults to: nil)

    The number of consecutive null values to forward fill.

Returns:



3374
3375
3376
# File 'lib/polars/series.rb', line 3374

def forward_fill(limit: nil)
  fill_null(strategy: "forward", limit: limit)
end

#gather(indices, null_on_oob: false) ⇒ Series

Take values by index.

Examples:

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

Parameters:

  • indices (Array)

    Index location used for selection.

  • null_on_oob (Boolean) (defaults to: false)

    Behavior if an index is out of bounds:

    • true -> set the result to null
    • false -> raise an error

Returns:



2413
2414
2415
# File 'lib/polars/series.rb', line 2413

def gather(indices, null_on_oob: false)
  super
end

#gather_every(n, offset = 0) ⇒ Series

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

Examples:

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

Parameters:

  • n (Integer)

    Gather every n-th row.

  • offset (Integer) (defaults to: 0)

    Start the row index at this offset.

Returns:



1995
1996
1997
# File 'lib/polars/series.rb', line 1995

def gather_every(n, offset = 0)
  super
end

#ge(other) ⇒ Series

Method equivalent of operator expression series >= other.

Returns:



334
335
336
# File 'lib/polars/series.rb', line 334

def ge(other)
  self >= other
end

#get_chunksArray

Get the chunks of this Series as a list of Series.

Examples:

s1 = Polars::Series.new("a", [1, 2, 3])
s2 = Polars::Series.new("a", [4, 5, 6])
s = Polars.concat([s1, s2], rechunk: false)
s.get_chunks
# =>
# [shape: (3,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
# ], shape: (3,)
# Series: 'a' [i64]
# [
#         4
#         5
#         6
# ]]

Returns:



6281
6282
6283
# File 'lib/polars/series.rb', line 6281

def get_chunks
  _s.get_chunks
end

#gt(other) ⇒ Series

Method equivalent of operator expression series > other.

Returns:



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

def gt(other)
  self > other
end

#has_nullsBoolean

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.

Examples:

s = Polars::Series.new([1, 2, nil])
s.has_nulls
# => true
s[...2].has_nulls
# => false

Returns:



2444
2445
2446
# File 'lib/polars/series.rb', line 2444

def has_nulls
  _s.has_nulls
end

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

Hash the Series.

The hash value is of type UInt64.

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:



5369
5370
5371
# File 'lib/polars/series.rb', line 5369

def hash_(seed = 0, seed_1 = nil, seed_2 = nil, seed_3 = nil)
  super
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:



1912
1913
1914
1915
1916
1917
# File 'lib/polars/series.rb', line 1912

def head(n = 10)
  if n < 0
    n = [0, len + n].max
  end
  self.class._from_rbseries(_s.head(n))
end

#hist(bins: nil, bin_count: nil, include_category: true, include_breakpoint: true) ⇒ DataFrame

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Bin values into buckets and count their occurrences.

Examples:

a = Polars::Series.new("a", [1, 3, 8, 8, 2, 1, 3])
a.hist(bin_count: 4)
# =>
# shape: (4, 3)
# ┌────────────┬─────────────┬───────┐
# │ breakpoint ┆ category    ┆ count │
# │ ---        ┆ ---         ┆ ---   │
# │ f64        ┆ cat         ┆ u32   │
# ╞════════════╪═════════════╪═══════╡
# │ 2.75       ┆ [1.0, 2.75] ┆ 3     │
# │ 4.5        ┆ (2.75, 4.5] ┆ 2     │
# │ 6.25       ┆ (4.5, 6.25] ┆ 0     │
# │ 8.0        ┆ (6.25, 8.0] ┆ 2     │
# └────────────┴─────────────┴───────┘

Parameters:

  • bins (Object) (defaults to: nil)

    Bin edges. If nil given, we determine the edges based on the data.

  • bin_count (Integer) (defaults to: nil)

    If bins is not provided, bin_count uniform bins are created that fully encompass the data.

  • include_category (Boolean) (defaults to: true)

    Include a column that shows the intervals as categories.

  • include_breakpoint (Boolean) (defaults to: true)

    Include a column that indicates the upper breakpoint.

Returns:



1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
# File 'lib/polars/series.rb', line 1412

def hist(
  bins: nil,
  bin_count: nil,
  include_category: true,
  include_breakpoint: true
)
  out = (
    to_frame
    .select_seq(
      F.col(name).hist(
        bins: bins,
        bin_count: bin_count,
        include_category: include_category,
        include_breakpoint: include_breakpoint
      )
    )
    .to_series
  )
  if !include_breakpoint && !include_category
    out.to_frame
  else
    out.struct.unnest
  end
end

#implodeSeries

Aggregate values into a list.

Examples:

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

Returns:



6298
6299
6300
# File 'lib/polars/series.rb', line 6298

def implode
  super
end

#index_of(element) ⇒ Object

Get the index of the first occurrence of a value, or nil if it's not found.

Examples:

s = Polars::Series.new("a", [1, nil, 17])
s.index_of(17)
# => 2
s.index_of(nil) # search for a null
# => 1
s.index_of(55).nil?
# => true

Parameters:

  • element (Object)

    Value to find.

Returns:



3233
3234
3235
# File 'lib/polars/series.rb', line 3233

def index_of(element)
  F.select(F.lit(self).index_of(element)).item
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' [f64]
# [
#         1.0
#         2.0
#         3.0
#         4.0
#         5.0
# ]

Returns:



5431
5432
5433
# File 'lib/polars/series.rb', line 5431

def interpolate(method: "linear")
  super
end

#interpolate_by(by) ⇒ Series

Fill null values using interpolation based on another column.

Examples:

Fill null values using linear interpolation.

s = Polars::Series.new("a", [1, nil, nil, 3])
by = Polars::Series.new("b", [1, 2, 7, 8])
s.interpolate_by(by)
# =>
# shape: (4,)
# Series: 'a' [f64]
# [
#         1.0
#         1.285714
#         2.714286
#         3.0
# ]

Parameters:

  • by (Expr)

    Column to interpolate values based on.

Returns:



5455
5456
5457
# File 'lib/polars/series.rb', line 5455

def interpolate_by(by)
  super
end

#is_between(lower_bound, upper_bound, closed: "both") ⇒ Series

Note:

If the value of the lower_bound is greater than that of the upper_bound then the result will be false, as no value can satisfy the condition.

Get a boolean mask of the values that are between the given lower/upper bounds.

Examples:

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

Use the closed argument to include or exclude the values at the bounds:

s.is_between(2, 4, closed: "left")
# =>
# shape: (5,)
# Series: 'num' [bool]
# [
#         false
#         true
#         true
#         false
#         false
# ]

You can also use strings as well as numeric/temporal values:

s = Polars::Series.new("s", ["a", "b", "c", "d", "e"])
s.is_between("b", "d", closed: "both")
# =>
# shape: (5,)
# Series: 's' [bool]
# [
#         false
#         true
#         true
#         true
#         false
# ]

Parameters:

  • lower_bound (Object)

    Lower bound value. Accepts expression input. Non-expression inputs (including strings) are parsed as literals.

  • upper_bound (Object)

    Upper bound value. Accepts expression input. Non-expression inputs (including strings) are parsed as literals.

  • closed ('both', 'left', 'right', 'none') (defaults to: "both")

    Define which sides of the interval are closed (inclusive).

Returns:



3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
# File 'lib/polars/series.rb', line 3052

def is_between(
  lower_bound,
  upper_bound,
  closed: "both"
)
  if closed == "none"
    out = (self > lower_bound) & (self < upper_bound)
  elsif closed == "both"
    out = (self >= lower_bound) & (self <= upper_bound)
  elsif closed == "right"
    out = (self > lower_bound) & (self <= upper_bound)
  elsif closed == "left"
    out = (self >= lower_bound) & (self < upper_bound)
  end

  if out.is_a?(Expr)
    out = F.select(out).to_series
  end

  out
end

#is_close(other, abs_tol: 0.0, rel_tol: 1.0e-09, nans_equal: false) ⇒ Series

Get a boolean mask of the values being close to the other values.

Examples:

s = Polars::Series.new("s", [1.0, 1.2, 1.4, 1.45, 1.6])
s.is_close(1.4, abs_tol: 0.1)
# =>
# shape: (5,)
# Series: 's' [bool]
# [
#         false
#         false
#         true
#         true
#         false
# ]

Parameters:

  • abs_tol (Float) (defaults to: 0.0)

    Absolute tolerance. This is the maximum allowed absolute difference between two values. Must be non-negative.

  • rel_tol (Float) (defaults to: 1.0e-09)

    Relative tolerance. This is the maximum allowed difference between two values, relative to the larger absolute value. Must be in the range [0, 1).

  • nans_equal (Boolean) (defaults to: false)

    Whether NaN values should be considered equal.

Returns:



3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
# File 'lib/polars/series.rb', line 3100

def is_close(
  other,
  abs_tol: 0.0,
  rel_tol: 1.0e-09,
  nans_equal: false
)
  F.select(
    F.lit(self).is_close(
      other, abs_tol: abs_tol, rel_tol: rel_tol, nans_equal: nans_equal
    )
  ).to_series
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:



2793
2794
2795
# File 'lib/polars/series.rb', line 2793

def is_duplicated
  super
end

#is_empty(ignore_nulls: false) ⇒ Boolean Also known as: empty?

Check if the Series is empty.

Examples:

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

Parameters:

  • ignore_nulls (Boolean) (defaults to: false)

    If true a series containing only nulls will also be considered empty. The default is false.

Returns:



2460
2461
2462
2463
2464
2465
2466
2467
# File 'lib/polars/series.rb', line 2460

def is_empty(ignore_nulls: false)
  if ignore_nulls
    msg = "the `ignore_nulls` parameter of `Series.is_empty()` is considered unstable."
    Utils.issue_unstable_warning(msg)
  end

  _s.is_empty(ignore_nulls: ignore_nulls)
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:



2567
2568
2569
# File 'lib/polars/series.rb', line 2567

def is_finite
  super
end

#is_first_distinctSeries

Get a mask of the first unique value.

Examples:

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

Returns:



2752
2753
2754
# File 'lib/polars/series.rb', line 2752

def is_first_distinct
  super
end

#is_in(other, nulls_equal: false) ⇒ Series Also known as: in?

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, nil])
s2.is_in(s)
# =>
# shape: (3,)
# Series: 'b' [bool]
# [
#         true
#         false
#         null
# ]
s2.is_in(s, nulls_equal: true)
# =>
# shape: (3,)
# Series: 'b' [bool]
# [
#         true
#         false
#         false
# ]
sets = Polars::Series.new("sets", [[1, 2, 3], [1, 2], [9, 10]])
# =>
# shape: (3,)
# Series: 'sets' [list[i64]]
# [
#         [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
# ]

Parameters:

  • nulls_equal (Boolean) (defaults to: false)

    If true, treat null as a distinct value. Null values will not propagate.

Returns:



2693
2694
2695
# File 'lib/polars/series.rb', line 2693

def is_in(other, nulls_equal: false)
  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:



2586
2587
2588
# File 'lib/polars/series.rb', line 2586

def is_infinite
  super
end

#is_last_distinctSeries

Return a boolean mask indicating the last occurrence of each distinct value.

Examples:

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

Returns:



2773
2774
2775
# File 'lib/polars/series.rb', line 2773

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



2606
2607
2608
# File 'lib/polars/series.rb', line 2606

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:



2626
2627
2628
# File 'lib/polars/series.rb', line 2626

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:



2548
2549
2550
# File 'lib/polars/series.rb', line 2548

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:



2528
2529
2530
# File 'lib/polars/series.rb', line 2528

def is_null
  super
end

#is_sorted(descending: false, nulls_last: false) ⇒ Boolean Also known as: sorted?

Check if the Series is sorted.

Examples:

s = Polars::Series.new([1, 3, 2])
s.is_sorted
# => false
s = Polars::Series.new([3, 2, 1])
s.is_sorted(descending: true)
# => true

Parameters:

  • descending (Boolean) (defaults to: false)

    Check if the Series is sorted in descending order

  • nulls_last (Boolean) (defaults to: false)

    Set nulls at the end of the Series in sorted check.

Returns:



2488
2489
2490
# File 'lib/polars/series.rb', line 2488

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



2731
2732
2733
# File 'lib/polars/series.rb', line 2731

def is_unique
  super
end

#item(index = nil) ⇒ Object

Return the Series as a scalar, or return the element at the given index.

If no index is provided, this is equivalent to s[0], with a check that the shape is (1,). With an index, this is equivalent to s[index].

Examples:

s1 = Polars::Series.new("a", [1])
s1.item
# => 1
s2 = Polars::Series.new("a", [9, 8, 7])
s2.cum_sum.item(-1)
# => 24

Returns:



522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/polars/series.rb', line 522

def item(index = nil)
  if index.nil?
    if len != 1
      msg = (
        "can only call '.item' if the Series is of length 1," +
        " or an explicit index is provided (Series is of length #{len})"
      )
      raise ArgumentError, msg
    end
    return _s.get_index(0)
  end

  _s.get_index_signed(index)
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

Examples:

s = Polars::Series.new("grades", [66, 79, 54, 97, 96, 70, 69, 85, 93, 75])
s.kurtosis
# => -1.0522623626787952
s.kurtosis(fisher: false)
# => 1.9477376373212048
s.kurtosis(fisher: false, bias: false)
# => 2.1040361802642717

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)


5686
5687
5688
# File 'lib/polars/series.rb', line 5686

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

#last(ignore_nulls: false) ⇒ Object

Get the last element of the Series.

Returns nil if the Series is empty.

Parameters:

  • ignore_nulls (Boolean) (defaults to: false)

    Ignore null values (default false). If set to true, the last non-null value is returned, otherwise nil is returned if no non-null value exists.

Returns:



6389
6390
6391
# File 'lib/polars/series.rb', line 6389

def last(ignore_nulls: false)
  _s.last(ignore_nulls)
end

#le(other) ⇒ Series

Method equivalent of operator expression series <= other.

Returns:



228
229
230
# File 'lib/polars/series.rb', line 228

def le(other)
  self <= other
end

#lenInteger Also known as: length, size

Return the number of elements in the Series.

Examples:

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

Returns:

  • (Integer)


2870
2871
2872
# File 'lib/polars/series.rb', line 2870

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:



1962
1963
1964
# File 'lib/polars/series.rb', line 1962

def limit(n = 10)
  head(n)
end

#listListNameSpace

Create an object namespace of all list related methods.

Returns:



6405
6406
6407
# File 'lib/polars/series.rb', line 6405

def list
  ListNameSpace.new(self)
end

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

Compute the logarithm to a given base.

Examples:

s = Polars::Series.new([1, 2, 3])
s.log
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         0.0
#         0.693147
#         1.098612
# ]

Parameters:

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

    Given base, defaults to Math::E.

Returns:



696
697
698
# File 'lib/polars/series.rb', line 696

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

#log10Series

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

Examples:

s = Polars::Series.new([10, 100, 1000])
s.log10
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         1.0
#         2.0
#         3.0
# ]

Returns:



734
735
736
# File 'lib/polars/series.rb', line 734

def log10
  super
end

#log1pSeries

Compute the natural logarithm of the input array plus one, element-wise.

Examples:

s = Polars::Series.new([1, 2, 3])
s.log1p
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         0.693147
#         1.098612
#         1.386294
# ]

Returns:



715
716
717
# File 'lib/polars/series.rb', line 715

def log1p
  super
end

#lower_boundSeries

Return the lower bound of this Series' dtype as a unit Series.

Examples:

s = Polars::Series.new("s", [-1, 0, 1], dtype: Polars::Int32)
s.lower_bound
# =>
# shape: (1,)
# Series: 's' [i32]
# [
#         -2147483648
# ]
s = Polars::Series.new("s", [1.0, 2.5, 3.0], dtype: Polars::Float32)
s.lower_bound
# =>
# shape: (1,)
# Series: 's' [f32]
# [
#         -inf
# ]

Returns:



5742
5743
5744
# File 'lib/polars/series.rb', line 5742

def lower_bound
  super
end

#lt(other) ⇒ Series

Method equivalent of operator expression series < other.

Returns:



235
236
237
# File 'lib/polars/series.rb', line 235

def lt(other)
  self < other
end

#map_elements(return_dtype: nil, skip_nulls: true, &function) ⇒ Series Also known as: map

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.map_elements(return_dtype: Polars::Int64) { |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:



3858
3859
3860
3861
3862
3863
3864
3865
# File 'lib/polars/series.rb', line 3858

def map_elements(return_dtype: nil, skip_nulls: true, &function)
  if return_dtype.nil?
    pl_return_dtype = nil
  else
    pl_return_dtype = Utils.parse_into_dtype(return_dtype)
  end
  Utils.wrap_s(_s.map_elements(function, pl_return_dtype, skip_nulls))
end

#maxObject

Get the maximum value in this Series.

Examples:

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

Returns:



1014
1015
1016
# File 'lib/polars/series.rb', line 1014

def max
  _s.max
end

#max_by(by) ⇒ Object

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Get the maximum value in this Series, ordered by an expression.

If the by expression has multiple values equal to the maximum it is not defined which value will be chosen.

Examples:

s = Polars::Series.new("a", [-2.0, Float::NAN, 1.0])
s.max_by(Polars.col("a").abs)
# => -2.0

Parameters:

  • by (Object)

    Column used to determine the largest element. Accepts expression input. Strings are parsed as column names.

Returns:



1037
1038
1039
# File 'lib/polars/series.rb', line 1037

def max_by(by)
  to_frame.select_seq(F.col(name).max_by(by)).item
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)


917
918
919
# File 'lib/polars/series.rb', line 917

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)


1123
1124
1125
# File 'lib/polars/series.rb', line 1123

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:



979
980
981
# File 'lib/polars/series.rb', line 979

def min
  _s.min
end

#min_by(by) ⇒ Expr

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Get the minimum value in this Series, ordered by an expression.

If the by expression has multiple values equal to the minimum it is not defined which value will be chosen.

Examples:

s = Polars::Series.new("a", [-2.0, Float::NAN, 1.0])
s.min_by(Polars.col("a").abs)
# => 1.0

Parameters:

  • by (Object)

    Column used to determine the smallest element. Accepts expression input. Strings are parsed as column names.

Returns:



1002
1003
1004
# File 'lib/polars/series.rb', line 1002

def min_by(by)
  to_frame.select_seq(F.col(name).min_by(by)).item
end

#mode(maintain_order: false) ⇒ Series

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

Parameters:

  • maintain_order (Boolean) (defaults to: false)

    Maintain order of data. This requires more work.

Returns:



3554
3555
3556
# File 'lib/polars/series.rb', line 3554

def mode(maintain_order: false)
  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], rechunk: true).n_chunks
# => 1

Concatenate Series with rechunk: false

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

Returns:

  • (Integer)


1644
1645
1646
# File 'lib/polars/series.rb', line 1644

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)


5322
5323
5324
# File 'lib/polars/series.rb', line 5322

def n_unique
  _s.n_unique
end

#nameString

Get the name of this Series.

Examples:

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

Returns:



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

def name
  _s.name
end

#nan_maxObject

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

Examples:

s = Polars::Series.new("a", [1, 3, 4])
s.nan_max
# => 4
s = Polars::Series.new("a", [1.0, Float::NAN, 4.0])
s.nan_max
# => NaN

Returns:



1054
1055
1056
# File 'lib/polars/series.rb', line 1054

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

#nan_minObject

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

Examples:

s = Polars::Series.new("a", [1, 3, 4])
s.nan_min
# => 1
s = Polars::Series.new("a", [1.0, Float::NAN, 4.0])
s.nan_min
# => NaN

Returns:



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

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

#ne(other) ⇒ Series

Method equivalent of operator expression series != other.

Returns:



288
289
290
# File 'lib/polars/series.rb', line 288

def ne(other)
  self != other
end

#ne_missing(other) ⇒ Object

Method equivalent of equality operator series != other where nil == nil.

This differs from the standard ne where null values are propagated.

Examples:

s1 = Polars::Series.new("a", [333, 200, nil])
s2 = Polars::Series.new("a", [100, 200, nil])
s1.ne(s2)
# =>
# shape: (3,)
# Series: 'a' [bool]
# [
#         true
#         false
#         null
# ]
s1.ne_missing(s2)
# =>
# shape: (3,)
# Series: 'a' [bool]
# [
#         true
#         false
#         false
# ]

Parameters:

  • other (Object)

    A literal or expression value to compare with.

Returns:



324
325
326
327
328
329
# File 'lib/polars/series.rb', line 324

def ne_missing(other)
  if other.is_a?(Expr)
    return Polars.lit(self).ne_missing(other)
  end
  to_frame.select(Polars.col(name).ne_missing(other)).to_series
end

#new_from_index(index, length) ⇒ Series

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

Examples:

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

Returns:



6229
6230
6231
# File 'lib/polars/series.rb', line 6229

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

#none?(&block) ⇒ Boolean Also known as: none

Check if all boolean values in the column are false.

Examples:

Polars::Series.new([true, false]).none?
# => false
Polars::Series.new([false, false]).none?
# => true
Polars::Series.new([nil, false]).none?
# => true

Returns:



669
670
671
672
673
674
675
# File 'lib/polars/series.rb', line 669

def none?(&block)
  if block_given?
    map_elements(return_dtype: Boolean, &block).none?
  else
    to_frame.select(Polars.col(name).is_not.all).to_series[0]
  end
end

#not_Series

Negate a boolean Series.

Examples:

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

Returns:



2508
2509
2510
# File 'lib/polars/series.rb', line 2508

def not_
  self.class._from_rbseries(_s.not_)
end

#null_countInteger

Count the null values in this Series.

Examples:

s = Polars::Series.new([1, nil, nil])
s.null_count
# => 2

Returns:

  • (Integer)


2425
2426
2427
# File 'lib/polars/series.rb', line 2425

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:



5633
5634
5635
# File 'lib/polars/series.rb', line 5633

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: 'a' [bool]
# [
#         false
#         false
#         false
#         false
#         true
# ]

Returns:



5289
5290
5291
# File 'lib/polars/series.rb', line 5289

def peak_max
  super
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: 'a' [bool]
# [
#         false
#         true
#         false
#         true
#         false
# ]

Returns:



5310
5311
5312
# File 'lib/polars/series.rb', line 5310

def peak_min
  super
end

#plotSeriesPlot

Note:

This functionality is currently considered unstable. It may be changed at any point without it being considered a breaking change.

Create a plot namespace.

Examples:

Histogram:

s = Polars::Series.new([1, 4, 4, 6, 2, 4, 3, 5, 5, 7, 1])
s.plot.hist

Returns:



6469
6470
6471
# File 'lib/polars/series.rb', line 6469

def plot
  SeriesPlot.new(self)
end

#pow(exponent) ⇒ Series

Raise to the power of the given exponent.

If the exponent is float, the result follows the dtype of exponent. Otherwise, it follows dtype of base.

Examples:

Raising integers to positive integers results in integers:

s = Polars::Series.new("foo", [1, 2, 3, 4])
s.pow(3)
# =>
# shape: (4,)
# Series: 'foo' [i64]
# [
#         1
#         8
#         27
#         64
# ]

In order to raise integers to negative integers, you can cast either the base or the exponent to float:

s.pow(-3.0)
# =>
# shape: (4,)
# Series: 'foo' [f64]
# [
#         1.0
#         0.125
#         0.037037
#         0.015625
# ]

Parameters:

  • exponent (Numeric)

    The exponent. Accepts Series input.

Returns:



967
968
969
# File 'lib/polars/series.rb', line 967

def pow(exponent)
  to_frame.select_seq(F.col(name).pow(exponent)).to_series
end

#productNumeric

Reduce this Series to the product value.

Examples:

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

Returns:

  • (Numeric)


929
930
931
# File 'lib/polars/series.rb', line 929

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

#qcut(quantiles, labels: nil, left_closed: false, allow_duplicates: false, include_breaks: false) ⇒ Series

Bin continuous values into discrete categories based on their quantiles.

Examples:

Divide a column into three categories according to pre-defined quantile probabilities.

s = Polars::Series.new("foo", [-2, -1, 0, 1, 2])
s.qcut([0.25, 0.75], labels: ["a", "b", "c"])
# =>
# shape: (5,)
# Series: 'foo' [cat]
# [
#         "a"
#         "a"
#         "b"
#         "b"
#         "c"
# ]

Divide a column into two categories using uniform quantile probabilities.

s.qcut(2, labels: ["low", "high"], left_closed: true)
# =>
# shape: (5,)
# Series: 'foo' [cat]
# [
#         "low"
#         "low"
#         "high"
#         "high"
#         "high"
# ]

Create a DataFrame with the breakpoint and category for each value.

cut = s.qcut([0.25, 0.75], include_breaks: true).alias("cut")
s.to_frame.with_columns(cut).unnest("cut")
# =>
# shape: (5, 3)
# ┌─────┬─────────────┬────────────┐
# │ foo ┆ break_point ┆ category   │
# │ --- ┆ ---         ┆ ---        │
# │ i64 ┆ f64         ┆ cat        │
# ╞═════╪═════════════╪════════════╡
# │ -2  ┆ -1.0        ┆ (-inf, -1] │
# │ -1  ┆ -1.0        ┆ (-inf, -1] │
# │ 0   ┆ 1.0         ┆ (-1, 1]    │
# │ 1   ┆ 1.0         ┆ (-1, 1]    │
# │ 2   ┆ inf         ┆ (1, inf]   │
# └─────┴─────────────┴────────────┘

Parameters:

  • quantiles (Object)

    Either an array of quantile probabilities between 0 and 1 or a positive integer determining the number of bins with uniform probability.

  • labels (Array) (defaults to: nil)

    Names of the categories. The number of labels must be equal to the number of cut points plus one.

  • left_closed (Boolean) (defaults to: false)

    Set the intervals to be left-closed instead of right-closed.

  • allow_duplicates (Boolean) (defaults to: false)

    If set to true, duplicates in the resulting quantiles are dropped, rather than raising a DuplicateError. This can happen even with unique probabilities, depending on the data.

  • include_breaks (Boolean) (defaults to: false)

    Include a column with the right endpoint of the bin each observation falls in. This will change the data type of the output from a Categorical to a Struct.

Returns:



1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'lib/polars/series.rb', line 1304

def qcut(quantiles, labels: nil, left_closed: false, allow_duplicates: false, include_breaks: false)
  result = (
    to_frame
    .select(
      Polars.col(name).qcut(
        quantiles,
        labels: labels,
        left_closed: left_closed,
        allow_duplicates: allow_duplicates,
        include_breaks: include_breaks
      )
    )
    .to_series
  )

  if include_breaks
    result = result.struct.rename_fields(["break_point", "category"])
  end

  result
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)


1140
1141
1142
# File 'lib/polars/series.rb', line 1140

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

#rank(method: "average", descending: false, seed: nil) ⇒ 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' [f64]
# [
#         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.
  • descending (Boolean) (defaults to: false)

    Reverse the operation.

  • seed (Integer) (defaults to: nil)

    If method: "random", use this as seed.

Returns:



5532
5533
5534
# File 'lib/polars/series.rb', line 5532

def rank(method: "average", descending: false, seed: nil)
  super
end

#rechunk(in_place: false) ⇒ Series

Create a single chunk of memory for this Series.

Examples:

s1 = Polars::Series.new("a", [1, 2, 3])
s1.n_chunks
# => 1
s2 = Polars::Series.new("a", [4, 5, 6])
s = Polars.concat([s1, s2], rechunk: false)
s.n_chunks
# => 2
s.rechunk(in_place: true)
# =>
# shape: (6,)
# Series: 'a' [i64]
# [
#         1
#         2
#         3
#         4
#         5
#         6
# ]
s.n_chunks
# => 1

Parameters:

  • in_place (Boolean) (defaults to: false)

    In place or not.

Returns:



2971
2972
2973
2974
# File 'lib/polars/series.rb', line 2971

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

#reinterpret(signed: nil, dtype: nil) ⇒ Series

Reinterpret the underlying bits as a signed/unsigned integer or float.

This operation is only allowed for numeric types of the same size. For lower bits numbers, you can safely use the cast operation.

Either signed or dtype can be specified.

Examples:

s = Polars::Series.new("a", [-(2**60), -2, 3])
s.reinterpret(signed: false)
# =>
# shape: (3,)
# Series: 'a' [u64]
# [
#         17293822569102704640
#         18446744073709551614
#         3
# ]
s.reinterpret(dtype: Polars::Int64)
# =>
# shape: (3,)
# Series: 'a' [i64]
# [
#         -1152921504606846976
#         -2
#         3
# ]

Parameters:

  • signed (Boolean) (defaults to: nil)

    If true, reinterpret as signed integer. Otherwise, reinterpret as unsigned integer.

  • dtype (Object) (defaults to: nil)

    DataType to reinterpret to.

Returns:



5410
5411
5412
# File 'lib/polars/series.rb', line 5410

def reinterpret(signed: nil, dtype: nil)
  super
end

#rename(name) ⇒ Series

Rename this Series.

Examples:

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

Parameters:

Returns:



1606
1607
1608
# File 'lib/polars/series.rb', line 1606

def rename(name)
  self.alias(name)
end

#repeat_by(by) ⇒ Object

Repeat the elements in this Series as specified in the given expression.

The repeated elements are expanded into a List.

Parameters:

  • by (Object)

    Numeric column that determines how often the values will be repeated. The column will be coerced to UInt32. Give this dtype to make the coercion a no-op.

Returns:



6483
6484
6485
# File 'lib/polars/series.rb', line 6483

def repeat_by(by)
  super
end

#replace(old, new = NO_DEFAULT, default: NO_DEFAULT, return_dtype: nil) ⇒ Series

Replace values by different values.

Examples:

Replace a single value by another value. Values that were not replaced remain unchanged.

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

Replace multiple values by passing arrays to the old and new parameters.

s.replace([2, 3], [100, 200])
# =>
# shape: (4,)
# Series: '' [i64]
# [
#         1
#         100
#         100
#         200
# ]

Passing a mapping with replacements is also supported as syntactic sugar.

mapping = {2 => 100, 3 => 200}
s.replace(mapping)
# =>
# shape: (4,)
# Series: '' [i64]
# [
#         1
#         100
#         100
#         200
# ]

The original data type is preserved when replacing by values of a different data type.

s = Polars::Series.new(["x", "y", "z"])
mapping = {"x" => 1, "y" => 2, "z" => 3}
s.replace(mapping)
# =>
# shape: (3,)
# Series: '' [str]
# [
#         "1"
#         "2"
#         "3"
# ]

Parameters:

  • old (Object)

    Value or array of values to replace. Also accepts a mapping of values to their replacement.

  • new (Object) (defaults to: NO_DEFAULT)

    Value or array of values to replace by. Length must match the length of old or have length 1.

  • default (Object) (defaults to: NO_DEFAULT)

    Set values that were not replaced to this value. Defaults to keeping the original value. Accepts expression input. Non-expression inputs are parsed as literals.

  • return_dtype (Object) (defaults to: nil)

    The data type of the resulting Series. If set to nil (default), the data type is determined automatically based on the other inputs.

Returns:



5841
5842
5843
# File 'lib/polars/series.rb', line 5841

def replace(old, new = NO_DEFAULT, default: NO_DEFAULT, return_dtype: nil)
  super
end

#replace_strict(old, new = NO_DEFAULT, default: NO_DEFAULT, return_dtype: nil) ⇒ Series

Replace all values by different values.

Examples:

Replace values by passing arrays to the old and new parameters.

s = Polars::Series.new([1, 2, 2, 3])
s.replace_strict([1, 2, 3], [100, 200, 300])
# =>
# shape: (4,)
# Series: '' [i64]
# [
#         100
#         200
#         200
#         300
# ]

Passing a mapping with replacements is also supported as syntactic sugar.

mapping = {1 => 100, 2 => 200, 3 => 300}
s.replace_strict(mapping)
# =>
# shape: (4,)
# Series: '' [i64]
# [
#         100
#         200
#         200
#         300
# ]

By default, an error is raised if any non-null values were not replaced. Specify a default to set all values that were not matched.

mapping = {2 => 200, 3 => 300}
s.replace_strict(mapping, default: -1)
# =>
# shape: (4,)
# Series: '' [i64]
# [
#         -1
#         200
#         200
#         300
# ]

The default can be another Series.

default = Polars::Series.new([2.5, 5.0, 7.5, 10.0])
s.replace_strict(2, 200, default: default)
# =>
# shape: (4,)
# Series: '' [f64]
# [
#         2.5
#         200.0
#         200.0
#         10.0
# ]

Replacing by values of a different data type sets the return type based on a combination of the new data type and the default data type.

s = Polars::Series.new(["x", "y", "z"])
mapping = {"x" => 1, "y" => 2, "z" => 3}
s.replace_strict(mapping)
# =>
# shape: (3,)
# Series: '' [i64]
# [
#         1
#         2
#         3
# ]
s.replace_strict(mapping, default: "x")
# =>
# shape: (3,)
# Series: '' [str]
# [
#         "1"
#         "2"
#         "3"
# ]

Set the return_dtype parameter to control the resulting data type directly.

s.replace_strict(mapping, return_dtype: Polars::UInt8)
# =>
# shape: (3,)
# Series: '' [u8]
# [
#         1
#         2
#         3
# ]

Parameters:

  • old (Object)

    Value or array of values to replace. Also accepts a mapping of values to their replacement as syntactic sugar for replace_strict(old: Polars::Series.new(mapping.keys), new: Polars::Series.new(mapping.values)).

  • new (Object) (defaults to: NO_DEFAULT)

    Value or array of values to replace by. Length must match the length of old or have length 1.

  • default (Object) (defaults to: NO_DEFAULT)

    Set values that were not replaced to this value. If no default is specified, (default), an error is raised if any values were not replaced. Accepts expression input. Non-expression inputs are parsed as literals.

  • return_dtype (Object) (defaults to: nil)

    The data type of the resulting Series. If set to nil (default), the data type is determined automatically based on the other inputs.

Returns:



5950
5951
5952
5953
5954
5955
5956
5957
# File 'lib/polars/series.rb', line 5950

def replace_strict(
  old,
  new = NO_DEFAULT,
  default: NO_DEFAULT,
  return_dtype: nil
)
  super
end

#reshape(dimensions) ⇒ Series

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

Examples:

s = Polars::Series.new("foo", [1, 2, 3, 4, 5, 6, 7, 8, 9])
square = s.reshape([3, 3])
# =>
# shape: (3,)
# Series: 'foo' [array[i64, 3]]
# [
#         [1, 2, 3]
#         [4, 5, 6]
#         [7, 8, 9]
# ]
square.reshape([9])
# =>
# shape: (9,)
# Series: 'foo' [i64]
# [
#         1
#         2
#         3
#         4
#         5
#         6
#         7
#         8
#         9
# ]

Parameters:

  • dimensions (Array)

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

Returns:



5995
5996
5997
# File 'lib/polars/series.rb', line 5995

def reshape(dimensions)
  self.class._from_rbseries(_s.reshape(dimensions))
end

#reverseSeries

Return Series in reverse order.

Examples:

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

Returns:



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

def reverse
  super
end

#rleSeries

Get the lengths of runs of identical values.

Examples:

s = Polars::Series.new("s", [1, 1, 2, 1, nil, 1, 3, 3])
s.rle.struct.unnest
# =>
# shape: (6, 2)
# ┌─────┬───────┐
# │ len ┆ value │
# │ --- ┆ ---   │
# │ u32 ┆ i64   │
# ╞═════╪═══════╡
# │ 2   ┆ 1     │
# │ 1   ┆ 2     │
# │ 1   ┆ 1     │
# │ 1   ┆ null  │
# │ 1   ┆ 1     │
# │ 2   ┆ 3     │
# └─────┴───────┘

Returns:



1347
1348
1349
# File 'lib/polars/series.rb', line 1347

def rle
  super
end

#rle_idSeries

Map values to run IDs.

Similar to RLE, but it maps each value to an ID corresponding to the run into which it falls. This is especially useful when you want to define groups by runs of identical values rather than the values themselves.

Examples:

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

Returns:



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

def rle_id
  super
end

#rolling_kurtosis(window_size, fisher: true, bias: true, min_samples: nil, center: false) ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Compute a rolling kurtosis.

The window at a given row will include the row itself, and the window_size - 1 elements before it.

Examples:

Polars::Series.new([1, 4, 2, 9]).rolling_kurtosis(3)
# =>
# shape: (4,)
# Series: '' [f64]
# [
#         null
#         null
#         -1.5
#         -1.5
# ]

Parameters:

  • window_size (Integer)

    Integer size of the rolling window.

  • 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.

  • min_samples (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If set to nil (default), it will be set equal to window_size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window.

Returns:



5225
5226
5227
5228
5229
5230
5231
5232
5233
# File 'lib/polars/series.rb', line 5225

def rolling_kurtosis(
  window_size,
  fisher: true,
  bias: true,
  min_samples: nil,
  center: false
)
  super
end

#rolling_map(window_size, weights: nil, min_samples: nil, center: false, &function) ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Compute a custom rolling window function.

Examples:

s = Polars::Series.new([11.0, 2.0, 9.0, Float::NAN, 8.0])
s.rolling_map(3) { |v| v.drop_nans.sum }
# =>
# shape: (5,)
# Series: '' [f64]
# [
#         null
#         null
#         22.0
#         11.0
#         17.0
# ]

Parameters:

  • window_size (Integer)

    The length of the window in number of elements.

  • weights (Object) (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_samples (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If set to nil (default), it will be set equal to window_size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window.

Returns:



4744
4745
4746
4747
4748
4749
4750
4751
4752
# File 'lib/polars/series.rb', line 4744

def rolling_map(
  window_size,
  weights: nil,
  min_samples: nil,
  center: false,
  &function
)
  super
end

#rolling_max(window_size, weights: nil, min_samples: 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_samples (Integer) (defaults to: nil)

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

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



4189
4190
4191
4192
4193
4194
4195
4196
# File 'lib/polars/series.rb', line 4189

def rolling_max(
  window_size,
  weights: nil,
  min_samples: nil,
  center: false
)
  super
end

#rolling_max_by(by, window_size, min_samples: 1, closed: "right") ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using rolling - this method can cache the window size computation.

Compute a rolling max based on another series.

Given a by column <t_0, t_1, ..., t_n>, then closed="right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Examples:

start = DateTime.new(2001, 1, 1)
stop = DateTime.new(2001, 1, 2)
s = Polars::Series.new("index", 25.times.to_a)
d = Polars::Series.new("date", Polars.datetime_range(start, stop, "1h", eager: true))
s.rolling_max_by(d, "3h")
# =>
# shape: (25,)
# Series: 'index' [i64]
# [
#         0
#         1
#         2
#         3
#         4
#         …
#         20
#         21
#         22
#         23
#         24
# ]

Parameters:

  • by (Object)

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size (String)

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • min_samples (Integer) (defaults to: 1)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

Returns:



4148
4149
4150
4151
4152
4153
4154
4155
# File 'lib/polars/series.rb', line 4148

def rolling_max_by(
  by,
  window_size,
  min_samples: 1,
  closed: "right"
)
  super
end

#rolling_mean(window_size, weights: nil, min_samples: 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_samples (Integer) (defaults to: nil)

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

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



4314
4315
4316
4317
4318
4319
4320
4321
# File 'lib/polars/series.rb', line 4314

def rolling_mean(
  window_size,
  weights: nil,
  min_samples: nil,
  center: false
)
  super
end

#rolling_mean_by(by, window_size, min_samples: 1, closed: "right") ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using rolling - this method can cache the window size computation.

Compute a rolling mean based on another series.

Given a by column <t_0, t_1, ..., t_n>, then closed: "right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Examples:

start = DateTime.new(2001, 1, 1)
stop = DateTime.new(2001, 1, 2)
s = Polars::Series.new("index", 25.times.to_a)
d = Polars::Series.new("date", Polars.datetime_range(start, stop, "1h", eager: true))
s.rolling_mean_by(d, "3h")
# =>
# shape: (25,)
# Series: 'index' [f64]
# [
#         0.0
#         0.5
#         1.0
#         2.0
#         3.0
#         …
#         19.0
#         20.0
#         21.0
#         22.0
#         23.0
# ]

Parameters:

  • by (Object)

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size (String)

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • min_samples (Integer) (defaults to: 1)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

Returns:



4273
4274
4275
4276
4277
4278
4279
4280
# File 'lib/polars/series.rb', line 4273

def rolling_mean_by(
  by,
  window_size,
  min_samples: 1,
  closed: "right"
)
  super
end

#rolling_median(window_size, weights: nil, min_samples: 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_samples (Integer) (defaults to: nil)

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

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



4867
4868
4869
4870
4871
4872
4873
4874
# File 'lib/polars/series.rb', line 4867

def rolling_median(
  window_size,
  weights: nil,
  min_samples: nil,
  center: false
)
  super
end

#rolling_median_by(by, window_size, min_samples: 1, closed: "right") ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using rolling - this method can cache the window size computation.

Compute a rolling median based on another series.

Given a by column <t_0, t_1, ..., t_n>, then closed: "right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Examples:

start = DateTime.new(2001, 1, 1)
stop = DateTime.new(2001, 1, 2)
s = Polars::Series.new("index", 25.times.to_a)
d = Polars::Series.new("date", Polars.datetime_range(start, stop, "1h", eager: true))
s.rolling_median_by(d, "3h")
# =>
# shape: (25,)
# Series: 'index' [f64]
# [
#         0.0
#         0.5
#         1.0
#         2.0
#         3.0
#         …
#         19.0
#         20.0
#         21.0
#         22.0
#         23.0
# ]

Parameters:

  • by (Object)

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size (String)

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • min_samples (Integer) (defaults to: 1)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

Returns:



4829
4830
4831
4832
4833
4834
4835
4836
# File 'lib/polars/series.rb', line 4829

def rolling_median_by(
  by,
  window_size,
  min_samples: 1,
  closed: "right"
)
  super
end

#rolling_min(window_size, weights: nil, min_samples: 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_samples (Integer) (defaults to: nil)

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

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



4064
4065
4066
4067
4068
4069
4070
4071
# File 'lib/polars/series.rb', line 4064

def rolling_min(
  window_size,
  weights: nil,
  min_samples: nil,
  center: false
)
  super
end

#rolling_min_by(by, window_size, min_samples: 1, closed: "right") ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using rolling - this method can cache the window size computation.

Compute a rolling min based on another series.

Given a by column <t_0, t_1, ..., t_n>, then closed: "right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Examples:

start = DateTime.new(2001, 1, 1)
stop = DateTime.new(2001, 1, 2)
s = Polars::Series.new("index", 25.times.to_a)
d = Polars::Series.new("date", Polars.datetime_range(start, stop, "1h", eager: true))
s.rolling_min_by(d, "3h")
# =>
# shape: (25,)
# Series: 'index' [i64]
# [
#         0
#         0
#         0
#         1
#         2
#         …
#         18
#         19
#         20
#         21
#         22
# ]

Parameters:

  • by (Object)

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size (String)

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • min_samples (Integer) (defaults to: 1)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

Returns:



4023
4024
4025
4026
4027
4028
4029
4030
# File 'lib/polars/series.rb', line 4023

def rolling_min_by(
  by,
  window_size,
  min_samples: 1,
  closed: "right"
)
  super
end

#rolling_quantile(quantile, interpolation: "nearest", window_size: 2, weights: nil, min_samples: 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
#         2.0
#         3.0
#         4.0
#         6.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_samples (Integer) (defaults to: nil)

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

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
# File 'lib/polars/series.rb', line 5013

def rolling_quantile(
  quantile,
  interpolation: "nearest",
  window_size: 2,
  weights: nil,
  min_samples: nil,
  center: false
)
  super
end

#rolling_quantile_by(by, window_size, quantile:, interpolation: "nearest", min_samples: 1, closed: "right") ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using rolling - this method can cache the window size computation.

Compute a rolling quantile based on another series.

Given a by column <t_0, t_1, ..., t_n>, then closed: "right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Examples:

start = DateTime.new(2001, 1, 1)
stop = DateTime.new(2001, 1, 2)
s = Polars::Series.new("index", 25.times.to_a)
d = Polars::Series.new("date", Polars.datetime_range(start, stop, "1h", eager: true))
s.rolling_quantile_by(d, "3h", quantile: 0.5)
# =>
# shape: (25,)
# Series: 'index' [f64]
# [
#         0.0
#         1.0
#         1.0
#         2.0
#         3.0
#         …
#         19.0
#         20.0
#         21.0
#         22.0
#         23.0
# ]

Parameters:

  • by (Object)

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size (String)

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • quantile (Float)

    Quantile between 0.0 and 1.0.

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

    Interpolation method.

  • min_samples (Integer) (defaults to: 1)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

Returns:



4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
# File 'lib/polars/series.rb', line 4955

def rolling_quantile_by(
  by,
  window_size,
  quantile:,
  interpolation: "nearest",
  min_samples: 1,
  closed: "right"
)
  super
end

#rolling_rank(window_size, method: "average", seed: nil, min_samples: nil, center: false) ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Compute a rolling rank.

A window of length window_size will traverse the array. The values that fill this window will be ranked according to the method parameter. The resulting values will be the rank of the value that is at the end of the sliding window.

Examples:

Polars::Series.new([1, 4, 4, 1, 9]).rolling_rank(3, method: "average")
# =>
# shape: (5,)
# Series: '' [f64]
# [
#         null
#         null
#         2.5
#         1.0
#         3.0
# ]

Parameters:

  • window_size (Integer)

    Integer size of the rolling window.

  • method ('average', 'min', 'max', 'dense', '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.
    • 'random' : Choose a random rank for each value in a tie.
  • seed (Integer) (defaults to: nil)

    Random seed used when method: 'random'. If set to nil (default), a random seed is generated for each rolling rank operation.

  • min_samples (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If set to nil (default), it will be set equal to window_size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window.

Returns:



5151
5152
5153
5154
5155
5156
5157
5158
5159
# File 'lib/polars/series.rb', line 5151

def rolling_rank(
  window_size,
  method: "average",
  seed: nil,
  min_samples: nil,
  center: false
)
  super
end

#rolling_rank_by(by, window_size, method: "average", seed: nil, min_samples: 1, closed: "right") ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Compute a rolling rank based on another column.

Given a by column <t_0, t_1, ..., t_n>, then closed="right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Parameters:

  • by (Expr)

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size (String)

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • method ('average', 'min', 'max', 'dense', '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.
    • 'random' : Choose a random rank for each value in a tie.
  • seed (Integer) (defaults to: nil)

    Random seed used when method: 'random'. If set to nil (default), a random seed is generated for each rolling rank operation.

  • min_samples (Integer) (defaults to: 1)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

Returns:



5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
# File 'lib/polars/series.rb', line 5089

def rolling_rank_by(
  by,
  window_size,
  method: "average",
  seed: nil,
  min_samples: 1,
  closed: "right"
)
  super
end

#rolling_skew(window_size, bias: true, min_samples: nil, center: false) ⇒ Series

Compute a rolling skew.

Examples:

Polars::Series.new([1, 4, 2, 9]).rolling_skew(3)
# =>
# shape: (4,)
# Series: '' [f64]
# [
#         null
#         null
#         0.381802
#         0.47033
# ]

Parameters:

  • window_size (Integer)

    Integer size of the rolling window.

  • bias (Boolean) (defaults to: true)

    If false, the calculations are corrected for statistical bias.

  • min_samples (Integer) (defaults to: nil)

    The number of values in the window that should be non-null before computing a result. If set to nil (default), it will be set equal to window_size.

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window.

Returns:



5186
5187
5188
# File 'lib/polars/series.rb', line 5186

def rolling_skew(window_size, bias: true, min_samples: nil, center: false)
  super
end

#rolling_std(window_size, weights: nil, min_samples: nil, center: false, ddof: 1) ⇒ 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_samples (Integer) (defaults to: nil)

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

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • ddof (Integer) (defaults to: 1)

    "Delta Degrees of Freedom": The divisor for a length N window is N - ddof

Returns:



4570
4571
4572
4573
4574
4575
4576
4577
4578
# File 'lib/polars/series.rb', line 4570

def rolling_std(
  window_size,
  weights: nil,
  min_samples: nil,
  center: false,
  ddof: 1
)
  super
end

#rolling_std_by(by, window_size, min_samples: 1, closed: "right", ddof: 1) ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using rolling - this method can cache the window size computation.

Compute a rolling standard deviation based on another series.

Given a by column <t_0, t_1, ..., t_n>, then closed="right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Examples:

start = DateTime.new(2001, 1, 1)
stop = DateTime.new(2001, 1, 2)
s = Polars::Series.new("index", 25.times.to_a)
d = Polars::Series.new("date", Polars.datetime_range(start, stop, "1h", eager: true))
s.rolling_std_by(d, "3h")
# =>
# shape: (25,)
# Series: 'index' [f64]
# [
#         null
#         0.707107
#         1.0
#         1.0
#         1.0
#         …
#         1.0
#         1.0
#         1.0
#         1.0
#         1.0
# ]

Parameters:

  • by (Object)

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size (String)

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • min_samples (Integer) (defaults to: 1)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

  • ddof (Integer) (defaults to: 1)

    "Delta Degrees of Freedom": The divisor for a length N window is N - ddof

Returns:



4525
4526
4527
4528
4529
4530
4531
4532
4533
# File 'lib/polars/series.rb', line 4525

def rolling_std_by(
  by,
  window_size,
  min_samples: 1,
  closed: "right",
  ddof: 1
)
  super
end

#rolling_sum(window_size, weights: nil, min_samples: 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_samples (Integer) (defaults to: nil)

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

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

Returns:



4439
4440
4441
4442
4443
4444
4445
4446
# File 'lib/polars/series.rb', line 4439

def rolling_sum(
  window_size,
  weights: nil,
  min_samples: nil,
  center: false
)
  super
end

#rolling_sum_by(by, window_size, min_samples: 0, closed: "right") ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using rolling - this method can cache the window size computation.

Compute a rolling sum based on another series.

Given a by column <t_0, t_1, ..., t_n>, then closed: "right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Examples:

start = DateTime.new(2001, 1, 1)
stop = DateTime.new(2001, 1, 2)
s = Polars::Series.new("index", 25.times.to_a)
d = Polars::Series.new("date", Polars.datetime_range(start, stop, "1h", eager: true))
s.rolling_sum_by(d, "3h")
# =>
# shape: (25,)
# Series: 'index' [i64]
# [
#         0
#         1
#         3
#         6
#         9
#         …
#         57
#         60
#         63
#         66
#         69
# ]

Parameters:

  • by (Object)

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size (String)

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • min_samples (Integer) (defaults to: 0)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

Returns:



4398
4399
4400
4401
4402
4403
4404
4405
# File 'lib/polars/series.rb', line 4398

def rolling_sum_by(
  by,
  window_size,
  min_samples: 0,
  closed: "right"
)
  super
end

#rolling_var(window_size, weights: nil, min_samples: nil, center: false, ddof: 1) ⇒ 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_samples (Integer) (defaults to: nil)

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

  • center (Boolean) (defaults to: false)

    Set the labels at the center of the window

  • ddof (Integer) (defaults to: 1)

    "Delta Degrees of Freedom": The divisor for a length N window is N - ddof

Returns:



4702
4703
4704
4705
4706
4707
4708
4709
4710
# File 'lib/polars/series.rb', line 4702

def rolling_var(
  window_size,
  weights: nil,
  min_samples: nil,
  center: false,
  ddof: 1
)
  super
end

#rolling_var_by(by, window_size, min_samples: 1, closed: "right", ddof: 1) ⇒ Series

Note:

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Note:

If you want to compute multiple aggregation statistics over the same dynamic window, consider using rolling - this method can cache the window size computation.

Compute a rolling variance based on another series.

Given a by column <t_0, t_1, ..., t_n>, then closed: "right" (the default) means the windows will be:

- (t_0 - window_size, t_0]
- (t_1 - window_size, t_1]
- ...
- (t_n - window_size, t_n]

Examples:

start = DateTime.new(2001, 1, 1)
stop = DateTime.new(2001, 1, 2)
s = Polars::Series.new("index", 25.times.to_a)
d = Polars::Series.new("date", Polars.datetime_range(start, stop, "1h", eager: true))
s.rolling_var_by(d, "3h")
# =>
# shape: (25,)
# Series: 'index' [f64]
# [
#         null
#         0.5
#         1.0
#         1.0
#         1.0
#         …
#         1.0
#         1.0
#         1.0
#         1.0
#         1.0
# ]

Parameters:

  • by

    Should be DateTime, Date, UInt64, UInt32, Int64, or Int32 data type (note that the integral ones require using 'i' in window size).

  • window_size

    The length of the window. Can be a dynamic temporal size indicated by a timedelta or the following string language:

    • 1ns (1 nanosecond)
    • 1us (1 microsecond)
    • 1ms (1 millisecond)
    • 1s (1 second)
    • 1m (1 minute)
    • 1h (1 hour)
    • 1d (1 calendar day)
    • 1w (1 calendar week)
    • 1mo (1 calendar month)
    • 1q (1 calendar quarter)
    • 1y (1 calendar year)
    • 1i (1 index count)

    By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".

  • min_samples (Integer) (defaults to: 1)

    The number of values in the window that should be non-null before computing a result.

  • closed ('left', 'right', 'both', 'none') (defaults to: "right")

    Define which sides of the temporal interval are closed (inclusive), defaults to 'right'.

  • ddof (defaults to: 1)

    "Delta Degrees of Freedom": The divisor for a length N window is N - ddof

Returns:



4657
4658
4659
4660
4661
4662
4663
4664
4665
# File 'lib/polars/series.rb', line 4657

def rolling_var_by(
  by,
  window_size,
  min_samples: 1,
  closed: "right",
  ddof: 1
)
  super
end

#round(decimals = 0, mode: "half_to_even") ⇒ Series

Round underlying floating point data by decimals digits.

The default rounding mode is "half to even" (also known as "bankers' rounding").

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.

  • mode ('half_to_even', 'half_away_from_zero') (defaults to: "half_to_even")

    Rounding mode.

Returns:



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

def round(decimals = 0, mode: "half_to_even")
  super
end

#round_sig_figs(digits) ⇒ Series

Round to a number of significant figures.

Examples:

s = Polars::Series.new([0.01234, 3.333, 3450.0])
s.round_sig_figs(2)
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         0.012
#         3.3
#         3500.0
# ]

Parameters:

  • digits (Integer)

    Number of significant figures to round to.

Returns:



3509
3510
3511
# File 'lib/polars/series.rb', line 3509

def round_sig_figs(digits)
  super
end

#sample(n: nil, fraction: 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]
# [
#         2
#         5
# ]

Parameters:

  • n (Integer) (defaults to: nil)

    Number of items to return. Cannot be used with fraction. Defaults to 1 if fraction is nil.

  • fraction (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 nil (default), a random seed is used.

Returns:



5262
5263
5264
5265
5266
5267
5268
5269
5270
# File 'lib/polars/series.rb', line 5262

def sample(
  n: nil,
  fraction: nil,
  with_replacement: false,
  shuffle: false,
  seed: nil
)
  super
end

#scatter(indices, values) ⇒ Series

Set values at the index locations.

Examples:

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

Parameters:

  • indices (Object)

    Integers representing the index locations.

  • values (Object)

    Replacement values.

Returns:



3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
# File 'lib/polars/series.rb', line 3191

def scatter(indices, values)
  if indices.is_a?(Integer)
    indices = [indices]
  end
  if indices.length == 0
    return self
  end

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

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

#search_sorted(element, side: "any", descending: false) ⇒ Integer

Find indices where elements should be inserted to maintain order.

Examples:

s = Polars::Series.new("set", [1, 2, 3, 4, 4, 5, 6, 7])
s.search_sorted(4)
# => 3
s.search_sorted(4, side: "left")
# => 3
s.search_sorted(4, side: "right")
# => 5
s.search_sorted([1, 4, 5])
# =>
# shape: (3,)
# Series: 'set' [u32]
# [
#         0
#         3
#         5
# ]
s.search_sorted([1, 4, 5], side: "left")
# =>
# shape: (3,)
# Series: 'set' [u32]
# [
#         0
#         3
#         5
# ]
s.search_sorted([1, 4, 5], side: "right")
# =>
# shape: (3,)
# Series: 'set' [u32]
# [
#         1
#         5
#         6
# ]

Parameters:

  • element (Object)

    Expression or scalar value.

  • side ('any', 'left', 'right') (defaults to: "any")

    If 'any', the index of the first suitable location found is given. If 'left', the index of the leftmost suitable location found is given. If 'right', return the rightmost suitable location found is given.

  • descending (Boolean) (defaults to: false)

    Boolean indicating whether the values are descending or not (they are required to be sorted either way).

Returns:

  • (Integer)


2360
2361
2362
2363
2364
2365
2366
# File 'lib/polars/series.rb', line 2360

def search_sorted(element, side: "any", descending: false)
  if element.is_a?(Integer) || element.is_a?(Float)
    return Polars.select(Polars.lit(self).search_sorted(element, side: side, descending: descending)).item
  end
  element = Series.new(element)
  Polars.select(Polars.lit(self).search_sorted(element, side: side, descending: descending)).to_series
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:



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

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

#set_sorted(descending: 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:

  • descending (Boolean) (defaults to: false)

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

Returns:



6210
6211
6212
# File 'lib/polars/series.rb', line 6210

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

#shapeArray

Shape of this Series.

Examples:

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

Returns:



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

def shape
  [_s.len]
end

#shift(n = 1, fill_value: nil) ⇒ 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:

  • n (Integer) (defaults to: 1)

    Number of places to shift (may be negative).

  • fill_value (Object) (defaults to: nil)

    Fill the resulting null values with this value. Accepts scalar expression input. Non-expression inputs are parsed as literals.

Returns:



3900
3901
3902
# File 'lib/polars/series.rb', line 3900

def shift(n = 1, fill_value: nil)
  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.

Examples:

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

Returns:



6254
6255
6256
# File 'lib/polars/series.rb', line 6254

def shrink_dtype
  Utils.wrap_s(_s.shrink_dtype)
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:



5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
# File 'lib/polars/series.rb', line 5332

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
#         3
#         1
# ]

Parameters:

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

    Seed for the random number generator.

Returns:



6017
6018
6019
# File 'lib/polars/series.rb', line 6017

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' [f64]
# [
#         -1.0
#         -0.0
#         0.0
#         1.0
#         null
# ]

Returns:



3575
3576
3577
# File 'lib/polars/series.rb', line 3575

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:



3594
3595
3596
# File 'lib/polars/series.rb', line 3594

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:



3789
3790
3791
# File 'lib/polars/series.rb', line 3789

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.

Examples:

s = Polars::Series.new([1, 2, 2, 4, 5])
s.skew
# => 0.34776706224699483

Parameters:

  • bias (Boolean) (defaults to: true)

    If false, the calculations are corrected for statistical bias.

Returns:

  • (Float, nil)


5654
5655
5656
# File 'lib/polars/series.rb', line 5654

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:



1787
1788
1789
# File 'lib/polars/series.rb', line 1787

def slice(offset, length = nil)
  self.class._from_rbseries(_s.slice(offset, length))
end

#sort(descending: false, nulls_last: false, multithreaded: true, 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(descending: true)
# =>
# shape: (4,)
# Series: 'a' [i64]
# [
#         4
#         3
#         2
#         1
# ]

Parameters:

  • descending (Boolean) (defaults to: false)

    Reverse sort.

  • nulls_last (Boolean) (defaults to: false)

    Place null values last instead of first.

  • multithreaded (Boolean) (defaults to: true)

    Sort using multiple threads.

  • in_place (Boolean) (defaults to: false)

    Sort in place.

Returns:



2103
2104
2105
2106
2107
2108
2109
2110
# File 'lib/polars/series.rb', line 2103

def sort(descending: false, nulls_last: false, multithreaded: true, in_place: false)
  if in_place
    self._s = _s.sort(descending, nulls_last, multithreaded)
    self
  else
    Utils.wrap_s(_s.sort(descending, nulls_last, multithreaded))
  end
end

#sql(query, table_name: "self") ⇒ DataFrame

Note:

This functionality is considered unstable, although it is close to being considered stable. It may be changed at any point without it being considered a breaking change.

Note:
  • The calling Series is automatically registered as a table in the SQLContext under the name "self". If you want access to the DataFrames, LazyFrames, and other Series found in the current globals, use :meth:pl.sql <polars.sql>.
  • More control over registration and execution behaviour is available by using the :class:SQLContext object.
  • The SQL query executes in lazy mode before being collected and returned as a DataFrame.
  • It is recommended to name your Series for use with SQL, otherwise the default Series name (an empty string) is used; while "" is valid, it is awkward.

Execute a SQL query against the Series.

Examples:

Query the Series using SQL:

s = Polars::Series.new(
  "dt",
  [Date.new(1999, 12, 31), Date.new(2099, 2, 14), Date.new(2026, 3, 5)]
)
s.sql("
  SELECT
    EXTRACT('year',dt) AS y,
    EXTRACT('month',dt) AS m,
    EXTRACT('day',dt) AS d,
  FROM self
  WHERE dt > '2020-01-01'
  ORDER BY dt DESC
")
# =>
# shape: (2, 3)
# ┌──────┬─────┬─────┐
# │ y    ┆ m   ┆ d   │
# │ ---  ┆ --- ┆ --- │
# │ i32  ┆ i8  ┆ i8  │
# ╞══════╪═════╪═════╡
# │ 2099 ┆ 2   ┆ 14  │
# │ 2026 ┆ 3   ┆ 5   │
# └──────┴─────┴─────┘

While you can refer to an unnamed Series column using the default empty string, it is not recommended:

s = Polars::Series.new([1, 2, 3])
s.sql('SELECT "" AS x, "" * 2 AS "2x" FROM self')
# =>
# shape: (3, 2)
# ┌─────┬─────┐
# │ x   ┆ 2x  │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 2   │
# │ 2   ┆ 4   │
# │ 3   ┆ 6   │
# └─────┴─────┘

Parameters:

  • query (String)

    SQL query to execute.

  • table_name (String) (defaults to: "self")

    Optionally provide an explicit name for the table that represents the calling frame (defaults to "self").

Returns:



2064
2065
2066
# File 'lib/polars/series.rb', line 2064

def sql(query, table_name: "self")
  to_frame.sql(query, table_name: table_name)
end

#sqrtSeries

Compute the square root of the elements.

Examples:

s = Polars::Series.new([1, 2, 3])
s.sqrt
# =>
# shape: (3,)
# Series: '' [f64]
# [
#         1.0
#         1.414214
#         1.732051
# ]

Returns:



583
584
585
# File 'lib/polars/series.rb', line 583

def sqrt
  super
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)


1087
1088
1089
1090
1091
1092
1093
# File 'lib/polars/series.rb', line 1087

def std(ddof: 1)
  if !dtype.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:



6440
6441
6442
# File 'lib/polars/series.rb', line 6440

def str
  StringNameSpace.new(self)
end

#structStructNameSpace

Create an object namespace of all struct related methods.

Returns:



6447
6448
6449
# File 'lib/polars/series.rb', line 6447

def struct
  StructNameSpace.new(self)
end

#sumNumeric

Note:

Dtypes in \{Int8, UInt8, Int16, UInt16} are cast to Int64 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)


905
906
907
# File 'lib/polars/series.rb', line 905

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:



1936
1937
1938
1939
1940
1941
# File 'lib/polars/series.rb', line 1936

def tail(n = 10)
  if n < 0
    n = [0, len + n].max
  end
  self.class._from_rbseries(_s.tail(n))
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:



3632
3633
3634
# File 'lib/polars/series.rb', line 3632

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:



3827
3828
3829
# File 'lib/polars/series.rb', line 3827

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



2932
2933
2934
# File 'lib/polars/series.rb', line 2932

def to_a
  _s.to_a
end

#to_arrowNanoarrow::Array

Return the underlying Arrow array.

Returns:

  • (Nanoarrow::Array)


3136
3137
3138
3139
3140
# File 'lib/polars/series.rb', line 3136

def to_arrow
  require "nanoarrow"

  Nanoarrow::Array.new(self)
end

#to_dummies(separator: "_", drop_first: false, drop_nulls: false) ⇒ DataFrame

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   │
# └─────┴─────┴─────┘

Parameters:

  • separator (String) (defaults to: "_")

    Separator/delimiter used when generating column names.

  • drop_first (Boolean) (defaults to: false)

    Remove the first category from the variable being encoded.

  • drop_nulls (Boolean) (defaults to: false)

    If there are nil values in the series, a null column is not generated.

Returns:



1169
1170
1171
# File 'lib/polars/series.rb', line 1169

def to_dummies(separator: "_", drop_first: false, drop_nulls: false)
  Utils.wrap_df(_s.to_dummies(separator, drop_first, drop_nulls))
end

#to_frame(name = nil) ⇒ DataFrame

Cast this Series to a DataFrame.

Examples:

s = Polars::Series.new("a", [123, 456])
s.to_frame
# =>
# shape: (2, 1)
# ┌─────┐
# │ a   │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 123 │
# │ 456 │
# └─────┘
s.to_frame("xyz")
# =>
# shape: (2, 1)
# ┌─────┐
# │ xyz │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 123 │
# │ 456 │
# └─────┘

Returns:



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

def to_frame(name = nil)
  if name
    return Utils.wrap_df(RbDataFrame.new([rename(name)._s]))
  end
  Utils.wrap_df(RbDataFrame.new([_s]))
end

#to_numoNumo::NArray

Convert this Series to a Numo array. This operation clones data but is completely safe.

Examples:

s = Polars::Series.new("a", [1, 2, 3])
s.to_numo
# =>
# Numo::Int64#shape=[3]
# [1, 2, 3]

Returns:

  • (Numo::NArray)


3123
3124
3125
3126
3127
3128
3129
3130
3131
# File 'lib/polars/series.rb', line 3123

def to_numo
  require "numo/narray"

  if dtype.temporal?
    Numo::RObject.cast(to_a)
  else
    _s.to_numo
  end
end

#to_physicalSeries

Cast to physical representation of the logical dtype.

Examples:

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

Returns:



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

def to_physical
  super
end

#to_sString Also known as: inspect

Returns a string representing the Series.

Returns:



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

def to_s
  _s.to_s
end

#top_k(k: 5) ⇒ Boolean

Return the k largest elements.

Examples:

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

Parameters:

  • k (Integer) (defaults to: 5)

    Number of elements to return.

Returns:



2130
2131
2132
# File 'lib/polars/series.rb', line 2130

def top_k(k: 5)
  super
end

#top_k_by(by, k: 5, reverse: false) ⇒ Series

Return the k largest elements of the by column.

Non-null elements are always preferred over null elements, regardless of the value of reverse. The output is not guaranteed to be in any particular order, call sort after this function if you wish the output to be sorted.

Examples:

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

Parameters:

  • by (Object)

    Column used to determine the largest elements. Accepts expression input. Strings are parsed as column names.

  • k (Integer) (defaults to: 5)

    Number of elements to return.

  • reverse (Object) (defaults to: false)

    Consider the k smallest elements of the by column (instead of the k largest). This can be specified per column by passing an array of booleans.

Returns:



2164
2165
2166
2167
2168
2169
2170
# File 'lib/polars/series.rb', line 2164

def top_k_by(
  by,
  k: 5,
  reverse: false
)
  super
end

#truncate(decimals = 0) ⇒ Series

Note:

Truncation discards the fractional part beyond the given number of decimals. For example, when rounding to 0 decimals 0.25, -0.25, 0.99, and -0.99 will all round to 0. When rounding to 1 decimal 1.9999 rounds to 1.9 and -1.9999 rounds to -1.9. There is no tiebreak behaviour at midpoint values as there is with :meth:round so 0.5 and -0.5 will also round to 0 when decimals=1.

Note:

This method performs numeric truncation. For truncating temporal data (dates/datetimes), use :func:Series.dt.truncate instead.

Truncate numeric data toward zero to decimals number of decimal places.

Examples:

s = Polars::Series.new("a", [1.12345, 2.56789, 3.991234])
s.truncate(2)
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         1.12
#         2.56
#         3.99
# ]
s = Polars::Series.new("a", [-1.78, 2.56, -3.99])
s.truncate(0)
# =>
# shape: (3,)
# Series: 'a' [f64]
# [
#         -1.0
#         2.0
#         -3.0
# ]

Parameters:

  • decimals (Integer) (defaults to: 0)

    Number of decimal places to truncate to.

Returns:



3461
3462
3463
# File 'lib/polars/series.rb', line 3461

def truncate(decimals = 0)
  super
end

#unique(maintain_order: false) ⇒ Series Also known as: uniq

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:



2386
2387
2388
# File 'lib/polars/series.rb', line 2386

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:



1501
1502
1503
# File 'lib/polars/series.rb', line 1501

def unique_counts
  super
end

#upper_boundSeries

Return the upper bound of this Series' dtype as a unit Series.

Examples:

s = Polars::Series.new("s", [-1, 0, 1], dtype: Polars::Int8)
s.upper_bound
# =>
# shape: (1,)
# Series: 's' [i8]
# [
#         127
# ]
s = Polars::Series.new("s", [1.0, 2.5, 3.0], dtype: Polars::Float64)
s.upper_bound
# =>
# shape: (1,)
# Series: 's' [f64]
# [
#         inf
# ]

Returns:



5769
5770
5771
# File 'lib/polars/series.rb', line 5769

def upper_bound
  super
end

#value_counts(sort: false, parallel: false, name: nil, normalize: 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.

  • parallel (Boolean) (defaults to: false)

    Execute the computation in parallel.

  • name (String) (defaults to: nil)

    Give the resulting count column a specific name; if normalize is true this defaults to "proportion", otherwise defaults to "count".

  • normalize (Boolean) (defaults to: false)

    If true, the count is returned as the relative frequency of unique values normalized to 1.0.

Returns:



1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
# File 'lib/polars/series.rb', line 1466

def value_counts(
  sort: false,
  parallel: false,
  name: nil,
  normalize: false
)
  if name.nil?
    if normalize
      name = "proportion"
    else
      name = "count"
    end
  end
  DataFrame._from_rbdf(
    self._s.value_counts(
      sort, parallel, name, normalize
    )
  )
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)


1107
1108
1109
1110
1111
1112
1113
# File 'lib/polars/series.rb', line 1107

def var(ddof: 1)
  if !dtype.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:



3944
3945
3946
# File 'lib/polars/series.rb', line 3944

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

#|(other) ⇒ Series

Bitwise OR.

Returns:



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

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