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.



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/polars/data_types.rb', line 387

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

  if categories.empty?
    @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.



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

def categories
  @categories
end

Instance Method Details

#==(other) ⇒ Object



416
417
418
419
420
421
422
423
424
# File 'lib/polars/data_types.rb', line 416

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

#to_sObject



426
427
428
# File 'lib/polars/data_types.rb', line 426

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