Indexing in Daru has grown more powerful. Here's a glimpse. Enjoy!
require 'daru'
true
A new class of index got added to Daru to handle indexing which can be categorical. This is basically how it works.
idx = Daru::CategoricalIndex.new [:a, 1, :a, 1, :c]
#<Daru::CategoricalIndex(5): {a, 1, a, 1, c}>
dv = Daru::Vector.new 'a'..'e', index: idx
Daru::Vector(5) | |
---|---|
a | a |
1 | b |
a | c |
1 | d |
c | e |
Get elements with single category.
dv[:a]
Daru::Vector(2) | |
---|---|
a | a |
a | c |
Retrive elements by multiple categories.
dv[:a, 1]
Daru::Vector(4) | |
---|---|
a | a |
1 | b |
a | c |
1 | d |
Get elements by position using #at
.
dv.at 1
"b"
You can get multiple element as a vector if you specify more than one position.
dv.at 0, 1, 2
Daru::Vector(3) | |
---|---|
a | a |
1 | b |
a | c |
Note: You can also use #[]
to get elements by position only if that position is not a index.
Get elements by single position.
dv[3]
"d"
Get elements by multiple positions.
dv[0, 3]
Daru::Vector(2) | |
---|---|
a | a |
1 | d |
Change values at certain indexes using []=
dv[:a] = 20
dv
Daru::Vector(5) | |
---|---|
a | 20 |
1 | b |
a | 20 |
1 | d |
c | e |
Change values at certain positions using at_set
dv.at_set [0, 1], 100
dv
Daru::Vector(5) | |
---|---|
a | 100 |
1 | 100 |
a | 20 |
1 | d |
c | e |
Overall all Index classes API are improved. Everyclass respond to multiple indexes, positional index, give proper error messages, etc.
Here are few examples:
idx = Daru::Index.new [:a, :b, :c, :d]
#<Daru::Index(4): {a, b, c, d}>
dv = Daru::Vector.new 1..4, index: idx
Daru::Vector(4) | |
---|---|
a | 1 |
b | 2 |
c | 3 |
d | 4 |
Retrive values by index or positions.
Note: When the value is both a valid index and a valid position, it will be treated as index value.
dv[:a, :b]
Daru::Vector(2) | |
---|---|
a | 1 |
b | 2 |
Retive values by positions.
dv.at 0, 1
Daru::Vector(2) | |
---|---|
a | 1 |
b | 2 |
Set values by index or positions.
Note: When value is both a valid index and a valid position, it will be treated as index value.
dv[:a, :b] = 'x'
dv
Daru::Vector(4) | |
---|---|
a | x |
b | x |
c | 3 |
d | 4 |
Set values in vector by positions.
dv.at_set [0, 2], 'y'
dv
Daru::Vector(4) | |
---|---|
a | y |
b | x |
c | y |
d | 4 |
idx = Daru::MultiIndex.from_tuples [
[:a,:one,:bar],
[:a,:one,:baz],
[:a,:two,:bat],
[:b,:one,:bar],
[:b,:two,:baz],
]
Daru::MultiIndex(5x3) | ||
---|---|---|
a | one | bar |
baz | ||
two | bat | |
b | one | bar |
two | baz |
dv = Daru::Vector.new 1..5, index: idx
Daru::Vector(5) | |||
---|---|---|---|
a | one | bar | 1 |
baz | 2 | ||
two | bat | 3 | |
b | one | bar | 4 |
two | baz | 5 |
Retrive values by index or positions.
Note: When value is both a valid index or position then value will be treated as index rather position.
Partial Index
dv[:a]
Daru::Vector(3) | ||
---|---|---|
one | bar | 1 |
baz | 2 | |
two | bat | 3 |
On mentioning complete index, one will get the value at that index
dv[:a, :one, :bar]
1
Retrive values by positions
dv.at 0, 1, 2
Daru::Vector(3) | |||
---|---|---|---|
a | one | bar | 1 |
baz | 2 | ||
two | bat | 3 |
Assign values by index or positions
dv[:a, :one] = 'x'
dv
Daru::Vector(5) | |||
---|---|---|---|
a | one | bar | x |
baz | x | ||
two | bat | 3 | |
b | one | bar | 4 |
two | baz | 5 |
Assign values by positions
dv.at_set [0, 1, 2], 'z'
dv
Daru::Vector(5) | |||
---|---|---|---|
a | one | bar | z |
baz | z | ||
two | bat | z | |
b | one | bar | 4 |
two | baz | 5 |