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.



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/polars/data_types.rb', line 368

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.



366
367
368
# File 'lib/polars/data_types.rb', line 366

def categories
  @categories
end

Instance Method Details

#==(other) ⇒ Object



397
398
399
400
401
402
403
404
405
# File 'lib/polars/data_types.rb', line 397

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

#to_sObject



407
408
409
# File 'lib/polars/data_types.rb', line 407

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