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



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

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



88
89
90
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 88

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

#from_csv(path, opts = {}, &block) ⇒ 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 = {}, &block)
  DaruLite::IO.from_csv path, opts, &block
end

#from_excel(path, opts = {}, &block) ⇒ 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 = {}, &block)
  DaruLite::IO.from_excel path, opts, &block
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]


104
105
106
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 104

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

#from_sql(dbh, query) ⇒ Object

Read a database query and returns a Dataset

USE:

dbh = DBI.connect("DBI:Mysql:database:localhost", "user", "password")
DaruLite::DataFrame.from_sql(dbh, "SELECT * FROM test")

#Alternatively

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

Parameters:

  • dbh (DBI::DatabaseHandle, String)

    A DBI connection OR Path to a SQlite3 database.

  • query (String)

    The query to be executed

Returns:

  • A dataframe containing the data resulting from the query



68
69
70
# File 'lib/daru_lite/data_frame/i_o_able.rb', line 68

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