Class: Daru::IO::Exporters::CSV

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/exporters/csv.rb

Overview

CSV Exporter Class, that extends to_csv_string and write_csv methods to Daru::DataFrame instance variables

Instance Method Summary collapse

Methods inherited from Base

#optional_gem

Constructor Details

#initialize(dataframe, converters: :numeric, compression: :infer, headers: nil, convert_comma: nil, **options) ⇒ CSV

Initializes a CSV Exporter instance

Examples:

Initializing a CSV Exporter Instance

df = Daru::DataFrame.new([[1,2],[3,4]], order: [:a, :b])

#=> #<Daru::DataFrame(2x2)>
#      a   b
#  0   1   3
#  1   2   4

csv_instance = Daru::IO::Exporters::CSV.new(df, col_sep: ' ')
csv_gz_instance = Daru::IO::Exporters::CSV.new(df, col_sep: ' ', compression: :gzip)

Parameters:

  • dataframe (Daru::DataFrame)

    A dataframe to export

  • converters (Symbol) (defaults to: :numeric)

    A type to convert the data in dataframe

  • compression (Symbol) (defaults to: :infer)

    Defaults to :infer, which decides depending on file format like .csv.gz. For explicitly writing into a .csv.gz file, set :compression as :gzip.

  • headers (Boolean) (defaults to: nil)

    When set to false, the headers aren't written to the CSV file

  • convert_comma (Boolean) (defaults to: nil)

    When set to true, the decimal delimiter for float values is a comma (,) rather than a dot (.).

  • options (Hash)

    CSV standard library options, to tweak other default options of CSV gem.



36
37
38
39
40
41
42
43
44
45
# File 'lib/daru/io/exporters/csv.rb', line 36

def initialize(dataframe, converters: :numeric, compression: :infer,
  headers: nil, convert_comma: nil, **options)
  require 'csv'

  super(dataframe)
  @headers       = headers
  @compression   = compression
  @convert_comma = convert_comma
  @options       = options.merge converters: converters
end

Instance Method Details

#to_sString

Exports a CSV Exporter instance to a file-writable String.

Examples:

Getting a file-writable string from CSV Exporter instance

csv_instance.to_s
#=> "a b\n1 3\n2 4\n"

csv_gz_instance.to_s
#=> "\u001F\x8B\b\u0000*D\xA4Y\u0000\u0003KTH\xE22T0\xE62R0\xE1\u0002\u0000\xF2\\\x96y\..."

Returns:

  • (String)

    A file-writable string



57
58
59
# File 'lib/daru/io/exporters/csv.rb', line 57

def to_s
  super
end

#write(path) ⇒ Object

Exports an Avro Exporter instance to a csv / csv.gz file.

Examples:

Writing an Avro Exporter instance to an Avro file

csv_instance.write('filename.csv')
csv_gz_instance.write('filename.csv.gz')

Parameters:

  • path (String)

    Path of the csv / csv.gz file where the dataframe is to be saved



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/daru/io/exporters/csv.rb', line 68

def write(path)
  @path    = path
  contents = process_dataframe

  if compression?(:gzip, '.csv.gz')
    require 'zlib'
    ::Zlib::GzipWriter.open(@path) do |gz|
      contents.each { |content| gz.write(content.to_csv(@options)) }
      gz.close
    end
  else
    csv = ::CSV.open(@path, 'w', @options)
    contents.each { |content| csv << content }
    csv.close
  end
end