Class: Polars::Enum

Inherits:
DataType show all
Defined in:
lib/polars/data_types.rb

Overview

A fixed set categorical encoding of a set of strings.

NOTE: this is an experimental work-in-progress feature and may not work as expected.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(categories) ⇒ Enum

Returns a new instance of Enum.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/polars/data_types.rb', line 308

def initialize(categories)
  if !categories.is_a?(Series)
    categories = Series.new(categories)
  end

  if categories.empty?
    self.categories = Series.new("category", [], dtype: String)
    return
  end

  if categories.null_count > 0
    msg = "Enum categories must not contain null values"
    raise TypeError, msg
  end

  if (dtype = categories.dtype) != String
    msg = "Enum categories must be strings; found data of type #{dtype}"
    raise TypeError, msg
  end

  if categories.n_unique != categories.len
    duplicate = categories.filter(categories.is_duplicated)[0]
    msg = "Enum categories must be unique; found duplicate #{duplicate}"
    raise ArgumentError, msg
  end

  @categories = categories.rechunk.alias("category")
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



306
307
308
# File 'lib/polars/data_types.rb', line 306

def categories
  @categories
end

Instance Method Details

#==(other) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/polars/data_types.rb', line 337

def ==(other)
  if other.eql?(Enum)
    true
  elsif other.is_a?(Enum)
    categories == other.categories
  else
    false
  end
end

#to_sObject



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

def to_s
  "#{self.class.name}(categories: #{categories.to_a.inspect})"
end