Module: DaruLite::DataFrame::IOAble::ClassMethods

Defined in:
lib/daru_lite/data_frame/i_o_able.rb

Instance Method Summary collapse

Instance Method Details

#_load(data) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 110

def _load(data)
  h = Marshal.load data
  DaruLite::DataFrame.new(
    h[:data],
    index: h[:index],
    order: h[:order],
    name: h[:name]
  )
end

#from_activerecord(relation, *fields) ⇒ Object

Read a dataframe from AR::Relation

USE:

# When Post model is defined as:
class Post < ActiveRecord::Base
scope :active, -> { where.not(published_at: nil) }
end

# You can load active posts into a dataframe by:
DaruLite::DataFrame.from_activerecord(Post.active, :title, :published_at)

Parameters:

  • relation (ActiveRecord::Relation)

    An AR::Relation object from which data is loaded

  • fields (Array)

    Field names to be loaded (optional)

Returns:

  • A dataframe containing the data loaded from the relation



90
91
92
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 90

def from_activerecord(relation, *fields)
  DaruLite::IO.from_activerecord relation, *fields
end

#from_csv(path, opts = {}) ⇒ Object

Load data from a CSV file. Specify an optional block to grab the CSV object and pre-condition it (for example use the convert or header_convert methods).

Arguments

  • path - Local path / Remote URL of the file to load specified as a String.

Options

Accepts the same options as the DaruLite::DataFrame constructor and CSV.open() and uses those to eventually construct the resulting DataFrame.

Verbose Description

You can specify all the options to the .from_csv function that you do to the Ruby CSV.read() function, since this is what is used internally.

For example, if the columns in your CSV file are separated by something other that commas, you can use the :col_sep option. If you want to convert numeric values to numbers and not keep them as strings, you can use the :converters option and set it to :numeric.

The .from_csv function uses the following defaults for reading CSV files (that are passed into the CSV.read() function):

{
:col_sep           => ',',
:converters        => :numeric
}


35
36
37
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 35

def from_csv(path, opts = {}, &)
  DaruLite::IO.from_csv(path, opts, &)
end

#from_excel(path, opts = {}) ⇒ Object

Read data from an Excel file into a DataFrame.

Arguments

  • path - Path of the file to be read.

Options

*:worksheet_id - ID of the worksheet that is to be read.



48
49
50
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 48

def from_excel(path, opts = {}, &)
  DaruLite::IO.from_excel(path, opts, &)
end

#from_plaintext(path, fields) ⇒ Object

Read the database from a plaintext file. For this method to work, the data should be present in a plain text file in columns. See spec/fixtures/bank2.dat for an example.

Arguments

  • path - Path of the file to be read.
  • fields - Vector names of the resulting database.

Usage

df = DaruLite::DataFrame.from_plaintext 'spec/fixtures/bank2.dat', [:v1,:v2,:v3,:v4,:v5,:v6]


106
107
108
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 106

def from_plaintext(path, fields)
  DaruLite::IO.from_plaintext path, fields
end

#from_sql(arh, query) ⇒ Object

Read a database query and returns a Dataset

OR Path to a SQlite3 database. USE:

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: "path/to/sqlite.db") arh = ActiveRecord::Base.connection DaruLite::DataFrame.from_sql(dbh, "SELECT * FROM test")

#Alternatively

require 'active_record' DaruLite::DataFrame.from_sql("path/to/sqlite.db", "SELECT * FROM test")

Parameters:

  • arh (ActiveRecord::ConnectionAdapters::AbstractAdapter, String)

    An ActiveRecord connection

  • query (String)

    The query to be executed

Returns:

  • A dataframe containing the data resulting from the query



70
71
72
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 70

def from_sql(arh, query)
  DaruLite::IO.from_sql arh, query
end