Class: Polars::BinaryNameSpace

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

Overview

Series.bin namespace.

Instance Method Summary collapse

Dynamic Method Handling

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

Instance Method Details

#contains(literal) ⇒ Series

Check if binaries in Series contain a binary substring.

Examples:

s = Polars::Series.new("colors", ["\x00\x00\x00".b, "\xff\xff\x00".b, "\x00\x00\xff".b])
s.bin.contains("\xff".b)
# =>
# shape: (3,)
# Series: 'colors' [bool]
# [
#         false
#         true
#         true
# ]

Parameters:

  • literal (String)

    The binary substring to look for

Returns:



31
32
33
# File 'lib/polars/binary_name_space.rb', line 31

def contains(literal)
  super
end

#decode(encoding, strict: true) ⇒ Series

Decode a value using the provided encoding.

Examples:

Decode values using hexadecimal encoding.

s = Polars::Series.new("colors", ["000000".b, "ffff00".b, "0000ff".b])
s.bin.decode("hex")
# =>
# shape: (3,)
# Series: 'colors' [binary]
# [
#         b"\x00\x00\x00"
#         b"\xff\xff\x00"
#         b"\x00\x00\xff"
# ]

Decode values using Base64 encoding.

s = Polars::Series.new("colors", ["AAAA".b, "//8A".b, "AAD/".b])
s.bin.decode("base64")
# =>
# shape: (3,)
# Series: 'colors' [binary]
# [
#         b"\x00\x00\x00"
#         b"\xff\xff\x00"
#         b"\x00\x00\xff"
# ]

Set strict: false to set invalid values to null instead of raising an error.

s = Polars::Series.new("colors", ["000000".b, "ffff00".b, "invalid_value".b])
s.bin.decode("hex", strict: false)
# =>
# shape: (3,)
# Series: 'colors' [binary]
# [
#         b"\x00\x00\x00"
#         b"\xff\xff\x00"
#         null
# ]

Parameters:

  • encoding ("hex", "base64")

    The encoding to use.

  • strict (Boolean) (defaults to: true)

    Raise an error if the underlying value cannot be decoded, otherwise mask out with a null value.

Returns:



124
125
126
# File 'lib/polars/binary_name_space.rb', line 124

def decode(encoding, strict: true)
  super
end

#encode(encoding) ⇒ Series

Encode a value using the provided encoding.

Examples:

Encode values using hexadecimal encoding.

s = Polars::Series.new("colors", ["\x00\x00\x00".b, "\xff\xff\x00".b, "\x00\x00\xff".b])
s.bin.encode("hex")
# =>
# shape: (3,)
# Series: 'colors' [str]
# [
#         "000000"
#         "ffff00"
#         "0000ff"
# ]

Encode values using Base64 encoding.

s.bin.encode("base64")
# =>
# shape: (3,)
# Series: 'colors' [str]
# [
#         "AAAA"
#         "//8A"
#         "AAD/"
# ]

Parameters:

  • encoding ("hex", "base64")

    The encoding to use.

Returns:



157
158
159
# File 'lib/polars/binary_name_space.rb', line 157

def encode(encoding)
  super
end

#ends_with(suffix) ⇒ Series

Check if string values end with a binary substring.

Examples:

s = Polars::Series.new("colors", ["\x00\x00\x00".b, "\xff\xff\x00".b, "\x00\x00\xff".b])
s.bin.ends_with("\x00".b)
# =>
# shape: (3,)
# Series: 'colors' [bool]
# [
#         true
#         true
#         false
# ]

Parameters:

  • suffix (String)

    Suffix substring.

Returns:



53
54
55
# File 'lib/polars/binary_name_space.rb', line 53

def ends_with(suffix)
  super
end

#get(index, null_on_oob: false) ⇒ Series

Get the byte value at the given index.

For example, index 0 would return the first byte of every binary value and index -1 would return the last byte of every binary value. The behavior if an index is out of bounds is determined by the argument null_on_oob.

Examples:

s = Polars::Series.new("a", ["\x01\x02\x03".b, "".b, "\x04\x05".b])
s.bin.get(0, null_on_oob: true)
# =>
# shape: (3,)
# Series: 'a' [u8]
# [
#         1
#         null
#         4
# ]

Parameters:

  • index (Object)

    Index to return per binary value

  • null_on_oob (Boolean) (defaults to: false)

    Behavior if an index is out of bounds:

    • true -> set as null
    • false -> raise an error

