Class: CAConstString

Inherits:
Object
  • Object
show all
Includes:
CArray::StringOperationMixin
Defined in:
lib/carray/const_string.rb

Overview

Read-only column of variable-length Strings: every element's bytes are packed into one shared buffer, with a (start, end) byte-range pair per element (the Arrow string layout).

Built through CArray.const_string. For a column with heavy duplication (categorical labels) prefer CACategorical, which stores each distinct value once.

Instance Method Summary collapse

Methods included from CArray::StringOperationMixin

#bytesize, #capitalize, #center, #chomp, #delete_prefix, #delete_suffix, #downcase, #encode, #end_with?, #extract, #force_encoding, #gsub, #in?, #include?, #ljust, #lstrip, #match?, #rindex, #rjust, #rstrip, #scrub, #start_with?, #str_index, #str_len, #strip, #swapcase, #to_const_string, #to_f, #to_fixlen_string, #to_i, #to_string, #upcase

Instance Method Details

#categorize(labels: nil, sort_labels: false) ⇒ CACategorical

Returns categories keyed on the strings.

Returns:



218
219
220
# File 'lib/carray/const_string.rb', line 218

def categorize (labels: nil, sort_labels: false)
  to_string.categorize(labels: labels, sort_labels: sort_labels)
end

#difference(other, sort: false) ⇒ CAConstString

Returns the distinct strings only self has.

Returns:



200
201
202
# File 'lib/carray/const_string.rb', line 200

def difference (other, sort: false)
  to_string.difference(string_operand(other), sort: sort).to_const_string
end

#intersection(other, sort: false) ⇒ CAConstString

Returns the distinct strings present in both.

Returns:



194
195
196
# File 'lib/carray/const_string.rb', line 194

def intersection (other, sort: false)
  to_string.intersection(string_operand(other), sort: sort).to_const_string
end

#is_in(values) ⇒ CArray

Returns boolean, true where the cell's string is in values.

Returns:

  • (CArray)

    boolean, true where the cell's string is in values.



188
189
190
# File 'lib/carray/const_string.rb', line 188

def is_in (values)
  to_string.is_in(string_operand(values))
end

#is_mode(axis: nil) ⇒ CArray

Returns boolean, true where the cell holds a modal string.

Returns:

  • (CArray)

    boolean, true where the cell holds a modal string.



176
177
178
# File 'lib/carray/const_string.rb', line 176

def is_mode (axis: nil)
  to_string.is_mode(axis: axis)
end

#locate_addr(ref) ⇒ CArray

Returns for each cell, its address in ref (UNDEF when absent).

Returns:

  • (CArray)

    for each cell, its address in ref (UNDEF when absent).



212
213
214
# File 'lib/carray/const_string.rb', line 212

def locate_addr (ref)
  to_string.locate_addr(string_operand(ref))
end

#mask_duplicates(axis: nil) ⇒ CAConstString

Returns a copy with every repeat occurrence masked.

Returns:

  • (CAConstString)

    a copy with every repeat occurrence masked.



182
183
184
# File 'lib/carray/const_string.rb', line 182

def mask_duplicates (axis: nil)
  to_string.mask_duplicates(axis: axis).to_const_string
end

#mode(axis: nil) ⇒ CAConstString, Array

Returns the most frequent string(s).

Returns:



165
166
167
168
169
170
171
172
# File 'lib/carray/const_string.rb', line 165

def mode (axis: nil)
  r = to_string.mode(axis: axis)
  case r
  when CArray then r.to_const_string
  when Array  then r.map { |c| c.is_a?(CArray) ? c.to_const_string : c }
  else r
  end
end

#nunique(axis: nil, keep_axis: false) ⇒ Integer, CArray

Returns the number of distinct strings.

Returns:

  • (Integer, CArray)

    the number of distinct strings.



159
160
161
# File 'lib/carray/const_string.rb', line 159

def nunique (axis: nil, keep_axis: false)
  to_string.nunique(axis: axis, keep_axis: keep_axis)
end

#sortCAConstString

Returns a byte-order sorted view built by gathering the offsets with sort_index. Neither bytes nor offsets are copied.

Returns:



97
98
99
# File 'lib/carray/const_string.rb', line 97

def sort
  self[sort_index]
end

#sort_copyCAConstString

Returns an owned sorted CAConstString; the materialised counterpart to #sort.

Returns:



105
106
107
# File 'lib/carray/const_string.rb', line 105

def sort_copy
  self[sort_index].copy
end

#template(type = nil, **opts) ⇒ CArray

CAConstString is the one Face with three separate entities -- storage (int64 offset), surface (fixlen), and the length-prefixed buffer -- so it has no byte-compatible blank form (other Faces have surface == storage or a struct tail, which the core template lift handles). A same-class template would be an unusable shell, so override it: with an explicit type defer to the generic (plain) path; with no type return a plain same-shape object array (a fillable string container), honouring a block.

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/carray/const_string.rb', line 118

def template (*args, &block)
  return super unless args.empty?    # a type was given: generic plain path
  out = CArray.object(*shape)
  if block
    if block.arity == 0
      out[] = block.call
    else
      out.map_with_index! { |_, *idx| block.call(*idx) }
    end
  end
  out
end

#union(other, sort: false) ⇒ CAConstString

Returns the distinct strings of either side.

Returns:



206
207
208
# File 'lib/carray/const_string.rb', line 206

def union (other, sort: false)
  to_string.union(string_operand(other), sort: sort).to_const_string
end

#unique(sort: false) ⇒ CAConstString

Returns the distinct strings.

Returns:



146
147
148
# File 'lib/carray/const_string.rb', line 146

def unique (sort: false)
  to_string.unique(sort: sort).to_const_string
end

#value_counts(sort: false) ⇒ Array(CAConstString, CArray)

Returns [values, counts].

Returns:



152
153
154
155
# File 'lib/carray/const_string.rb', line 152

def value_counts (sort: false)
  values, counts = to_string.value_counts(sort: sort)
  [values.to_const_string, counts]
end