Sha256: bc8f8fa49d46bb52b9de78eea5a772d61eb8976c181af0a6a6980c9442176e92

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module DF #:nodoc:
  module Saving #:nodoc:

    # Saves a data frame as CSV.  
    # Examples:
    # df.save('/tmp/some_filename.csv')
    # df.save('/tmp/some_filename.csv', :include_header => false) # No header information is saved
    # df.save('/tmp/some_filename.csv', :only => [:list, :of, :columns])
    # df.save('/tmp/some_filename.csv', :subset => [:list, :of, :columns])
    # df.save('/tmp/some_filename.csv', 
    #   :filter => {:column_name => :category_value, 
    #     :another_column_name => (range..values)}) # Filter by category
    def save(filename, opts={})

      df = self
      df = df.subset_from_columns(*Array(opts[:only])) if opts[:only]
      df = df.subset_from_columns(*Array(opts[:subset])) if opts[:subset]
      df = df.filter_by_category(opts[:filter]) if opts[:filter]
      df = df.filter_by_category(opts[:filter_by_category]) if opts[:filter_by_category]

      File.open(filename, "w") { |f| f.write df.to_csv(opts.fetch(:include_header, true)) }
    end

  end
end

class DataFrame
  include DF::Saving
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
davidrichards-data_frame-0.0.19 lib/data_frame/core/saving.rb
davidrichards-data_frame-0.0.20 lib/data_frame/core/saving.rb
data_frame-0.1.8 lib/data_frame/core/saving.rb