Returns:



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

def get(index, null_on_oob: false)
  super
end

#head(n = 5) ⇒ Series

Note:

(1) A similar method exists for taking the last n bytes: tail. (2) If n is negative, it is interpreted as "until the nth byte from the end", e.g., head(-3) returns all but the last three bytes.

Take the first n bytes of the binary values.

Examples:

colors = Polars::Series.new(["\x00\x00\x00".b, "\xff\xff\x00".b, "\x00\x00\xff".b])
colors.bin.head(2)
# =>
# shape: (3,)
# Series: '' [binary]
# [
#         b"\x00\x00"
#         b"\xff\xff"
#         b"\x00\x00"
# ]

Parameters:

  • n (Object) (defaults to: 5)

    Length of the slice. Negative indexing is supported; see note (2) below.

Returns:



284
285
286
# File 'lib/polars/binary_name_space.rb', line 284

def head(n = 5)
  super
end

#reinterpret(dtype:, endianness: "little") ⇒ Series

Interpret a buffer as a numerical polars type.

Examples:

s = Polars::Series.new("data", ["\x05\x00\x00\x00".b, "\x10\x00\x01\x00".b])
s.bin.reinterpret(dtype: Polars::Int32, endianness: "little")
# =>
# shape: (2,)
# Series: 'data' [i32]
# [
#         5
#         65552
# ]

Parameters:

  • dtype (Object)

    Which type to interpret binary column into.

  • endianness ("big", "little") (defaults to: "little")

    Which endianness to use when interpreting bytes, by default "little".

Returns:



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

def reinterpret(dtype:, endianness: "little")
  super
end

#size(unit = "b") ⇒ Series

Get the size of the binary values in a Series in the given unit.

Examples:

s = Polars::Series.new("data", [512, 256, 2560, 1024].map { |n| "\x00".b * n })
s.bin.size("kb")
# =>
# shape: (4,)
# Series: 'data' [f64]
# [
#         0.5
#         0.25
#         2.5
#         1.0
# ]

Returns:



177
178
179
# File 'lib/polars/binary_name_space.rb', line 177

def size(unit = "b")
  super
end

#slice(offset, length = nil) ⇒ Series

Slice the binary values.

Examples:

colors = Polars::Series.new(["\x00\x00\x00".b, "\xff\xff\x00".b, "\x00\x00\xff".b])
colors.bin.slice(1, 2)
# =>
# shape: (3,)
# Series: '' [binary]
# [
#         b"\x00\x00"
#         b"\xff\x00"
#         b"\x00\xff"
# ]

Parameters:

  • offset (Object)

    Start index. Negative indexing is supported.

  • length (Object) (defaults to: nil)

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

Returns:



225
226
227
# File 'lib/polars/binary_name_space.rb', line 225

def slice(offset, length = nil)
  super
end

#starts_with(prefix) ⇒ Series

Check if values start with a binary substring.

Examples:

s = Polars::Series.new("colors", ["\x00\x00\x00".b, "\xff\xff\x00".b, "\x00\x00\xff".b])
s.bin.starts_with("\x00".b)
# =>
# shape: (3,)
# Series: 'colors' [bool]
# [
#         true
#         false
#         true
# ]

Parameters:

  • prefix (String)

    Prefix substring.

Returns:



75
76
77
# File 'lib/polars/binary_name_space.rb', line 75

def starts_with(prefix)
  super
end

#tail(n = 5) ⇒ Series

Note:

(1) A similar method exists for taking the first n bytes: head. (2) If n is negative, it is interpreted as "starting at the nth byte", e.g., tail(-3) returns all but the first three bytes.

Take the last n bytes of the binary values.

Examples:

colors = Polars::Series.new(["\x00\x00\x00".b, "\xff\xff\x00".b, "\x00\x00\xff".b])
colors.bin.tail(2)
# =>
# shape: (3,)
# Series: '' [binary]
# [
#         b"\x00\x00"
#         b"\xff\x00"
#         b"\x00\xff"
# ]

Parameters:

  • n (Object) (defaults to: 5)

    Length of the slice. Negative indexing is supported; see note (2) below.

Returns:



311
312
313
# File 'lib/polars/binary_name_space.rb', line 311

def tail(n = 5)
  super
end