daru-io 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.rspec_formatter.rb +24 -0
- data/.rubocop.yml +109 -0
- data/.travis.yml +30 -0
- data/.yardopts +2 -0
- data/CODE_OF_CONDUCT.md +46 -0
- data/CONTRIBUTING.md +65 -0
- data/Gemfile +20 -0
- data/Guardfile +7 -0
- data/LICENSE.md +21 -0
- data/README.md +654 -0
- data/Rakefile +12 -0
- data/daru-io.gemspec +39 -0
- data/lib/daru/io.rb +3 -0
- data/lib/daru/io/base.rb +45 -0
- data/lib/daru/io/exporters.rb +1 -0
- data/lib/daru/io/exporters/avro.rb +96 -0
- data/lib/daru/io/exporters/base.rb +54 -0
- data/lib/daru/io/exporters/csv.rb +103 -0
- data/lib/daru/io/exporters/excel.rb +148 -0
- data/lib/daru/io/exporters/json.rb +570 -0
- data/lib/daru/io/exporters/r_data.rb +66 -0
- data/lib/daru/io/exporters/rds.rb +79 -0
- data/lib/daru/io/exporters/sql.rb +55 -0
- data/lib/daru/io/importers.rb +1 -0
- data/lib/daru/io/importers/active_record.rb +75 -0
- data/lib/daru/io/importers/avro.rb +54 -0
- data/lib/daru/io/importers/base.rb +62 -0
- data/lib/daru/io/importers/csv.rb +190 -0
- data/lib/daru/io/importers/excel.rb +99 -0
- data/lib/daru/io/importers/excelx.rb +138 -0
- data/lib/daru/io/importers/html.rb +144 -0
- data/lib/daru/io/importers/json.rb +152 -0
- data/lib/daru/io/importers/mongo.rb +139 -0
- data/lib/daru/io/importers/plaintext.rb +97 -0
- data/lib/daru/io/importers/r_data.rb +74 -0
- data/lib/daru/io/importers/rds.rb +67 -0
- data/lib/daru/io/importers/redis.rb +135 -0
- data/lib/daru/io/importers/sql.rb +127 -0
- data/lib/daru/io/link.rb +80 -0
- data/lib/daru/io/version.rb +5 -0
- metadata +269 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c1a45587a595f3d6c836314ef552e54fb51953ae
|
4
|
+
data.tar.gz: f71f9ba75000d5edfc249cbf83c04404bcf73b66
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d73293b623a63bde856d5c791353da6501ffbfd0332ef4ffbf41cd92d2bce48376f86870b34b6d6dd175acfeb6bbdafc2130fc7db00dd7164b1fc6f14a5e816
|
7
|
+
data.tar.gz: 9b72a21fc1bf2622ccaeaed6c7f7e221934c04028e4b585447faeec578ad42a3c85f63bf380beb133d8e897485c5032a4887363e59fd55e14d8f83f112353ef8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rspec_formatter.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
RSpec::Support.require_rspec_core 'formatters/base_text_formatter'
|
2
|
+
|
3
|
+
class SimpleFormatter < RSpec::Core::Formatters::BaseTextFormatter
|
4
|
+
RSpec::Core::Formatters.register self,
|
5
|
+
:example_passed, :example_pending, :example_failed, :dump_pending, :dump_failures, :dump_summary
|
6
|
+
|
7
|
+
def example_passed(message); end
|
8
|
+
|
9
|
+
def example_pending(message); end
|
10
|
+
|
11
|
+
def example_failed(message); end
|
12
|
+
|
13
|
+
def dump_pending(message); end
|
14
|
+
|
15
|
+
def dump_failures(message); end
|
16
|
+
|
17
|
+
def dump_summary(message)
|
18
|
+
colorizer = ::RSpec::Core::Formatters::ConsoleCodes
|
19
|
+
|
20
|
+
output.puts "\nFinished in #{message.formatted_duration} " \
|
21
|
+
"(files took #{message.formatted_load_time} to load)\n" \
|
22
|
+
"#{message.colorized_totals_line(colorizer)}\n"
|
23
|
+
end
|
24
|
+
end
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
Include:
|
5
|
+
- 'lib/**/*'
|
6
|
+
Exclude:
|
7
|
+
- 'vendor/**/*'
|
8
|
+
- 'benchmarks/*'
|
9
|
+
- 'profile/*'
|
10
|
+
DisplayCopNames: true
|
11
|
+
TargetRubyVersion: 2.1
|
12
|
+
|
13
|
+
# Preferred codebase style ---------------------------------------------
|
14
|
+
|
15
|
+
### Layouts ------------------------------------------------------------
|
16
|
+
|
17
|
+
Layout/AlignParameters:
|
18
|
+
EnforcedStyle: with_fixed_indentation
|
19
|
+
|
20
|
+
Layout/ExtraSpacing:
|
21
|
+
AllowForAlignment: true
|
22
|
+
|
23
|
+
Layout/SpaceAfterComma:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
27
|
+
EnforcedStyle: no_space
|
28
|
+
|
29
|
+
Layout/SpaceAroundOperators:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Layout/SpaceInsideBlockBraces:
|
33
|
+
EnforcedStyle: space
|
34
|
+
|
35
|
+
Layout/SpaceInsideHashLiteralBraces:
|
36
|
+
EnforcedStyle: no_space
|
37
|
+
|
38
|
+
### Styles -------------------------------------------------------------
|
39
|
+
|
40
|
+
Style/AndOr:
|
41
|
+
EnforcedStyle: conditionals
|
42
|
+
|
43
|
+
Style/DoubleNegation:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Style/EmptyCaseCondition:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Style/EmptyElse:
|
50
|
+
EnforcedStyle: empty
|
51
|
+
|
52
|
+
Style/EmptyMethod:
|
53
|
+
EnforcedStyle: compact
|
54
|
+
|
55
|
+
Style/FileName:
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Style/FormatString:
|
59
|
+
EnforcedStyle: percent
|
60
|
+
|
61
|
+
Style/ParallelAssignment:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Style/SingleLineBlockParams:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
Style/PerlBackrefs:
|
68
|
+
Enabled: false
|
69
|
+
|
70
|
+
Style/Documentation:
|
71
|
+
Enabled: false # TODO
|
72
|
+
|
73
|
+
### Metrics ------------------------------------------------------------
|
74
|
+
|
75
|
+
Metrics/AbcSize:
|
76
|
+
Max: 20
|
77
|
+
|
78
|
+
Metrics/BlockLength:
|
79
|
+
Exclude:
|
80
|
+
- 'spec/**/*'
|
81
|
+
|
82
|
+
Metrics/ClassLength:
|
83
|
+
Max: 200
|
84
|
+
|
85
|
+
Metrics/CyclomaticComplexity:
|
86
|
+
Max: 7
|
87
|
+
|
88
|
+
Metrics/LineLength:
|
89
|
+
Max: 120
|
90
|
+
|
91
|
+
Metrics/MethodLength:
|
92
|
+
Max: 15
|
93
|
+
|
94
|
+
Metrics/ModuleLength:
|
95
|
+
Max: 200
|
96
|
+
|
97
|
+
Style/MultilineBlockChain:
|
98
|
+
Enabled: false
|
99
|
+
|
100
|
+
Metrics/ParameterLists:
|
101
|
+
Max: 10
|
102
|
+
|
103
|
+
### RSpec --------------------------------------------------------------
|
104
|
+
|
105
|
+
RSpec/MessageSpies:
|
106
|
+
EnforcedStyle: receive
|
107
|
+
|
108
|
+
RSpec/NestedGroups:
|
109
|
+
Max: 5
|
data/.travis.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
language:
|
2
|
+
ruby
|
3
|
+
|
4
|
+
rvm:
|
5
|
+
- '2.1'
|
6
|
+
- '2.2'
|
7
|
+
- '2.3.0'
|
8
|
+
- '2.4.0'
|
9
|
+
|
10
|
+
script:
|
11
|
+
- bundle exec rspec
|
12
|
+
- bundle exec rubocop
|
13
|
+
|
14
|
+
services:
|
15
|
+
- redis-server
|
16
|
+
- mongodb
|
17
|
+
|
18
|
+
before_install:
|
19
|
+
- redis-server --daemonize yes
|
20
|
+
|
21
|
+
install:
|
22
|
+
- gem install bundler
|
23
|
+
- gem install rainbow -v '2.2.1'
|
24
|
+
- sudo apt-get update -qq
|
25
|
+
- sudo apt-get install -y libgsl0-dev r-base r-base-dev
|
26
|
+
- sudo Rscript -e "install.packages(c('Rserve','irr'),,'http://cran.us.r-project.org')"
|
27
|
+
- R CMD Rserve
|
28
|
+
- export R_HOME=/usr/lib/R
|
29
|
+
- gem install rsruby -- --with-R-dir=$R_HOME --with-R-include=/usr/share/R/include --with_cflags="-fPIC -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wall -fno-strict-aliasing"
|
30
|
+
- bundle install
|
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
## Our Standards
|
8
|
+
|
9
|
+
Examples of behavior that contributes to creating a positive environment include:
|
10
|
+
|
11
|
+
* Using welcoming and inclusive language
|
12
|
+
* Being respectful of differing viewpoints and experiences
|
13
|
+
* Gracefully accepting constructive criticism
|
14
|
+
* Focusing on what is best for the community
|
15
|
+
* Showing empathy towards other community members
|
16
|
+
|
17
|
+
Examples of unacceptable behavior by participants include:
|
18
|
+
|
19
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
20
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
21
|
+
* Public or private harassment
|
22
|
+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
23
|
+
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
24
|
+
|
25
|
+
## Our Responsibilities
|
26
|
+
|
27
|
+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
28
|
+
|
29
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
30
|
+
|
31
|
+
## Scope
|
32
|
+
|
33
|
+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
34
|
+
|
35
|
+
## Enforcement
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at athityakumar@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
38
|
+
|
39
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
40
|
+
|
41
|
+
## Attribution
|
42
|
+
|
43
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
44
|
+
|
45
|
+
[homepage]: http://contributor-covenant.org
|
46
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Contribution guidelines
|
2
|
+
|
3
|
+
First of all, thanks for thinking of contributing to this project. :smile:
|
4
|
+
|
5
|
+
Before sending a Pull Request, please make sure that you're assigned the task on a GitHub issue.
|
6
|
+
|
7
|
+
- If a relevant issue already exists, discuss on the issue and get it assigned to yourself on GitHub.
|
8
|
+
- If no relevant issue exists, open a new issue and get it assigned to yourself on GitHub.
|
9
|
+
|
10
|
+
Please proceed with a Pull Request only after you're assigned. It'd be sad if your Pull Request (and your hardwork) isn't accepted just because it isn't idealogically compatible.
|
11
|
+
|
12
|
+
# Developing the gem
|
13
|
+
|
14
|
+
1. Install required dependencies.
|
15
|
+
|
16
|
+
- For the Mongo Importer, install Mongo.
|
17
|
+
- For the RData Importer, RData Exporter, RDS Importer or RDS Exporter, install R and set the R_HOME
|
18
|
+
variable in your shell configuration:
|
19
|
+
```sh
|
20
|
+
export R_HOME=/usr/lib/R # For Unix systems
|
21
|
+
export R_HOME=/usr/local/Frameworks/R.framework/Resources # For Mac systems
|
22
|
+
```
|
23
|
+
- For the Redis Importer, install Redis and start the redis server by typing `redis-server` in another
|
24
|
+
terminal window, before running the test suites.
|
25
|
+
|
26
|
+
For any issue(s) related to installation steps, kindly refer to the configurations mentioned in the
|
27
|
+
`.travis.yml` file.
|
28
|
+
|
29
|
+
2. Clone this repository and install all the required gem dependencies.
|
30
|
+
|
31
|
+
```sh
|
32
|
+
git clone https://github.com/athityakumar/daru-io.git
|
33
|
+
cd daru-io
|
34
|
+
gem install bundler
|
35
|
+
bundle install
|
36
|
+
```
|
37
|
+
|
38
|
+
3. Checkout to a different git branch (say, `adds-format-importer`).
|
39
|
+
|
40
|
+
4. Add any gem dependencies required for the Format Importer to the `:optional` group of the Gemfile.
|
41
|
+
|
42
|
+
5. Add code and YARD documentation to `lib/daru/io/importers/format.rb`, consistent with other IO modules.
|
43
|
+
|
44
|
+
6. Add tests to `spec/daru/io/importers/format_spec.rb`. Add any `.format` files required for importer in `spec/fixtures/format/` directory.
|
45
|
+
|
46
|
+
7. Run the rspec test-suite.
|
47
|
+
```sh
|
48
|
+
# Runs test suite for all Importers & Exporters
|
49
|
+
bundle exec rspec
|
50
|
+
|
51
|
+
# Runs test-suite only for the newly added Format Importer
|
52
|
+
bundle exec rspec spec/daru/io/importers/format_spec.rb
|
53
|
+
```
|
54
|
+
|
55
|
+
8. Run the rubocop for static code quality comments.
|
56
|
+
|
57
|
+
```sh
|
58
|
+
# Runs rubocop test for all Importer & Exporters
|
59
|
+
bundle exec rubocop
|
60
|
+
|
61
|
+
# Runs rubocop test only for the newly added Format Importer
|
62
|
+
bundle exec rubocop lib/daru/io/importers/format.rb spec/daru/io/importers/format_spec.rb
|
63
|
+
```
|
64
|
+
|
65
|
+
9. Send a Pull Request back to this repository. :tada:
|
data/Gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
gemspec
|
3
|
+
|
4
|
+
group :optional do
|
5
|
+
gem 'activerecord', '~> 4.0'
|
6
|
+
gem 'avro'
|
7
|
+
gem 'dbd-sqlite3'
|
8
|
+
gem 'dbi'
|
9
|
+
gem 'jsonpath'
|
10
|
+
gem 'mongo'
|
11
|
+
gem 'nokogiri'
|
12
|
+
gem 'redis'
|
13
|
+
gem 'roo', '~> 2.7.0'
|
14
|
+
gem 'rsruby'
|
15
|
+
gem 'snappy'
|
16
|
+
gem 'spreadsheet', '~> 1.1.1'
|
17
|
+
gem 'sqlite3'
|
18
|
+
end
|
19
|
+
|
20
|
+
gem 'saharspec', git: 'https://github.com/zverok/saharspec.git'
|
data/Guardfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 [Athitya Kumar](https://github.com/athityakumar/) and [Ruby Science Foundation](https://github.com/SciRuby/).
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,654 @@
|
|
1
|
+
# Daru-IO
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/athityakumar/daru-io.svg?branch=master)](https://travis-ci.org/athityakumar/daru-io)
|
4
|
+
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/athityakumar/daru-io/master/)
|
5
|
+
[![Inline docs](http://inch-ci.org/github/athityakumar/daru-io.png)](http://inch-ci.org/github/athityakumar/daru-io)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/athityakumar/daru-io.png)](https://codeclimate.com/github/athityakumar/daru-io)
|
7
|
+
[![Stories in Ready](https://badge.waffle.io/athityakumar/daru-io.png?label=ready&title=Ready)](https://waffle.io/athityakumar/daru-io?utm_source=badge)
|
8
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
|
9
|
+
|
10
|
+
A Ruby plugin-gem to [daru gem](https://github.com/SciRuby/daru), that extends support for many Import and
|
11
|
+
Export methods of **Daru::DataFrame**. This gem is intended to help Rubyists who are into Data Analysis or Web
|
12
|
+
Development, by serving as a general purpose conversion library that takes input in one format (say, JSON) and
|
13
|
+
converts it another format (say, Avro) while also making it incredibly easy to getting started on
|
14
|
+
analyzing data with daru.
|
15
|
+
|
16
|
+
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](#creating-your-own-io-modules), if you're interested in creating new Importers / Exporters.**
|
17
|
+
|
18
|
+
# Table of contents
|
19
|
+
|
20
|
+
- [Installation](#installation)
|
21
|
+
- *[Importers](#importers): [ActiveRecord](#activerecord-importer), [Avro](#avro-importer), [CSV](#csv-importer), [Excel](#excel-importer), [Excelx](#excelx-importer), [HTML](#html-importer), [JSON](#json-importer), [Mongo](#mongo-importer), [Plaintext](#plaintext-importer), [RData](#rdata-importer), [RDS](#rds-importer), [Redis](#redis-importer), [SQL](#sql-importer)*
|
22
|
+
- *[Exporters](#exporters): [Avro](#avro-exporter), [CSV](#csv-exporter), [Excel](#excel-exporter), [JSON](#json-exporter), [RData](#rdata-exporter), [RDS](#rds-exporter), [SQL](#sql-exporter)*
|
23
|
+
- [Creating your own IO modules](#creating-your-own-io-modules)
|
24
|
+
- [Contributing](#contributing)
|
25
|
+
- [License](#license)
|
26
|
+
|
27
|
+
# Installation
|
28
|
+
|
29
|
+
[(Go to Table of Contents)](#table-of-contents)
|
30
|
+
|
31
|
+
- If you're working with a Gemfile,
|
32
|
+
|
33
|
+
- Add this line to your application's Gemfile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
gem 'daru-io'
|
37
|
+
```
|
38
|
+
|
39
|
+
- And then execute on your terminal:
|
40
|
+
|
41
|
+
```sh
|
42
|
+
bundle
|
43
|
+
```
|
44
|
+
|
45
|
+
- If you're NOT working with a Gemfile, simply install it yourself by executing on your terminal:
|
46
|
+
|
47
|
+
```sh
|
48
|
+
gem install daru-io
|
49
|
+
```
|
50
|
+
|
51
|
+
- Require `daru-io` gem in your application:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
require 'daru/io' #! Requires all Importers & Exporters
|
55
|
+
require 'daru/io/importers' #! Requires all Importers and no Exporters
|
56
|
+
require 'daru/io/importers/json' #! Requires only JSON Importer
|
57
|
+
```
|
58
|
+
|
59
|
+
**Note: Each IO module has it's own set of dependencies. Have a look at the [Importers](#importers) and [Exporters](#exporters) section for dependency-specific information.**
|
60
|
+
|
61
|
+
# Importers
|
62
|
+
|
63
|
+
The **Daru::IO** Importers are intended to return a **Daru::DataFrame** from the given arguments. Generally,
|
64
|
+
all Importers can be called in two ways - from **Daru::IO** or **Daru::DataFrame**.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
#! Partially requires Format Importer
|
68
|
+
require 'daru/io/importers/format'
|
69
|
+
|
70
|
+
#! Usage from Daru::IO
|
71
|
+
instance = Daru::IO::Importers::Format.from(connection)
|
72
|
+
# or,
|
73
|
+
instance = Daru::IO::Importers::Format.read(path)
|
74
|
+
df = instance.call(opts)
|
75
|
+
|
76
|
+
#! Usage from Daru::DataFrame
|
77
|
+
df1 = Daru::DataFrame.from_format(connection, opts)
|
78
|
+
df2 = Daru::DataFrame.read_format(path, opts)
|
79
|
+
```
|
80
|
+
|
81
|
+
**Note: Please have a look at the respective Importer Doc links below, for having a look at arguments and examples.**
|
82
|
+
|
83
|
+
### ActiveRecord Importer
|
84
|
+
|
85
|
+
[(Go to Table of Contents)](#table-of-contents)
|
86
|
+
|
87
|
+
Imports a **Daru::DataFrame** from an **ActiveRecord** connection.
|
88
|
+
|
89
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/ActiveRecord)
|
90
|
+
- **Gem Dependencies**: `activerecord` gem
|
91
|
+
- **Other Dependencies**: Install database server(s) such as SQL / Postgresql / etc.
|
92
|
+
- **Usage**:
|
93
|
+
```ruby
|
94
|
+
#! Partially require just ActiveRecord Importer
|
95
|
+
require 'daru/io/importers/active_record'
|
96
|
+
|
97
|
+
#! Usage from Daru::IO
|
98
|
+
df = Daru::IO::Importers::ActiveRecord.from(activerecord_relation).call(:field_1, :field_2)
|
99
|
+
|
100
|
+
#! Usage from Daru::DataFrame
|
101
|
+
df = Daru::DataFrame.from_activerecord(activerecord_relation, :field_1, :field_2)
|
102
|
+
```
|
103
|
+
|
104
|
+
### Avro Importer
|
105
|
+
|
106
|
+
[(Go to Table of Contents)](#table-of-contents)
|
107
|
+
|
108
|
+
Imports a **Daru::DataFrame** from an **.avro** file.
|
109
|
+
|
110
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/Avro)
|
111
|
+
- **Gem Dependencies**: `avro` and `snappy` gems
|
112
|
+
- **Usage**:
|
113
|
+
```ruby
|
114
|
+
#! Partially require just Avro Importer
|
115
|
+
require 'daru/io/importers/avro'
|
116
|
+
|
117
|
+
#! Usage from Daru::IO
|
118
|
+
df = Daru::IO::Importers::Avro.read('path/to/file.avro').call
|
119
|
+
|
120
|
+
#! Usage from Daru::DataFrame
|
121
|
+
df = Daru::DataFrame.read_avro('path/to/file.avro')
|
122
|
+
```
|
123
|
+
|
124
|
+
### CSV Importer
|
125
|
+
|
126
|
+
[(Go to Table of Contents)](#table-of-contents)
|
127
|
+
|
128
|
+
Imports a **Daru::DataFrame** from a **.csv** or **.csv.gz** file.
|
129
|
+
|
130
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/CSV)
|
131
|
+
- **Usage**:
|
132
|
+
```ruby
|
133
|
+
#! Partially require just CSV Importer
|
134
|
+
require 'daru/io/importers/csv'
|
135
|
+
|
136
|
+
#! Usage from Daru::IO
|
137
|
+
df1 = Daru::IO::Importers::CSV.read('path/to/file.csv').call(skiprows: 10, col_sep: ' ')
|
138
|
+
df2 = Daru::IO::Importers::CSV.read('path/to/file.csv.gz').call(skiprows: 10, compression: :gzip)
|
139
|
+
|
140
|
+
#! Usage from Daru::DataFrame
|
141
|
+
df1 = Daru::DataFrame.read_csv('path/to/file.csv', skiprows: 10, col_sep: ' ')
|
142
|
+
df2 = Daru::DataFrame.read_csv('path/to/file.csv.gz', skiprows: 10, compression: :gzip)
|
143
|
+
```
|
144
|
+
|
145
|
+
### Excel Importer
|
146
|
+
|
147
|
+
[(Go to Table of Contents)](#table-of-contents)
|
148
|
+
|
149
|
+
Imports a **Daru::DataFrame** from a **.xls** file.
|
150
|
+
|
151
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/Excel)
|
152
|
+
- **Gem Dependencies**: `spreadsheet` gem
|
153
|
+
- **Usage**:
|
154
|
+
```ruby
|
155
|
+
#! Partially require just Excel Importer
|
156
|
+
require 'daru/io/importers/excel'
|
157
|
+
|
158
|
+
#! Usage from Daru::IO
|
159
|
+
df = Daru::IO::Importers::Excel.read('path/to/file.xls').call(worksheet_id: 1)
|
160
|
+
|
161
|
+
#! Usage from Daru::DataFrame
|
162
|
+
df = Daru::DataFrame.read_excel('path/to/file.xls', worksheet_id: 1)
|
163
|
+
```
|
164
|
+
|
165
|
+
### Excelx Importer
|
166
|
+
|
167
|
+
[(Go to Table of Contents)](#table-of-contents)
|
168
|
+
|
169
|
+
Imports a **Daru::DataFrame** from a **.xlsx** file.
|
170
|
+
|
171
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/Excelx)
|
172
|
+
- **Gem Dependencies**: `roo` gem
|
173
|
+
- **Usage**:
|
174
|
+
```ruby
|
175
|
+
#! Partially require just Excel Importer
|
176
|
+
require 'daru/io/importers/excelx'
|
177
|
+
|
178
|
+
#! Usage from Daru::IO
|
179
|
+
df = Daru::IO::Importers::Excelx.read('path/to/file.xlsx').call(sheet: 2, skiprows: 10, skipcols: 2)
|
180
|
+
|
181
|
+
#! Usage from Daru::DataFrame
|
182
|
+
require 'daru/io/importers/excel'
|
183
|
+
df = Daru::DataFrame.read_excel('path/to/file.xlsx', sheet: 2, skiprows: 10, skipcols: 2)
|
184
|
+
```
|
185
|
+
|
186
|
+
### HTML Importer
|
187
|
+
|
188
|
+
[(Go to Table of Contents)](#table-of-contents)
|
189
|
+
|
190
|
+
**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.**
|
191
|
+
|
192
|
+
Imports an **Array** of **Daru::DataFrame**s from a **.html** file or website.
|
193
|
+
|
194
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/HTML)
|
195
|
+
- **Gem Dependencies**: `nokogiri` gem
|
196
|
+
- **Usage**:
|
197
|
+
```ruby
|
198
|
+
#! Partially require just HTML Importer
|
199
|
+
require 'daru/io/importers/html'
|
200
|
+
|
201
|
+
#! Usage from Daru::IO
|
202
|
+
df1 = Daru::IO::Importers::HTML.read('https://some/url/with/tables').call(match: 'market', name: 'Shares analysis')
|
203
|
+
df2 = Daru::IO::Importers::HTML.read('path/to/file.html').call(match: 'market', name: 'Shares analysis')
|
204
|
+
|
205
|
+
#! Usage from Daru::DataFrame
|
206
|
+
df1 = Daru::DataFrame.read_html('https://some/url/with/tables', match: 'market', name: 'Shares analysis')
|
207
|
+
df2 = Daru::DataFrame.read_html('path/to/file.html', match: 'market', name: 'Shares analysis')
|
208
|
+
```
|
209
|
+
|
210
|
+
### JSON Importer
|
211
|
+
|
212
|
+
[(Go to Table of Contents)](#table-of-contents)
|
213
|
+
|
214
|
+
Imports a **Daru::DataFrame** from a **.json** file / response.
|
215
|
+
|
216
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/JSON)
|
217
|
+
- **Gem Dependencies**: `jsonpath` gem
|
218
|
+
- **Usage**:
|
219
|
+
```ruby
|
220
|
+
#! Partially require just JSON Importer
|
221
|
+
require 'daru/io/importers/json'
|
222
|
+
|
223
|
+
#! Usage from Daru::IO
|
224
|
+
df1 = Daru::IO::Importers::JSON.read('https://path/to/json/response').call(index: '$..time', col1: '$..name', col2: '$..age')
|
225
|
+
df2 = Daru::IO::Importers::JSON.read('path/to/file.json').call(index: '$..time', col1: '$..name', col2: '$..age')
|
226
|
+
|
227
|
+
#! Usage from Daru::DataFrame
|
228
|
+
df1 = Daru::DataFrame.read_json('https://path/to/json/response', index: '$..time', col1: '$..name', col2: '$..age')
|
229
|
+
df2 = Daru::DataFrame.read_json('path/to/file.json', index: '$..time', col1: '$..name', col2: '$..age')
|
230
|
+
```
|
231
|
+
|
232
|
+
### Mongo Importer
|
233
|
+
|
234
|
+
[(Go to Table of Contents)](#table-of-contents)
|
235
|
+
|
236
|
+
**Note: The Mongo gem faces Argument Error : expected Proc Argument issue due to the bug in MRI Ruby 2.4.0 mentioned [here](https://bugs.ruby-lang.org/issues/13107). 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.**
|
237
|
+
|
238
|
+
Imports a **Daru::DataFrame** from a Mongo collection.
|
239
|
+
|
240
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/Mongo)
|
241
|
+
- **Gem Dependencies**: `jsonpath` and `mongo` gems
|
242
|
+
- **Other Dependencies**: Install MongoDB
|
243
|
+
- **Usage**:
|
244
|
+
```ruby
|
245
|
+
#! Partially require just Mongo Importer
|
246
|
+
require 'daru/io/importers/mongo'
|
247
|
+
|
248
|
+
#! Usage from Daru::IO
|
249
|
+
df = Daru::IO::Importers::Mongo.from('mongodb://127.0.0.1:27017/test').call('cars')
|
250
|
+
|
251
|
+
#! Usage from Daru::DataFrame
|
252
|
+
df = Daru::DataFrame.from_mongo('mongodb://127.0.0.1:27017/test', 'cars')
|
253
|
+
```
|
254
|
+
|
255
|
+
### Plaintext Importer
|
256
|
+
|
257
|
+
[(Go to Table of Contents)](#table-of-contents)
|
258
|
+
|
259
|
+
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](https://github.com/athityakumar/daru-io/blob/master/spec/fixtures/plaintext/bank2.dat) file.
|
260
|
+
|
261
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/Plaintext)
|
262
|
+
- **Usage**:
|
263
|
+
```ruby
|
264
|
+
#! Partially require just Plaintext Importer
|
265
|
+
require 'daru/io/importers/plaintext'
|
266
|
+
|
267
|
+
#! Usage from Daru::IO
|
268
|
+
df = Daru::IO::Importers::Plaintext.read('path/to/file.dat').call([:col1, :col2, :col3])
|
269
|
+
|
270
|
+
#! Usage from Daru::DataFrame
|
271
|
+
df = Daru::DataFrame.read_plaintext('path/to/file.dat', [:col1, :col2, :col3])
|
272
|
+
```
|
273
|
+
|
274
|
+
### RData Importer
|
275
|
+
|
276
|
+
[(Go to Table of Contents)](#table-of-contents)
|
277
|
+
|
278
|
+
Imports a **Daru::DataFrame** from a variable in **.rdata** file.
|
279
|
+
|
280
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/RData)
|
281
|
+
- **Gem Dependencies**: `rsruby` gem
|
282
|
+
- **Other Dependencies**: Install R and set `R_HOME` variable as given in the [Contribution Guidelines](CONTRIBUTING.md)
|
283
|
+
- **Usage**:
|
284
|
+
```ruby
|
285
|
+
#! Partially require just RData Importer
|
286
|
+
require 'daru/io/importers/r_data'
|
287
|
+
|
288
|
+
#! Usage from Daru::IO
|
289
|
+
df = Daru::IO::Importers::RData.read('path/to/file.RData').call('ACS3')
|
290
|
+
|
291
|
+
#! Usage from Daru::DataFrame
|
292
|
+
df = Daru::DataFrame.read_rdata('path/to/file.RData', 'ACS3')
|
293
|
+
```
|
294
|
+
|
295
|
+
### RDS Importer
|
296
|
+
|
297
|
+
[(Go to Table of Contents)](#table-of-contents)
|
298
|
+
|
299
|
+
Imports a **Daru::DataFrame** from a **.rds** file.
|
300
|
+
|
301
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/RDS)
|
302
|
+
- **Gem Dependencies**: `rsruby` gem
|
303
|
+
- **Other Dependencies**: Install R and set `R_HOME` variable as given in the [Contribution Guidelines](CONTRIBUTING.md)
|
304
|
+
- **Usage**:
|
305
|
+
```ruby
|
306
|
+
#! Partially require just RDS Importer
|
307
|
+
require 'daru/io/importers/rds'
|
308
|
+
|
309
|
+
#! Usage from Daru::IO
|
310
|
+
df = Daru::IO::Importers::RDS.read('path/to/file.rds').call
|
311
|
+
|
312
|
+
#! Usage from Daru::DataFrame
|
313
|
+
df = Daru::DataFrame.read_rds('path/to/file.rds')
|
314
|
+
```
|
315
|
+
|
316
|
+
### Redis Importer
|
317
|
+
|
318
|
+
[(Go to Table of Contents)](#table-of-contents)
|
319
|
+
|
320
|
+
Imports a **Daru::DataFrame** from **Redis** key(s).
|
321
|
+
|
322
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/Redis)
|
323
|
+
- **Gem Dependencies**: `redis` gem
|
324
|
+
- **Other Dependencies**: Install Redis, and run an instance of `redis-server`
|
325
|
+
- **Usage**:
|
326
|
+
```ruby
|
327
|
+
#! Partially require just Redis Importer
|
328
|
+
require 'daru/io/importers/redis'
|
329
|
+
|
330
|
+
#! Usage from Daru::IO
|
331
|
+
df = Daru::IO::Importers::Redis.from({url: 'redis://:password@host:port/db'}).call(match: 'time:1*', count: 1000)
|
332
|
+
|
333
|
+
#! Usage from Daru::DataFrame
|
334
|
+
df = Daru::DataFrame.from_redis({url: 'redis://:password@host:port/db'}, match: 'time:1*', count: 1000)
|
335
|
+
```
|
336
|
+
|
337
|
+
### SQL Importer
|
338
|
+
|
339
|
+
[(Go to Table of Contents)](#table-of-contents)
|
340
|
+
|
341
|
+
Imports a **Daru::DataFrame** from a **sqlite.db** file / **DBI** connection.
|
342
|
+
|
343
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Importers/SQL)
|
344
|
+
- **Gem Dependencies**: `dbd-sqlite3`, `activerecord`, `dbi` and `sqlite3` gems
|
345
|
+
- **Usage**:
|
346
|
+
```ruby
|
347
|
+
#! Partially require just SQL Importer
|
348
|
+
require 'daru/io/importers/sql'
|
349
|
+
|
350
|
+
#! Usage from Daru::IO
|
351
|
+
df1 = Daru::IO::Importers::SQL.read('path/to/file.sqlite').call('SELECT * FROM test')
|
352
|
+
df2 = Daru::IO::Importers::SQL.from(dbi_connection).call('SELECT * FROM test')
|
353
|
+
|
354
|
+
#! Usage from Daru::DataFrame
|
355
|
+
df1 = Daru::DataFrame.read_sql('path/to/file.sqlite', 'SELECT * FROM test')
|
356
|
+
df2 = Daru::DataFrame.from_sql(dbi_connection, 'SELECT * FROM test')
|
357
|
+
```
|
358
|
+
|
359
|
+
# Exporters
|
360
|
+
|
361
|
+
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**.
|
362
|
+
|
363
|
+
```ruby
|
364
|
+
#! Partially requires Format Exporter
|
365
|
+
require 'daru/io/exporters/format'
|
366
|
+
|
367
|
+
#! Usage from Daru::IO
|
368
|
+
instance = Daru::IO::Exporters::Format.new(df, opts)
|
369
|
+
instance.to_s #=> Provides a file-writable string, which can be used in web applications for file download purposes
|
370
|
+
instance.to #=> Provides a Format instance
|
371
|
+
instance.write(path) #=> Writes to the given path
|
372
|
+
|
373
|
+
#! Usage from Daru::DataFrame
|
374
|
+
string = df.to_format_string(opts) #=> Provides a file-writable string, which can be to write into a file later
|
375
|
+
instance = df.to_format(opts) #=> Provides a Format instance
|
376
|
+
df.write_format(path, opts) #=> Writes to the given path
|
377
|
+
```
|
378
|
+
|
379
|
+
**Note: Please have a look at the respective Exporter Doc links below, for having a look at arguments and examples.**
|
380
|
+
|
381
|
+
### Avro Exporter
|
382
|
+
|
383
|
+
[(Go to Table of Contents)](#table-of-contents)
|
384
|
+
|
385
|
+
Exports a **Daru::DataFrame** into a **.avro** file.
|
386
|
+
|
387
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Exporters/Avro)
|
388
|
+
- **Gem Dependencies**: `avro` gem
|
389
|
+
- **Usage**:
|
390
|
+
```ruby
|
391
|
+
#! Partially require just Avro Exporter
|
392
|
+
require 'daru/io/exporters/avro'
|
393
|
+
|
394
|
+
avro_schema = {
|
395
|
+
'type' => 'record',
|
396
|
+
'name' => 'Example',
|
397
|
+
'fields' => [
|
398
|
+
{'name' => 'col_1', 'type' => 'string'},
|
399
|
+
{'name' => 'col_2', 'type' => 'int'},
|
400
|
+
{'name' => 'col_3', 'type'=> 'boolean'}
|
401
|
+
]
|
402
|
+
}
|
403
|
+
|
404
|
+
#! Usage from Daru::IO
|
405
|
+
string = Daru::IO::Exporters::Avro.new(df, avro_schema).to_s
|
406
|
+
Daru::IO::Exporters::Avro.new(df, avro_schema).write('path/to/file.avro')
|
407
|
+
|
408
|
+
#! Usage from Daru::DataFrame
|
409
|
+
string = df.to_avro_string(avro_schema)
|
410
|
+
df.write_avro('path/to/file.avro', avro_schema)
|
411
|
+
```
|
412
|
+
|
413
|
+
### CSV Exporter
|
414
|
+
|
415
|
+
[(Go to Table of Contents)](#table-of-contents)
|
416
|
+
|
417
|
+
Exports a **Daru::DataFrame** into a **.csv** or **.csv.gz** file.
|
418
|
+
|
419
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Exporters/CSV)
|
420
|
+
- **Usage**:
|
421
|
+
```ruby
|
422
|
+
#! Partially require just CSV Exporter
|
423
|
+
require 'daru/io/exporters/csv'
|
424
|
+
|
425
|
+
#! Usage from Daru::IO
|
426
|
+
csv_string = Daru::IO::Exporters::CSV.new(df, converters: :numeric, convert_comma: true).to_s
|
427
|
+
Daru::IO::Exporters::CSV.new(df, converters: :numeric, convert_comma: true).write('path/to/file.csv')
|
428
|
+
csv_gz_string = Daru::IO::Exporters::CSV.new(df, converters: :numeric, compression: :gzip, convert_comma: true).to_s
|
429
|
+
Daru::IO::Exporters::CSV.new(df, converters: :numeric, compression: :gzip, convert_comma: true).write('path/to/file.csv.gz')
|
430
|
+
|
431
|
+
#! Usage from Daru::DataFrame
|
432
|
+
csv_string = df.to_csv_string(converters: :numeric, convert_comma: true)
|
433
|
+
df.write_csv('path/to/file.csv', converters: :numeric, convert_comma: true)
|
434
|
+
csv_gz_string = df.to_csv_string(converters: :numeric, compression: :gzip, convert_comma: true)
|
435
|
+
df.write_csv('path/to/file.csv.gz', converters: :numeric, compression: :gzip, convert_comma: true)
|
436
|
+
```
|
437
|
+
|
438
|
+
### Excel Exporter
|
439
|
+
|
440
|
+
[(Go to Table of Contents)](#table-of-contents)
|
441
|
+
|
442
|
+
Exports a **Daru::DataFrame** into a **.xls** file.
|
443
|
+
|
444
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Exporters/Excel)
|
445
|
+
- **Gem Dependencies**: `spreadsheet` gem
|
446
|
+
- **Usage**:
|
447
|
+
```ruby
|
448
|
+
#! Partially require just Excel Exporter
|
449
|
+
require 'daru/io/exporters/excel'
|
450
|
+
|
451
|
+
#! Usage from Daru::IO
|
452
|
+
string = Daru::IO::Exporters::Excel.new(df, header: {color: :red, weight: :bold}, data: {color: :blue }, index: false).to_s
|
453
|
+
Daru::IO::Exporters::Excel.new(df, header: {color: :red, weight: :bold}, data: {color: :blue }, index: false).write('path/to/file.xls')
|
454
|
+
|
455
|
+
#! Usage from Daru::DataFrame
|
456
|
+
string = df.to_excel_string(header: {color: :red, weight: :bold}, data: {color: :blue }, index: false)
|
457
|
+
df.write_excel('path/to/file.xls', header: {color: :red, weight: :bold}, data: {color: :blue }, index: false)
|
458
|
+
```
|
459
|
+
|
460
|
+
### JSON Exporter
|
461
|
+
|
462
|
+
[(Go to Table of Contents)](#table-of-contents)
|
463
|
+
|
464
|
+
Exports a **Daru::DataFrame** into a **.json** file.
|
465
|
+
|
466
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Exporters/JSON)
|
467
|
+
- **Gem Dependencies**: `jsonpath` gem
|
468
|
+
- **Usage**:
|
469
|
+
```ruby
|
470
|
+
#! Partially require just JSON Exporter
|
471
|
+
require 'daru/io/exporters/json'
|
472
|
+
|
473
|
+
#! Usage from Daru::IO
|
474
|
+
hashes = Daru::IO::Exporters::JSON.new(df, orient: :records, pretty: true, name: '$.person.name', age: '$.person.age').to
|
475
|
+
string = Daru::IO::Exporters::JSON.new(df, 'path/to/file.json', orient: :records, pretty: true, name: '$.person.name', age: '$.person.age').to_s
|
476
|
+
Daru::IO::Exporters::JSON.new(df, orient: :records, pretty: true, name: '$.person.name', age: '$.person.age').write('path/to/file.json')
|
477
|
+
|
478
|
+
#! Usage from Daru::DataFrame
|
479
|
+
hashes = df.to_json('orient: :records, pretty: true, name: '$.person.name', age: '$.person.age')
|
480
|
+
string = df.to_json_string(orient: :records, pretty: true, name: '$.person.name', age: '$.person.age')
|
481
|
+
df.write_json('path/to/file.json', orient: :records, pretty: true, name: '$.person.name', age: '$.person.age')
|
482
|
+
```
|
483
|
+
|
484
|
+
### RData Exporter
|
485
|
+
|
486
|
+
[(Go to Table of Contents)](#table-of-contents)
|
487
|
+
|
488
|
+
Exports multiple **Daru::DataFrame**s into a **.rdata** file.
|
489
|
+
|
490
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Exporters/RData)
|
491
|
+
- **Gem Dependencies**: `rsruby` gem
|
492
|
+
- **Other Dependencies**: Install R and set `R_HOME` variable as given in the [Contribution Guidelines](CONTRIBUTING.md)
|
493
|
+
- **Usage**:
|
494
|
+
```ruby
|
495
|
+
#! Partially require just RData Exporter
|
496
|
+
require 'daru/io/exporters/r_data'
|
497
|
+
|
498
|
+
#! Usage from Daru::IO
|
499
|
+
string = Daru::IO::Exporters::RData.new('first.df': df1, 'second.df': df2).to_s
|
500
|
+
Daru::IO::Exporters::RData.new('first.df': df1, 'second.df': df2).write('path/to/file.RData')
|
501
|
+
```
|
502
|
+
|
503
|
+
### RDS Exporter
|
504
|
+
|
505
|
+
[(Go to Table of Contents)](#table-of-contents)
|
506
|
+
|
507
|
+
Exports a **Daru::DataFrame** into a **.rds** file.
|
508
|
+
|
509
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Exporters/RDS)
|
510
|
+
- **Gem Dependencies**: `rsruby` gem
|
511
|
+
- **Other Dependencies**: Install R and set `R_HOME` variable as given in the [Contribution Guidelines](CONTRIBUTING.md)
|
512
|
+
- **Usage**:
|
513
|
+
```ruby
|
514
|
+
#! Partially require just RDS Exporter
|
515
|
+
require 'daru/io/exporters/rds'
|
516
|
+
|
517
|
+
#! Usage from Daru::IO
|
518
|
+
string = Daru::IO::Exporters::RDS.new(df, 'sample.dataframe').to_s
|
519
|
+
Daru::IO::Exporters::RDS.new(df, 'sample.dataframe').write('path/to/file.rds')
|
520
|
+
|
521
|
+
#! Usage from Daru::DataFrame
|
522
|
+
string = df.to_rds_string('sample.dataframe')
|
523
|
+
df.write_rds('path/to/file.rds', 'sample.dataframe')
|
524
|
+
```
|
525
|
+
|
526
|
+
### SQL Exporter
|
527
|
+
|
528
|
+
[(Go to Table of Contents)](#table-of-contents)
|
529
|
+
|
530
|
+
Exports a **Daru::DataFrame** into a database (SQL) table through DBI connection.
|
531
|
+
|
532
|
+
- **Docs**: [rubydoc.info](http://www.rubydoc.info/github/athityakumar/daru-io/master/Daru/IO/Exporters/SQL)
|
533
|
+
- **Gem Dependencies**: `dbd-sqlite3`, `dbi` and `sqlite3` gems
|
534
|
+
- **Other Dependencies**: Install SQL database server
|
535
|
+
- **Usage**:
|
536
|
+
```ruby
|
537
|
+
#! Partially require just SQL Exporter
|
538
|
+
require 'daru/io/exporters/sql'
|
539
|
+
|
540
|
+
#! Usage from Daru::IO
|
541
|
+
Daru::IO::Exporters::SQL.new(df, DBI.connect('DBI:Mysql:database:localhost', 'user', 'password'), 'cars_table').to
|
542
|
+
|
543
|
+
#! Usage from Daru::DataFrame
|
544
|
+
df.to_sql(DBI.connect('DBI:Mysql:database:localhost', 'user', 'password'), 'cars_table')
|
545
|
+
```
|
546
|
+
|
547
|
+
# Creating your own IO modules
|
548
|
+
|
549
|
+
**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.
|
550
|
+
|
551
|
+
- **Adding new IO modules to Daru-IO**
|
552
|
+
|
553
|
+
Say, your unique use-case is of YAML IO Modules. Here's how you can proceed with tweaking -
|
554
|
+
|
555
|
+
```ruby
|
556
|
+
#! YAML Importer
|
557
|
+
|
558
|
+
require 'daru/io'
|
559
|
+
|
560
|
+
class Daru::IO::Importers::YAML < Daru::IO::Importers::Base
|
561
|
+
Daru::DataFrame.register_io_module :from_yaml, self
|
562
|
+
Daru::DataFrame.register_io_module :read_yaml, self
|
563
|
+
|
564
|
+
def initialize
|
565
|
+
optional_gem 'yaml'
|
566
|
+
#! Add all required gem(s) here.
|
567
|
+
end
|
568
|
+
|
569
|
+
def from(instance)
|
570
|
+
#! Your code to create initialize instance
|
571
|
+
self
|
572
|
+
end
|
573
|
+
|
574
|
+
def read(path)
|
575
|
+
#! Your code to read the YAML file
|
576
|
+
#! and create Daru::DataFrame
|
577
|
+
self
|
578
|
+
end
|
579
|
+
|
580
|
+
def call(opts)
|
581
|
+
#! Unified code to create Daru::DataFrame
|
582
|
+
#! irrespective of which method
|
583
|
+
#! (from / read) is used by user
|
584
|
+
end
|
585
|
+
end
|
586
|
+
|
587
|
+
df = Daru::DataFrame.read_yaml('path/to/file.yaml', skip: 10)
|
588
|
+
# or,
|
589
|
+
df = Daru::IO::Importers::YAML.read('path/to/file.yaml').call(skip: 10)
|
590
|
+
```
|
591
|
+
|
592
|
+
```ruby
|
593
|
+
#! YAML Exporter
|
594
|
+
|
595
|
+
require 'daru/io'
|
596
|
+
|
597
|
+
class Daru::IO::Exporters::YAML < Daru::IO::Exporters::Base
|
598
|
+
Daru::DataFrame.register_io_module :to_yaml, self
|
599
|
+
Daru::DataFrame.register_io_module :to_yaml_string, self
|
600
|
+
Daru::DataFrame.register_io_module :write_yaml, self
|
601
|
+
|
602
|
+
def initialize(dataframe, opts)
|
603
|
+
super(dataframe) #! Have a look at documentation of Daru::IO::Exporters::Base#initialize
|
604
|
+
@opts = opts
|
605
|
+
end
|
606
|
+
|
607
|
+
def to
|
608
|
+
#! Your code to return a YAML instance
|
609
|
+
end
|
610
|
+
|
611
|
+
def to_s
|
612
|
+
super
|
613
|
+
#! By default, Exporters::Base adds this to_s method to all Exporters,
|
614
|
+
#! by making the write mthod to write to a tempfile, and then reading it.
|
615
|
+
end
|
616
|
+
|
617
|
+
def write(path)
|
618
|
+
#! Your code to write the YAML file
|
619
|
+
#! with the data in the @dataframe
|
620
|
+
end
|
621
|
+
end
|
622
|
+
|
623
|
+
df = Daru::DataFrame.new(x: [1,2], y: [3,4])
|
624
|
+
|
625
|
+
df.to_yaml(rows: 10..19) #! or, Daru::IO::Exporters::YAML.new(df, rows: 10..19).to
|
626
|
+
df.to_yaml_string(rows: 10..19) #! or, Daru::IO::Exporters::YAML.new(df, rows: 10..19).to_s
|
627
|
+
df.write_yaml('dataframe.yml', rows: 10..19) #! or, Daru::IO::Exporters::YAML.new(df, rows: 10..19).write('dataframe.yml')
|
628
|
+
```
|
629
|
+
|
630
|
+
- **Adding new IO modules to custom modules**
|
631
|
+
|
632
|
+
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 -
|
633
|
+
|
634
|
+
```ruby
|
635
|
+
class CustomNamespace::Importers::CSV < Daru::IO::Importers::CSV
|
636
|
+
Daru::DataFrame.register_io_module :custom_csv, self
|
637
|
+
|
638
|
+
#! Your CSV Importer code here
|
639
|
+
end
|
640
|
+
```
|
641
|
+
|
642
|
+
**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).**
|
643
|
+
|
644
|
+
# Contributing
|
645
|
+
|
646
|
+
[(Go to Table of Contents)](#table-of-contents)
|
647
|
+
|
648
|
+
Contributions are always welcome. But, please have a look at the [contribution guidelines](CONTRIBUTING.md) first before contributing. :tada:
|
649
|
+
|
650
|
+
# License
|
651
|
+
|
652
|
+
[(Go to Table of Contents)](#table-of-contents)
|
653
|
+
|
654
|
+
The MIT License (MIT) 2017 - [Athitya Kumar](https://github.com/athityakumar/) and [Ruby Science Foundation](https://github.com/SciRuby/). Please have a look at the [LICENSE.md](LICENSE.md) for more details.
|