daru_lite 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.md +30 -0
- data/README.md +1 -1
- data/lib/daru_lite/data_frame/convertible.rb +0 -33
- data/lib/daru_lite/data_frame/fetchable.rb +1 -1
- data/lib/daru_lite/data_frame/i_o_able.rb +0 -102
- data/lib/daru_lite/data_frame/indexable.rb +24 -0
- data/lib/daru_lite/data_frame/relabelable.rb +125 -0
- data/lib/daru_lite/data_frame/sortable.rb +27 -1
- data/lib/daru_lite/dataframe.rb +3 -4
- data/lib/daru_lite/index/categorical_index.rb +8 -0
- data/lib/daru_lite/index/index.rb +0 -1
- data/lib/daru_lite/index/multi_index.rb +8 -5
- data/lib/daru_lite/io/io.rb +0 -195
- data/lib/daru_lite/maths/arithmetic/vector.rb +31 -0
- data/lib/daru_lite/maths/statistics/vector/descriptive.rb +134 -0
- data/lib/daru_lite/maths/statistics/vector/dispersion.rb +120 -0
- data/lib/daru_lite/maths/statistics/vector/extrema.rb +217 -0
- data/lib/daru_lite/maths/statistics/vector/normalization.rb +86 -0
- data/lib/daru_lite/maths/statistics/vector/percentile.rb +64 -0
- data/lib/daru_lite/maths/statistics/vector/sampling.rb +56 -0
- data/lib/daru_lite/maths/statistics/vector/time_series.rb +350 -0
- data/lib/daru_lite/maths/statistics/vector.rb +16 -994
- data/lib/daru_lite/vector/convertible.rb +0 -32
- data/lib/daru_lite/vector.rb +0 -9
- data/lib/daru_lite/version.rb +1 -1
- data/lib/daru_lite.rb +0 -30
- metadata +9 -75
- data/lib/daru_lite/date_time/index.rb +0 -570
- data/lib/daru_lite/date_time/offsets.rb +0 -397
- data/lib/daru_lite/extensions/which_dsl.rb +0 -53
- data/lib/daru_lite/io/sql_data_source.rb +0 -80
- data/lib/daru_lite/iruby/helpers.rb +0 -38
- data/lib/daru_lite/iruby/templates/dataframe.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/dataframe_mi.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/dataframe_mi_tbody.html.erb +0 -35
- data/lib/daru_lite/iruby/templates/dataframe_mi_thead.html.erb +0 -21
- data/lib/daru_lite/iruby/templates/dataframe_tbody.html.erb +0 -28
- data/lib/daru_lite/iruby/templates/dataframe_thead.html.erb +0 -21
- data/lib/daru_lite/iruby/templates/multi_index.html.erb +0 -12
- data/lib/daru_lite/iruby/templates/vector.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/vector_mi.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/vector_mi_tbody.html.erb +0 -26
- data/lib/daru_lite/iruby/templates/vector_mi_thead.html.erb +0 -8
- data/lib/daru_lite/iruby/templates/vector_tbody.html.erb +0 -17
- data/lib/daru_lite/iruby/templates/vector_thead.html.erb +0 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 87c462e411cd87585713c87579242f4cbae79886537117c5d76588ce5e3ac25f
|
|
4
|
+
data.tar.gz: e176f0f6032278dd572c3890de25484c547202b60cad0866de8d8692ca1847f2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 476a7ba85cae27d3ff1d6eb9dc8659ae4d90f33de70adc3b404acaabaf83efb4116b491fb1166c3173d8c799cb420c5a4f46b15dc72049c41bbbd33abe005cde
|
|
7
|
+
data.tar.gz: a08250b7baa3d81434874e6f38039953f9c6c14cb9076937108630fa9f3ad8e9a45e9f434d460e7698fcde1d6601b9f0cffbf1f0dc3599019437a701a8ad0753
|
data/History.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
+
# 0.5.0
|
|
2
|
+
* [Breaking change] Removed `DateTimeIndex`, the date offsets and the `DaruLite::DAYS_OF_WEEK` and
|
|
3
|
+
`DaruLite::MONTH_DAYS` constants. `Index.new` given `DateTime` objects now returns a plain `Index`
|
|
4
|
+
* [Breaking change] Removed the Excel, SQL, ActiveRecord and plain text readers and writers
|
|
5
|
+
(`from_excel`, `write_excel`, `from_sql`, `write_sql`, `from_activerecord`, `from_plaintext`)
|
|
6
|
+
and `DaruLite::IOHelpers.process_row`. CSV reading and writing is unchanged
|
|
7
|
+
* [Breaking change] Removed `DataFrame#save`, `Vector#save` and `DaruLite::IO.load`.
|
|
8
|
+
`Marshal.dump` and `Marshal.load` still work on dataframes, vectors and indexes
|
|
9
|
+
* [Breaking change] Removed `#to_html` on `DataFrame`, `Vector` and `MultiIndex` and the IRuby templates
|
|
10
|
+
* [Breaking change] Removed the `which` DSL extension, which was never loaded by default
|
|
11
|
+
* [Breaking change] `Vector#acvf`'s `unbiased` argument was inverted and now means what it says: `true` divides
|
|
12
|
+
lag k by n - k, `false` by n. Its default becomes `false`, so `acvf` without that argument returns the same values.
|
|
13
|
+
Swap the value wherever it is passed explicitly
|
|
14
|
+
* Fixed `DataFrame#inspect` and `DataFrame#rotate_vectors` raising `NoMethodError` when ActiveSupport is not loaded
|
|
15
|
+
* Fixed `Marshal.load` raising `NoMethodError` on a `MultiIndex`, a `CategoricalIndex`, and any `Vector` or `DataFrame`
|
|
16
|
+
built on one
|
|
17
|
+
* Fixed `DataFrame#inspect` raising `ArgumentError` on a DataFrame without vectors, and truncating
|
|
18
|
+
`MultiIndex` vector names
|
|
19
|
+
* Fixed `Vector#proportion` for a value absent from the vector and for Integer/Float mismatches
|
|
20
|
+
* Fixed `Vector#percentile(100)` with the `:midpoint` strategy raising `TypeError`
|
|
21
|
+
* Fixed `Vector#skew`, `#kurtosis` and `#sum_of_squared_deviation` raising on vectors with missing values
|
|
22
|
+
* Fixed `Vector#index_of_max`, `#index_of_min` and their `_by` variants repeating an index for tied values
|
|
23
|
+
* Fixed `Vector#ema`, `#emv`, `#emsd` and `#macd` on vectors with `nil`, shorter than the window or with a custom index.
|
|
24
|
+
A `nil` in the first window is now ignored instead of counting as 0
|
|
25
|
+
* Fixed `Vector#acvf` raising on short vectors and subtracting the mean when `demean` is false
|
|
26
|
+
* Added `DataFrame#reorder_rows`, which moves rows to given positions while keeping the index order
|
|
27
|
+
* Added `DataFrame#add_missing_vectors`, which adds the vectors a dataframe lacks, filled with `nil`
|
|
28
|
+
* Added `DataFrame#symbolize_vectors!`, `DataFrame#rename_blank_vectors!` and `DataFrame#rename_blank_rows!`
|
|
29
|
+
* Added `Vector#divide_or_zero`, a division returning `0.0` instead of failing when the divisor is zero or `nil`
|
|
30
|
+
|
|
1
31
|
# 0.4.0
|
|
2
32
|
* [Breaking change] Removed the methods deprecated since 2016:
|
|
3
33
|
`Vector#has_missing_data?`, `#flawed?`, `#n_valid`, `#only_valid`, `#only_missing`, `#missing_positions` and `#freqs`,
|
data/README.md
CHANGED
|
@@ -119,7 +119,7 @@ data_frame.where(
|
|
|
119
119
|
|
|
120
120
|
A few methods are not meant to receive untrusted input:
|
|
121
121
|
- `DataFrame#compute` with a String evaluates it as Ruby code. This form is deprecated, pass a block instead.
|
|
122
|
-
- `
|
|
122
|
+
- `Marshal.load` on a dumped `DataFrame`, `Vector` or `Index` can execute arbitrary code when the data is forged, like any use of `Marshal.load`.
|
|
123
123
|
- `DataFrame.from_csv` opens any local path or HTTP(S) URL it is given.
|
|
124
124
|
|
|
125
125
|
## Deprecations
|
|
@@ -66,39 +66,6 @@ module DaruLite
|
|
|
66
66
|
.map { |vec_name, idx| [vec_name, @data[idx]] }.to_h
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
# Convert to html for IRuby.
|
|
70
|
-
def to_html(threshold = DaruLite.max_rows)
|
|
71
|
-
table_thead = to_html_thead
|
|
72
|
-
table_tbody = to_html_tbody(threshold)
|
|
73
|
-
path = if index.is_a?(MultiIndex)
|
|
74
|
-
File.expand_path('../iruby/templates/dataframe_mi.html.erb', __dir__)
|
|
75
|
-
else
|
|
76
|
-
File.expand_path('../iruby/templates/dataframe.html.erb', __dir__)
|
|
77
|
-
end
|
|
78
|
-
ERB.new(File.read(path).strip).result(binding)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def to_html_thead
|
|
82
|
-
table_thead_path =
|
|
83
|
-
if index.is_a?(MultiIndex)
|
|
84
|
-
File.expand_path('../iruby/templates/dataframe_mi_thead.html.erb', __dir__)
|
|
85
|
-
else
|
|
86
|
-
File.expand_path('../iruby/templates/dataframe_thead.html.erb', __dir__)
|
|
87
|
-
end
|
|
88
|
-
ERB.new(File.read(table_thead_path).strip).result(binding)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def to_html_tbody(threshold = DaruLite.max_rows)
|
|
92
|
-
threshold ||= @size
|
|
93
|
-
table_tbody_path =
|
|
94
|
-
if index.is_a?(MultiIndex)
|
|
95
|
-
File.expand_path('../iruby/templates/dataframe_mi_tbody.html.erb', __dir__)
|
|
96
|
-
else
|
|
97
|
-
File.expand_path('../iruby/templates/dataframe_tbody.html.erb', __dir__)
|
|
98
|
-
end
|
|
99
|
-
ERB.new(File.read(table_tbody_path).strip).result(binding)
|
|
100
|
-
end
|
|
101
|
-
|
|
102
69
|
def to_s
|
|
103
70
|
"#<#{self.class}#{": #{@name}" if @name}(#{nrows}x#{ncols})>"
|
|
104
71
|
end
|
|
@@ -236,7 +236,7 @@ module DaruLite
|
|
|
236
236
|
def access_vector_single_index(*names)
|
|
237
237
|
if names.count < 2
|
|
238
238
|
begin
|
|
239
|
-
pos = @vectors.
|
|
239
|
+
pos = @vectors.pos(names.first)
|
|
240
240
|
rescue IndexError
|
|
241
241
|
raise IndexError, "Specified vector #{names.first} does not exist"
|
|
242
242
|
end
|
|
@@ -36,77 +36,6 @@ module DaruLite
|
|
|
36
36
|
DaruLite::IO.from_csv(path, opts, &)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
# Read data from an Excel file into a DataFrame.
|
|
40
|
-
#
|
|
41
|
-
# == Arguments
|
|
42
|
-
#
|
|
43
|
-
# * path - Path of the file to be read.
|
|
44
|
-
#
|
|
45
|
-
# == Options
|
|
46
|
-
#
|
|
47
|
-
# *:worksheet_id - ID of the worksheet that is to be read.
|
|
48
|
-
def from_excel(path, opts = {}, &)
|
|
49
|
-
DaruLite::IO.from_excel(path, opts, &)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# Read a database query and returns a Dataset
|
|
53
|
-
#
|
|
54
|
-
# @param arh [ActiveRecord::ConnectionAdapters::AbstractAdapter, String] An ActiveRecord connection
|
|
55
|
-
# OR Path to a SQlite3 database.
|
|
56
|
-
# @param query [String] The query to be executed
|
|
57
|
-
#
|
|
58
|
-
# @return A dataframe containing the data resulting from the query
|
|
59
|
-
#
|
|
60
|
-
# USE:
|
|
61
|
-
#
|
|
62
|
-
# ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: "path/to/sqlite.db")
|
|
63
|
-
# arh = ActiveRecord::Base.connection
|
|
64
|
-
# DaruLite::DataFrame.from_sql(dbh, "SELECT * FROM test")
|
|
65
|
-
#
|
|
66
|
-
# #Alternatively
|
|
67
|
-
#
|
|
68
|
-
# require 'active_record'
|
|
69
|
-
# DaruLite::DataFrame.from_sql("path/to/sqlite.db", "SELECT * FROM test")
|
|
70
|
-
def from_sql(arh, query)
|
|
71
|
-
DaruLite::IO.from_sql arh, query
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
# Read a dataframe from AR::Relation
|
|
75
|
-
#
|
|
76
|
-
# @param relation [ActiveRecord::Relation] An AR::Relation object from which data is loaded
|
|
77
|
-
# @param fields [Array] Field names to be loaded (optional)
|
|
78
|
-
#
|
|
79
|
-
# @return A dataframe containing the data loaded from the relation
|
|
80
|
-
#
|
|
81
|
-
# USE:
|
|
82
|
-
#
|
|
83
|
-
# # When Post model is defined as:
|
|
84
|
-
# class Post < ActiveRecord::Base
|
|
85
|
-
# scope :active, -> { where.not(published_at: nil) }
|
|
86
|
-
# end
|
|
87
|
-
#
|
|
88
|
-
# # You can load active posts into a dataframe by:
|
|
89
|
-
# DaruLite::DataFrame.from_activerecord(Post.active, :title, :published_at)
|
|
90
|
-
def from_activerecord(relation, *fields)
|
|
91
|
-
DaruLite::IO.from_activerecord relation, *fields
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
# Read the database from a plaintext file. For this method to work,
|
|
95
|
-
# the data should be present in a plain text file in columns. See
|
|
96
|
-
# spec/fixtures/bank2.dat for an example.
|
|
97
|
-
#
|
|
98
|
-
# == Arguments
|
|
99
|
-
#
|
|
100
|
-
# * path - Path of the file to be read.
|
|
101
|
-
# * fields - Vector names of the resulting database.
|
|
102
|
-
#
|
|
103
|
-
# == Usage
|
|
104
|
-
#
|
|
105
|
-
# df = DaruLite::DataFrame.from_plaintext 'spec/fixtures/bank2.dat', [:v1,:v2,:v3,:v4,:v5,:v6]
|
|
106
|
-
def from_plaintext(path, fields)
|
|
107
|
-
DaruLite::IO.from_plaintext path, fields
|
|
108
|
-
end
|
|
109
|
-
|
|
110
39
|
def _load(data)
|
|
111
40
|
h = Marshal.load data
|
|
112
41
|
DaruLite::DataFrame.new(
|
|
@@ -138,37 +67,6 @@ module DaruLite
|
|
|
138
67
|
DaruLite::IO.dataframe_write_csv self, filename, opts
|
|
139
68
|
end
|
|
140
69
|
|
|
141
|
-
# Write this dataframe to an Excel Spreadsheet
|
|
142
|
-
#
|
|
143
|
-
# == Arguments
|
|
144
|
-
#
|
|
145
|
-
# * filename - The path of the file where the DataFrame should be written.
|
|
146
|
-
def write_excel(filename, opts = {})
|
|
147
|
-
DaruLite::IO.dataframe_write_excel self, filename, opts
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
# Insert each case of the Dataset on the selected table
|
|
151
|
-
#
|
|
152
|
-
# == Arguments
|
|
153
|
-
#
|
|
154
|
-
# * arh - ActiveRecord database connection object.
|
|
155
|
-
# * query - Query string.
|
|
156
|
-
#
|
|
157
|
-
# == Usage
|
|
158
|
-
#
|
|
159
|
-
# ds = DaruLite::DataFrame.new({:id=>DaruLite::Vector.new([1,2,3]), :name=>DaruLite::Vector.new(["a","b","c"])})
|
|
160
|
-
# ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: "path/to/sqlite.db")
|
|
161
|
-
# arh = ActiveRecord::Base.connection
|
|
162
|
-
# ds.write_sql(arh,"test")
|
|
163
|
-
def write_sql(arh, table)
|
|
164
|
-
DaruLite::IO.dataframe_write_sql self, arh, table
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
# Use marshalling to save dataframe to a file.
|
|
168
|
-
def save(filename)
|
|
169
|
-
DaruLite::IO.save self, filename
|
|
170
|
-
end
|
|
171
|
-
|
|
172
70
|
def _dump(_depth)
|
|
173
71
|
Marshal.dump(
|
|
174
72
|
data: @data,
|
|
@@ -139,6 +139,30 @@ module DaruLite
|
|
|
139
139
|
end
|
|
140
140
|
end
|
|
141
141
|
|
|
142
|
+
# Adds the vectors of +names+ that the dataframe does not have yet, filled
|
|
143
|
+
# with nil. Existing vectors keep their position, missing ones are appended
|
|
144
|
+
# in the order given.
|
|
145
|
+
#
|
|
146
|
+
# @param names [Array, DaruLite::Index] vector names the result must contain
|
|
147
|
+
# @return [DaruLite::DataFrame] a new dataframe
|
|
148
|
+
# @example
|
|
149
|
+
# df = DaruLite::DataFrame.new({ a: [1, 2], b: [3, 4] })
|
|
150
|
+
# df.add_missing_vectors([:b, :c]).vectors.to_a #=> [:a, :b, :c]
|
|
151
|
+
def add_missing_vectors(names)
|
|
152
|
+
missing = names.to_a.uniq - @vectors.to_a
|
|
153
|
+
return dup if missing.empty?
|
|
154
|
+
|
|
155
|
+
# Built by position: a MultiIndex may repeat a tuple, which cannot be looked up by name.
|
|
156
|
+
all_names = @vectors.to_a + missing
|
|
157
|
+
all_vectors = @vectors.is_a?(MultiIndex) ? MultiIndex.from_tuples(all_names) : DaruLite::Index.new(all_names)
|
|
158
|
+
all_vectors.name = @vectors.name if @vectors.name
|
|
159
|
+
added = missing.map { |name| DaruLite::Vector.new(Array.new(nrows), index: @index.dup, name: name) }
|
|
160
|
+
|
|
161
|
+
DaruLite::DataFrame.new(
|
|
162
|
+
@data.map(&:dup) + added, order: all_vectors, index: @index.dup, name: @name, clone: true
|
|
163
|
+
)
|
|
164
|
+
end
|
|
165
|
+
|
|
142
166
|
# Reassign vectors with a new index of type DaruLite::Index or any of its subclasses.
|
|
143
167
|
#
|
|
144
168
|
# @param new_index [DaruLite::Index] idx The new index object on which the vectors are to
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
module DaruLite
|
|
2
|
+
class DataFrame
|
|
3
|
+
module Relabelable
|
|
4
|
+
# Converts every String vector name to a Symbol, in place. Other names are
|
|
5
|
+
# left as they are. With MultiIndex vectors, every level of each name is
|
|
6
|
+
# converted.
|
|
7
|
+
#
|
|
8
|
+
# @raise [ArgumentError] when the conversion gives two vectors the same name
|
|
9
|
+
# @return [DaruLite::DataFrame] self
|
|
10
|
+
# @example
|
|
11
|
+
# df = DaruLite::DataFrame.new({ 'a' => [1, 2], 'b' => [3, 4] })
|
|
12
|
+
# df.symbolize_vectors!
|
|
13
|
+
# df.vectors.to_a #=> [:a, :b]
|
|
14
|
+
def symbolize_vectors!
|
|
15
|
+
multi_index = vectors.is_a?(DaruLite::MultiIndex)
|
|
16
|
+
names = vectors.to_a.map do |name|
|
|
17
|
+
multi_index ? name.map { |level| symbolize_label(level) } : symbolize_label(name)
|
|
18
|
+
end
|
|
19
|
+
ensure_unique_labels!(names) if names.uniq.size < vectors.to_a.uniq.size
|
|
20
|
+
|
|
21
|
+
symbolized = multi_index ? DaruLite::MultiIndex.from_tuples(names) : DaruLite::Index.new(names)
|
|
22
|
+
symbolized.name = vectors.name if vectors.name
|
|
23
|
+
self.vectors = symbolized
|
|
24
|
+
|
|
25
|
+
self
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Renames, in place, every vector whose name is blank (nil, false, empty
|
|
29
|
+
# or made of whitespace only).
|
|
30
|
+
#
|
|
31
|
+
# The first one is named +label+ and the following ones "label (1)",
|
|
32
|
+
# "label (2)"... skipping the names already taken by another vector, so
|
|
33
|
+
# that no vector is lost. Vectors that are not blank keep their name and
|
|
34
|
+
# every vector keeps its position.
|
|
35
|
+
#
|
|
36
|
+
# @param label [String, Symbol] the name given to blank vectors, not blank itself
|
|
37
|
+
# @raise [ArgumentError] when +label+ is blank
|
|
38
|
+
# @raise [NotImplementedError] with MultiIndex vectors
|
|
39
|
+
# @return [DaruLite::DataFrame] self
|
|
40
|
+
# @example
|
|
41
|
+
# df = DaruLite::DataFrame.new({ nil => [1, 2], b: [3, 4], '' => [5, 6] })
|
|
42
|
+
# df.rename_blank_vectors!(:none)
|
|
43
|
+
# df.vectors.to_a #=> [:none, :b, :"none (1)"]
|
|
44
|
+
def rename_blank_vectors!(label)
|
|
45
|
+
raise ArgumentError, 'label must not be blank' if blank_label?(label)
|
|
46
|
+
raise NotImplementedError, 'Cannot rename blank vectors under a MultiIndex' if vectors.is_a?(MultiIndex)
|
|
47
|
+
|
|
48
|
+
names = replace_blank_labels(vectors.to_a, label)
|
|
49
|
+
self.vectors = DaruLite::Index.new(names, name: vectors.name) unless names == vectors.to_a
|
|
50
|
+
|
|
51
|
+
self
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Renames, in place, every row whose index label is blank (nil, false,
|
|
55
|
+
# empty or made of whitespace only).
|
|
56
|
+
#
|
|
57
|
+
# The first one is named +label+ and the following ones "label (1)",
|
|
58
|
+
# "label (2)"... skipping the labels already taken by another row, so
|
|
59
|
+
# that index labels stay unique. Rows that are not blank keep their
|
|
60
|
+
# label, including one already named +label+. The index becomes a plain
|
|
61
|
+
# DaruLite::Index; nothing changes when no label is blank.
|
|
62
|
+
#
|
|
63
|
+
# @param label [String] the label given to blank rows, not blank itself
|
|
64
|
+
# @raise [ArgumentError] when +label+ is blank, or when the other labels
|
|
65
|
+
# are not unique, as a CategoricalIndex allows
|
|
66
|
+
# @return [DaruLite::DataFrame] self
|
|
67
|
+
# @example
|
|
68
|
+
# df = DaruLite::DataFrame.new({ a: [1, 2, 3] }, index: [nil, '', 'x'])
|
|
69
|
+
# df.rename_blank_rows!('none')
|
|
70
|
+
# df.index.to_a #=> ['none', 'none (1)', 'x']
|
|
71
|
+
def rename_blank_rows!(label)
|
|
72
|
+
raise ArgumentError, 'label must not be blank' if blank_label?(label)
|
|
73
|
+
|
|
74
|
+
labels = replace_blank_labels(index.to_a, label)
|
|
75
|
+
return self if labels == index.to_a
|
|
76
|
+
|
|
77
|
+
ensure_unique_labels!(labels)
|
|
78
|
+
|
|
79
|
+
self.index = DaruLite::Index.new(labels, name: index.name)
|
|
80
|
+
|
|
81
|
+
self
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def replace_blank_labels(labels, label)
|
|
87
|
+
blank, kept = labels.partition { |candidate| blank_label?(candidate) }
|
|
88
|
+
replacements = numbered_labels(label, blank.size, kept.to_set).each
|
|
89
|
+
|
|
90
|
+
labels.map { |candidate| blank_label?(candidate) ? replacements.next : candidate }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# +label+, then "label (1)", "label (2)"... leaving out those in +taken+.
|
|
94
|
+
# A Symbol label gives Symbols.
|
|
95
|
+
def numbered_labels(label, size, taken)
|
|
96
|
+
candidates = Enumerator.produce(0) { |count| count + 1 }.lazy.map do |count|
|
|
97
|
+
next label if count.zero?
|
|
98
|
+
|
|
99
|
+
numbered = "#{label} (#{count})"
|
|
100
|
+
label.is_a?(Symbol) ? numbered.to_sym : numbered
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
candidates.reject { |candidate| taken.include?(candidate) }.first(size)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def symbolize_label(label)
|
|
107
|
+
label.is_a?(String) ? label.to_sym : label
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def ensure_unique_labels!(labels)
|
|
111
|
+
duplicates = labels.tally.select { |_, count| count > 1 }.keys
|
|
112
|
+
return if duplicates.empty?
|
|
113
|
+
|
|
114
|
+
raise ArgumentError, "Labels must be unique, got duplicates: #{duplicates.map(&:inspect).join(', ')}"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def blank_label?(label)
|
|
118
|
+
return true if label.nil? || label == false
|
|
119
|
+
return label.to_s.match?(/\A[[:space:]]*\z/) if label.is_a?(String) || label.is_a?(Symbol)
|
|
120
|
+
|
|
121
|
+
label.respond_to?(:empty?) && label.empty?
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -21,6 +21,32 @@ module DaruLite
|
|
|
21
21
|
initialize(to_h, order: order_array)
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
+
# Returns a dataframe whose rows are those found at +positions+, in that
|
|
25
|
+
# order. Only the values move: the index keeps its original order. Use
|
|
26
|
+
# #row_at to move the index labels along with their rows.
|
|
27
|
+
#
|
|
28
|
+
# @param positions [Array<Integer>] every row position, once, in the wanted order
|
|
29
|
+
# @return [DaruLite::DataFrame]
|
|
30
|
+
# @example
|
|
31
|
+
# df = DaruLite::DataFrame.new({ a: [1, 2, 3], b: %w[x y z] })
|
|
32
|
+
# df.reorder_rows([2, 0, 1])
|
|
33
|
+
# # => #<DaruLite::DataFrame(3x2)>
|
|
34
|
+
# # a b
|
|
35
|
+
# # 0 3 z
|
|
36
|
+
# # 1 1 x
|
|
37
|
+
# # 2 2 y
|
|
38
|
+
def reorder_rows(positions)
|
|
39
|
+
raise NotImplementedError, 'Cannot reorder rows under a CategoricalIndex' if index.is_a?(CategoricalIndex)
|
|
40
|
+
raise ArgumentError, 'Invalid order' unless positions.sort == (0...nrows).to_a
|
|
41
|
+
|
|
42
|
+
dup.tap do |reordered|
|
|
43
|
+
# Vector#reorder! also reorders the vector's index: give it a cheap one to work on.
|
|
44
|
+
reordered.index = DaruLite::Index.new(nrows)
|
|
45
|
+
reordered.data.each { |vector| vector.reorder!(positions) }
|
|
46
|
+
reordered.index = index.dup
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
24
50
|
# Return the dataframe with rotate vectors positions, the vector at position count is now
|
|
25
51
|
# the first vector of the dataframe.
|
|
26
52
|
# If only one vector in the dataframe, the dataframe is return without any change.
|
|
@@ -39,7 +65,7 @@ module DaruLite
|
|
|
39
65
|
# # 1 7 5 2
|
|
40
66
|
# # 2 9 6 3
|
|
41
67
|
def rotate_vectors(count = -1)
|
|
42
|
-
return self unless vectors.
|
|
68
|
+
return self unless vectors.size > 1
|
|
43
69
|
|
|
44
70
|
self.order = vectors.to_a.rotate(count)
|
|
45
71
|
self
|
data/lib/daru_lite/dataframe.rb
CHANGED
|
@@ -11,6 +11,7 @@ require 'daru_lite/data_frame/iterable'
|
|
|
11
11
|
require 'daru_lite/data_frame/joinable'
|
|
12
12
|
require 'daru_lite/data_frame/missable'
|
|
13
13
|
require 'daru_lite/data_frame/pivotable'
|
|
14
|
+
require 'daru_lite/data_frame/relabelable'
|
|
14
15
|
require 'daru_lite/data_frame/setable'
|
|
15
16
|
require 'daru_lite/data_frame/sortable'
|
|
16
17
|
require 'daru_lite/data_frame/queryable'
|
|
@@ -32,6 +33,7 @@ module DaruLite
|
|
|
32
33
|
include DaruLite::DataFrame::Joinable
|
|
33
34
|
include DaruLite::DataFrame::Missable
|
|
34
35
|
include DaruLite::DataFrame::Pivotable
|
|
36
|
+
include DaruLite::DataFrame::Relabelable
|
|
35
37
|
include DaruLite::DataFrame::Setable
|
|
36
38
|
include DaruLite::DataFrame::Sortable
|
|
37
39
|
include DaruLite::DataFrame::Queryable
|
|
@@ -451,10 +453,7 @@ module DaruLite
|
|
|
451
453
|
# Pretty print in a nice table format for the command line (irb/pry/iruby)
|
|
452
454
|
def inspect(spacing = DaruLite.spacing, threshold = DaruLite.max_rows)
|
|
453
455
|
name_part = @name ? ": #{@name} " : ''
|
|
454
|
-
spacing =
|
|
455
|
-
headers.to_a.map { |header| header.try(:length) || header.to_s.length }.max,
|
|
456
|
-
spacing
|
|
457
|
-
].max
|
|
456
|
+
spacing = headers.to_a.map { |header| header.to_s.length }.push(spacing).max
|
|
458
457
|
|
|
459
458
|
"#<#{self.class}#{name_part}(#{nrows}x#{ncols})>#{$INPUT_RECORD_SEPARATOR}" +
|
|
460
459
|
Formatters::Table.format(
|
|
@@ -151,6 +151,14 @@ module DaruLite
|
|
|
151
151
|
# b = DaruLite::CategoricalIndex.new [:b, :a, :a]
|
|
152
152
|
# a == b
|
|
153
153
|
# # => false
|
|
154
|
+
def _dump(*)
|
|
155
|
+
Marshal.dump(to_a)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def self._load(data)
|
|
159
|
+
new(Marshal.load(data)) # rubocop:disable Security/MarshalLoad
|
|
160
|
+
end
|
|
161
|
+
|
|
154
162
|
def ==(other)
|
|
155
163
|
self.class == other.class &&
|
|
156
164
|
size == other.size &&
|
|
@@ -356,6 +356,14 @@ module DaruLite
|
|
|
356
356
|
@levels.size
|
|
357
357
|
end
|
|
358
358
|
|
|
359
|
+
def _dump(*)
|
|
360
|
+
Marshal.dump(labels: @labels, levels: levels, name: @name)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def self._load(data)
|
|
364
|
+
new(**Marshal.load(data)) # rubocop:disable Security/MarshalLoad
|
|
365
|
+
end
|
|
366
|
+
|
|
359
367
|
def ==(other)
|
|
360
368
|
self.class == other.class &&
|
|
361
369
|
labels == other.labels &&
|
|
@@ -375,11 +383,6 @@ module DaruLite
|
|
|
375
383
|
Formatters::Table.format([], headers: @name, row_headers: sparse_tuples, threshold: threshold)
|
|
376
384
|
end
|
|
377
385
|
|
|
378
|
-
def to_html
|
|
379
|
-
path = File.expand_path('../iruby/templates/multi_index.html.erb', __dir__)
|
|
380
|
-
ERB.new(File.read(path).strip).result(binding)
|
|
381
|
-
end
|
|
382
|
-
|
|
383
386
|
# Provide a MultiIndex for sub vector produced
|
|
384
387
|
#
|
|
385
388
|
# @param input_indexes [Array] the input by user to index the vector
|