Name: daru-io
Owner: SciRuby
Description: daru-io is a plugin gem to the existing daru gem, which aims to add support to Importing DataFrames from / Exporting DataFrames to multiple formats.
Created: 2017-05-30 06:39:04.0
Updated: 2018-03-07 09:51:03.0
Pushed: 2018-05-20 17:32:17.0
Homepage: http://www.rubydoc.info/github/athityakumar/daru-io/master/
Size: 2347
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A Ruby plugin-gem to daru gem, that extends support for many Import and Export methods of Daru::DataFrame. This gem is intended to help Rubyists who are into Data Analysis or Web Development, by serving as a general purpose conversion library that takes input in one format (say, JSON) and converts it another format (say, Avro) while also making it incredibly easy to getting started on analyzing data with daru.
While supporting various IO modules, daru-io also provides an easier way of adding more Importers / Exporters. It's strongly recommended to have a look at 'Creating your own IO modules' section, if you're interested in creating new Importers / Exporters.
If you're working with a Gemfile,
Add this line to your application's Gemfile:
'daru-io'
And then execute on your terminal:
ndle
If you're NOT working with a Gemfile, simply install it yourself by executing on your terminal:
m install daru-io
Require daru-io
gem in your application:
ire 'daru/io' #! Requires all Importers & Exporters
ire 'daru/io/importers' #! Requires all Importers and no Exporters
ire 'daru/io/importers/json' #! Requires only JSON Importer
Note: Each IO module has it's own set of dependencies. Have a look at the Importers and Exporters section for dependency-specific information.
The Daru::IO Importers are intended to return a Daru::DataFrame from the given arguments. Generally, all Importers can be called in two ways - from Daru::IO or Daru::DataFrame.
artially requires Format Importer
ire 'daru/io/importers/format'
sage from Daru::IO
ance = Daru::IO::Importers::Format.from(connection)
,
ance = Daru::IO::Importers::Format.read(path)
instance.call(opts)
sage from Daru::DataFrame
= Daru::DataFrame.from_format(connection, opts)
= Daru::DataFrame.read_format(path, opts)
Note: Please have a look at the respective Importer Doc links below, for having a look at arguments and examples.
Imports a Daru::DataFrame from an ActiveRecord connection.
activerecord
gemartially require just ActiveRecord Importer
ire 'daru/io/importers/active_record'
sage from Daru::IO
Daru::IO::Importers::ActiveRecord.from(activerecord_relation).call(:field_1, :field_2)
sage from Daru::DataFrame
Daru::DataFrame.from_activerecord(activerecord_relation, :field_1, :field_2)
Imports a Daru::DataFrame from an .avro file.
avro
and snappy
gemsartially require just Avro Importer
ire 'daru/io/importers/avro'
sage from Daru::IO
Daru::IO::Importers::Avro.read('path/to/file.avro').call
sage from Daru::DataFrame
Daru::DataFrame.read_avro('path/to/file.avro')
Imports a Daru::DataFrame from a .csv or .csv.gz file.
artially require just CSV Importer
ire 'daru/io/importers/csv'
sage from Daru::IO
= Daru::IO::Importers::CSV.read('path/to/file.csv').call(skiprows: 10, col_sep: ' ')
= Daru::IO::Importers::CSV.read('path/to/file.csv.gz').call(skiprows: 10, compression: :gzip)
sage from Daru::DataFrame
= Daru::DataFrame.read_csv('path/to/file.csv', skiprows: 10, col_sep: ' ')
= Daru::DataFrame.read_csv('path/to/file.csv.gz', skiprows: 10, compression: :gzip)
Imports a Daru::DataFrame from a .xls file.
spreadsheet
gemartially require just Excel Importer
ire 'daru/io/importers/excel'
sage from Daru::IO
Daru::IO::Importers::Excel.read('path/to/file.xls').call(worksheet_id: 1)
sage from Daru::DataFrame
Daru::DataFrame.read_excel('path/to/file.xls', worksheet_id: 1)
Imports a Daru::DataFrame from a .xlsx file.
roo
gemartially require just Excel Importer
ire 'daru/io/importers/excelx'
sage from Daru::IO
Daru::IO::Importers::Excelx.read('path/to/file.xlsx').call(sheet: 2, skiprows: 10, skipcols: 2)
sage from Daru::DataFrame
ire 'daru/io/importers/excel'
Daru::DataFrame.read_excel('path/to/file.xlsx', sheet: 2, skiprows: 10, skipcols: 2)
Note: This module works only for static tables on a HTML page, and won't work in cases where the table is being loaded into the HTML table by inline Javascript. This is how the Nokogiri gem works, and the HTML Importer also follows suit.
Imports an Array of Daru::DataFrames from a .html file or website.
nokogiri
gemartially require just HTML Importer
ire 'daru/io/importers/html'
sage from Daru::IO
= Daru::IO::Importers::HTML.read('https://some/url/with/tables').call(match: 'market', name: 'Shares analysis')
= Daru::IO::Importers::HTML.read('path/to/file.html').call(match: 'market', name: 'Shares analysis')
sage from Daru::DataFrame
= Daru::DataFrame.read_html('https://some/url/with/tables', match: 'market', name: 'Shares analysis')
= Daru::DataFrame.read_html('path/to/file.html', match: 'market', name: 'Shares analysis')
Imports a Daru::DataFrame from a .json file / response.
jsonpath
gemartially require just JSON Importer
ire 'daru/io/importers/json'
sage from Daru::IO
= Daru::IO::Importers::JSON.read('https://path/to/json/response').call(index: '$..time', col1: '$..name', col2: '$..age')
= Daru::IO::Importers::JSON.read('path/to/file.json').call(index: '$..time', col1: '$..name', col2: '$..age')
sage from Daru::DataFrame
= Daru::DataFrame.read_json('https://path/to/json/response', index: '$..time', col1: '$..name', col2: '$..age')
= Daru::DataFrame.read_json('path/to/file.json', index: '$..time', col1: '$..name', col2: '$..age')
Note: The Mongo gem faces Argument Error : expected Proc Argument issue due to the bug in MRI Ruby 2.4.0 mentioned here. This seems to have been fixed in Ruby 2.4.1 onwards. Hence, please avoid using this Mongo Importer in Ruby version 2.4.0.
Imports a Daru::DataFrame from a Mongo collection.
jsonpath
and mongo
gemsartially require just Mongo Importer
ire 'daru/io/importers/mongo'
sage from Daru::IO
Daru::IO::Importers::Mongo.from('mongodb://127.0.0.1:27017/test').call('cars')
sage from Daru::DataFrame
Daru::DataFrame.from_mongo('mongodb://127.0.0.1:27017/test', 'cars')
Imports a Daru::DataFrame from a .dat plaintext file (space separated table of simple strings and numbers). For a sample format of the plaintext file, have a look at the example bank2.dat file.
artially require just Plaintext Importer
ire 'daru/io/importers/plaintext'
sage from Daru::IO
Daru::IO::Importers::Plaintext.read('path/to/file.dat').call([:col1, :col2, :col3])
sage from Daru::DataFrame
Daru::DataFrame.read_plaintext('path/to/file.dat', [:col1, :col2, :col3])
Imports a Daru::DataFrame from a variable in .rdata file.
rsruby
gemR_HOME
variable as given in the Contribution Guidelinesartially require just RData Importer
ire 'daru/io/importers/r_data'
sage from Daru::IO
Daru::IO::Importers::RData.read('path/to/file.RData').call('ACS3')
sage from Daru::DataFrame
Daru::DataFrame.read_rdata('path/to/file.RData', 'ACS3')
Imports a Daru::DataFrame from a .rds file.
rsruby
gemR_HOME
variable as given in the Contribution Guidelinesartially require just RDS Importer
ire 'daru/io/importers/rds'
sage from Daru::IO
Daru::IO::Importers::RDS.read('path/to/file.rds').call
sage from Daru::DataFrame
Daru::DataFrame.read_rds('path/to/file.rds')
Imports a Daru::DataFrame from Redis key(s).
redis
gemredis-server
artially require just Redis Importer
ire 'daru/io/importers/redis'
sage from Daru::IO
Daru::IO::Importers::Redis.from({url: 'redis://:password@host:port/db'}).call(match: 'time:1*', count: 1000)
sage from Daru::DataFrame
Daru::DataFrame.from_redis({url: 'redis://:password@host:port/db'}, match: 'time:1*', count: 1000)
Imports a Daru::DataFrame from a sqlite.db file / DBI connection.
dbd-sqlite3
, activerecord
, dbi
and sqlite3
gemsartially require just SQL Importer
ire 'daru/io/importers/sql'
sage from Daru::IO
= Daru::IO::Importers::SQL.read('path/to/file.sqlite').call('SELECT * FROM test')
= Daru::IO::Importers::SQL.from(dbi_connection).call('SELECT * FROM test')
sage from Daru::DataFrame
= Daru::DataFrame.read_sql('path/to/file.sqlite', 'SELECT * FROM test')
= Daru::DataFrame.from_sql(dbi_connection, 'SELECT * FROM test')
The Daru::IO Exporters are intended to 'migrate' a Daru::DataFrame into a file, or database. All Exporters can be called in two ways - from Daru::IO or Daru::DataFrame.
artially requires Format Exporter
ire 'daru/io/exporters/format'
sage from Daru::IO
ance = Daru::IO::Exporters::Format.new(df, opts)
ance.to_s #=> Provides a file-writable string, which can be used in web applications for file download purposes
ance.to #=> Provides a Format instance
ance.write(path) #=> Writes to the given path
sage from Daru::DataFrame
ng = df.to_format_string(opts) #=> Provides a file-writable string, which can be to write into a file later
ance = df.to_format(opts) #=> Provides a Format instance
rite_format(path, opts) #=> Writes to the given path
Note: Please have a look at the respective Exporter Doc links below, for having a look at arguments and examples.
Exports a Daru::DataFrame into a .avro file.
avro
gemartially require just Avro Exporter
ire 'daru/io/exporters/avro'
_schema = {
ype' => 'record',
ame' => 'Example',
ields' => [
{'name' => 'col_1', 'type' => 'string'},
{'name' => 'col_2', 'type' => 'int'},
{'name' => 'col_3', 'type'=> 'boolean'}
sage from Daru::IO
ng = Daru::IO::Exporters::Avro.new(df, avro_schema).to_s
::IO::Exporters::Avro.new(df, avro_schema).write('path/to/file.avro')
sage from Daru::DataFrame
ng = df.to_avro_string(avro_schema)
rite_avro('path/to/file.avro', avro_schema)
Exports a Daru::DataFrame into a .csv or .csv.gz file.
artially require just CSV Exporter
ire 'daru/io/exporters/csv'
sage from Daru::IO
string = Daru::IO::Exporters::CSV.new(df, converters: :numeric, convert_comma: true).to_s
::IO::Exporters::CSV.new(df, converters: :numeric, convert_comma: true).write('path/to/file.csv')
gz_string = Daru::IO::Exporters::CSV.new(df, converters: :numeric, compression: :gzip, convert_comma: true).to_s
::IO::Exporters::CSV.new(df, converters: :numeric, compression: :gzip, convert_comma: true).write('path/to/file.csv.gz')
sage from Daru::DataFrame
string = df.to_csv_string(converters: :numeric, convert_comma: true)
rite_csv('path/to/file.csv', converters: :numeric, convert_comma: true)
gz_string = df.to_csv_string(converters: :numeric, compression: :gzip, convert_comma: true)
rite_csv('path/to/file.csv.gz', converters: :numeric, compression: :gzip, convert_comma: true)
Exports a Daru::DataFrame into a .xls file.
spreadsheet
gemartially require just Excel Exporter
ire 'daru/io/exporters/excel'
sage from Daru::IO
ng = Daru::IO::Exporters::Excel.new(df, header: {color: :red, weight: :bold}, data: {color: :blue }, index: false).to_s
::IO::Exporters::Excel.new(df, header: {color: :red, weight: :bold}, data: {color: :blue }, index: false).write('path/to/file.xls')
sage from Daru::DataFrame
ng = df.to_excel_string(header: {color: :red, weight: :bold}, data: {color: :blue }, index: false)
rite_excel('path/to/file.xls', header: {color: :red, weight: :bold}, data: {color: :blue }, index: false)
Exports a Daru::DataFrame into a .json file.
jsonpath
gemartially require just JSON Exporter
ire 'daru/io/exporters/json'
sage from Daru::IO
es = Daru::IO::Exporters::JSON.new(df, orient: :records, pretty: true, name: '$.person.name', age: '$.person.age').to
ng = Daru::IO::Exporters::JSON.new(df, 'path/to/file.json', orient: :records, pretty: true, name: '$.person.name', age: '$.person.age').to_s
::IO::Exporters::JSON.new(df, orient: :records, pretty: true, name: '$.person.name', age: '$.person.age').write('path/to/file.json')
sage from Daru::DataFrame
es = df.to_json('orient: :records, pretty: true, name: '$.person.name', age: '$.person.age')
ng = df.to_json_string(orient: :records, pretty: true, name: '$.person.name', age: '$.person.age')
rite_json('path/to/file.json', orient: :records, pretty: true, name: '$.person.name', age: '$.person.age')
Exports multiple Daru::DataFrames into a .rdata file.
rsruby
gemR_HOME
variable as given in the Contribution Guidelinesartially require just RData Exporter
ire 'daru/io/exporters/r_data'
sage from Daru::IO
ng = Daru::IO::Exporters::RData.new('first.df': df1, 'second.df': df2).to_s
::IO::Exporters::RData.new('first.df': df1, 'second.df': df2).write('path/to/file.RData')
Exports a Daru::DataFrame into a .rds file.
rsruby
gemR_HOME
variable as given in the Contribution Guidelinesartially require just RDS Exporter
ire 'daru/io/exporters/rds'
sage from Daru::IO
ng = Daru::IO::Exporters::RDS.new(df, 'sample.dataframe').to_s
::IO::Exporters::RDS.new(df, 'sample.dataframe').write('path/to/file.rds')
sage from Daru::DataFrame
ng = df.to_rds_string('sample.dataframe')
rite_rds('path/to/file.rds', 'sample.dataframe')
Exports a Daru::DataFrame into a database (SQL) table through DBI connection.
dbd-sqlite3
, dbi
and sqlite3
gemsartially require just SQL Exporter
ire 'daru/io/exporters/sql'
sage from Daru::IO
::IO::Exporters::SQL.new(df, DBI.connect('DBI:Mysql:database:localhost', 'user', 'password'), 'cars_table').to
sage from Daru::DataFrame
o_sql(DBI.connect('DBI:Mysql:database:localhost', 'user', 'password'), 'cars_table')
Daru-IO currently supports various Import / Export methods, as it can be seen from the above list. But the list is NEVER complete - there may always be specific use-case format(s) that you need very badly, but might not fit the needs of majority of the community. In such a case, don't worry - you can always tweak (aka monkey-patch) daru-io in your application. The architecture of daru-io
provides a neater way of monkey-patching into Daru::DataFrame to support your unique use-case.
Adding new IO modules to Daru-IO
Say, your unique use-case is of YAML IO Modules. Here's how you can proceed with tweaking -
AML Importer
ire 'daru/io'
s Daru::IO::Importers::YAML < Daru::IO::Importers::Base
ru::DataFrame.register_io_module :from_yaml, self
ru::DataFrame.register_io_module :read_yaml, self
f initialize
optional_gem 'yaml'
#! Add all required gem(s) here.
d
f from(instance)
#! Your code to create initialize instance
self
d
f read(path)
#! Your code to read the YAML file
#! and create Daru::DataFrame
self
d
f call(opts)
#! Unified code to create Daru::DataFrame
#! irrespective of which method
#! (from / read) is used by user
d
Daru::DataFrame.read_yaml('path/to/file.yaml', skip: 10)
,
Daru::IO::Importers::YAML.read('path/to/file.yaml').call(skip: 10)
uby
AML Exporter
ire 'daru/io'
s Daru::IO::Exporters::YAML < Daru::IO::Exporters::Base
ru::DataFrame.register_io_module :to_yaml, self
ru::DataFrame.register_io_module :to_yaml_string, self
ru::DataFrame.register_io_module :write_yaml, self
f initialize(dataframe, opts)
super(dataframe) #! Have a look at documentation of Daru::IO::Exporters::Base#initialize
@opts = opts
d
f to
#! Your code to return a YAML instance
d
f to_s
super
#! By default, Exporters::Base adds this to_s method to all Exporters,
#! by making the write mthod to write to a tempfile, and then reading it.
d
f write(path)
#! Your code to write the YAML file
#! with the data in the @dataframe
d
Daru::DataFrame.new(x: [1,2], y: [3,4])
o_yaml(rows: 10..19) #! or, Daru::IO::Exporters::YAML.new(df, rows: 10..19).to
o_yaml_string(rows: 10..19) #! or, Daru::IO::Exporters::YAML.new(df, rows: 10..19).to_s
rite_yaml('dataframe.yml', rows: 10..19) #! or, Daru::IO::Exporters::YAML.new(df, rows: 10..19).write('dataframe.yml')
Adding new IO modules to custom modules
Behaviour of existing IO modules can also be reused according to your needs, similar to the above example. For example, if the CSV Importer has to be tweaked with a faster processing gem, simply follow an approach similar to this -
s CustomNamespace::Importers::CSV < Daru::IO::Importers::CSV
ru::DataFrame.register_io_module :custom_csv, self
Your CSV Importer code here
Note: The new module can be made to inherit from another module (like Importers::JSON
) rather than Importers::Base
, depending on use-case (say, parse a complexly nested API response with JsonPaths).
Contributions are always welcome. But, please have a look at the contribution guidelines first before contributing. :tada:
The MIT License (MIT) 2017 - Athitya Kumar and Ruby Science Foundation. Please have a look at the LICENSE.md for more details.