Indexing in Daru has grown more powerful. Here's a glimpse. Enjoy!

In [1]:
require 'daru'
Out[1]:
true

Categorical Index

A new class of index got added to Daru to handle indexing which can be categorical. This is basically how it works.

In [2]:
idx = Daru::CategoricalIndex.new [:a, 1, :a, 1, :c]
Out[2]:
#<Daru::CategoricalIndex(5): {a, 1, a, 1, c}>
In [3]:
dv = Daru::Vector.new 'a'..'e', index: idx
Out[3]:
Daru::Vector(5)
a a
1 b
a c
1 d
c e

Get elements with single category.

In [4]:
dv[:a]
Out[4]:
Daru::Vector(2)
a a
a c

Retrive elements by multiple categories.

In [5]:
dv[:a, 1]
Out[5]:
Daru::Vector(4)
a a
1 b
a c
1 d

Get elements by position using #at.

In [6]:
dv.at 1
Out[6]:
"b"

You can get multiple element as a vector if you specify more than one position.

In [7]:
dv.at 0, 1, 2
Out[7]:
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.

In [8]:
dv[3]
Out[8]:
"d"

Get elements by multiple positions.

In [9]:
dv[0, 3]
Out[9]:
Daru::Vector(2)
a a
1 d

Change values at certain indexes using []=

In [10]:
dv[:a] = 20
dv
Out[10]:
Daru::Vector(5)
a 20
1 b
a 20
1 d
c e

Change values at certain positions using at_set

In [11]:
dv.at_set [0, 1], 100
dv
Out[11]:
Daru::Vector(5)
a 100
1 100
a 20
1 d
c e

Other index classes

Overall all Index classes API are improved. Everyclass respond to multiple indexes, positional index, give proper error messages, etc.

Here are few examples:

Index

In [12]:
idx = Daru::Index.new [:a, :b, :c, :d]
Out[12]:
#<Daru::Index(4): {a, b, c, d}>
In [13]:
dv = Daru::Vector.new 1..4, index: idx
Out[13]:
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.

In [14]:
dv[:a, :b]
Out[14]:
Daru::Vector(2)
a 1
b 2

Retive values by positions.

In [15]:
dv.at 0, 1
Out[15]:
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.

In [16]:
dv[:a, :b] = 'x'
dv
Out[16]:
Daru::Vector(4)
a x
b x
c 3
d 4

Set values in vector by positions.

In [17]:
dv.at_set [0, 2], 'y'
dv
Out[17]:
Daru::Vector(4)
a y
b x
c y
d 4

MultiIndex

In [18]:
idx = Daru::MultiIndex.from_tuples [
          [:a,:one,:bar],
          [:a,:one,:baz],
          [:a,:two,:bat],
          [:b,:one,:bar],
          [:b,:two,:baz],
        ]
Out[18]:
Daru::MultiIndex(5x3)
a one bar
baz
two bat
b one bar
two baz
In [19]:
dv = Daru::Vector.new 1..5, index: idx
Out[19]:
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

In [20]:
dv[:a]
Out[20]:
Daru::Vector(3)
one bar 1
baz 2
two bat 3

On mentioning complete index, one will get the value at that index

In [21]:
dv[:a, :one, :bar]
Out[21]:
1

Retrive values by positions

In [22]:
dv.at 0, 1, 2
Out[22]:
Daru::Vector(3)
a one bar 1
baz 2
two bat 3

Assign values by index or positions

In [23]:
dv[:a, :one] = 'x'
dv
Out[23]:
Daru::Vector(5)
a one bar x
baz x
two bat 3
b one bar 4
two baz 5

Assign values by positions

In [24]:
dv.at_set [0, 1, 2], 'z'
dv
Out[24]:
Daru::Vector(5)
a one bar z
baz z
two bat z
b one bar 4
two baz 5