Module: DaruLite::DataFrame::IOAble::ClassMethods
- Defined in:
- lib/daru_lite/data_frame/i_o_able.rb
Instance Method Summary collapse
- #_load(data) ⇒ Object
-
#from_activerecord(relation, *fields) ⇒ Object
Read a dataframe from AR::Relation.
-
#from_csv(path, opts = {}, &block) ⇒ Object
Load data from a CSV file.
-
#from_excel(path, opts = {}, &block) ⇒ Object
Read data from an Excel file into a DataFrame.
-
#from_plaintext(path, fields) ⇒ Object
Read the database from a plaintext file.
-
#from_sql(dbh, query) ⇒ Object
Read a database query and returns a Dataset.
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
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 |