Class: Rust::CSV

Inherits:
Object
  • Object
show all
Defined in:
lib/rust-core.rb

Class Method Summary collapse

Class Method Details

.read(filename, **options) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/rust-core.rb', line 348

def self.read(filename, **options)
    hash = {}
    labels = nil
    ::CSV.parse(File.read(filename), **options) do |row|
        labels = row.headers || (1..row.size).to_a.map { |e| "X#{e}" } unless labels
        
        labels.each do |label|
            hash[label] = [] unless hash[label]
            hash[label] << row[label]
        end
    end
    
    return Rust::DataFrame.new(hash)
end

.write(filename, dataframe, **options) ⇒ Object

Raises:

  • (TypeError)


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/rust-core.rb', line 363

def self.write(filename, dataframe, **options)
    raise TypeError, "Expected Rust::DataFrame" unless dataframe.is_a?(Rust::DataFrame)
    
    x[:headers] = dataframe.column_names if x[:headers]
    
    hash = {}
    labels = nil
    ::CSV.open(filename, 'w', write_headers: (x[:headers] ? true : false), **options) do |csv|
        dataframe.each do |row|
            csv << row
        end
    end
    
    return true
end