Class: Daru::IO::Exporters::CSV
- 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
-
#initialize(dataframe, converters: :numeric, compression: :infer, headers: nil, convert_comma: nil, **options) ⇒ CSV
constructor
Initializes a CSV Exporter instance.
-
#to_s ⇒ String
Exports a CSV Exporter instance to a file-writable String.
-
#write(path) ⇒ Object
Exports an Avro Exporter instance to a csv / csv.gz file.
Methods inherited from Base
Constructor Details
#initialize(dataframe, converters: :numeric, compression: :infer, headers: nil, convert_comma: nil, **options) ⇒ CSV
Initializes a CSV Exporter instance
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, **) require 'csv' super(dataframe) @headers = headers @compression = compression @convert_comma = convert_comma @options = .merge converters: converters end |
Instance Method Details
#to_s ⇒ String
Exports a CSV Exporter instance to 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.
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 |