indexedSeries
语法
indexedSeries(index, value)
参数
index 与 value 为等长向量。index 须严格递增,无重复项。
详情
创建一个有索引的序列。
indexedSeries 支持面板数据对齐运算。普通矩阵或向量进行二元运算时,按照对应元素分别进行计算,需要保持维度(shape)一致。而 indexedSeries 之间,indexedSeries 与 indexed Matrix 之间,进行二元运算时,根据行列标签(index)自动对齐,对维度没有硬性要求。
支持的二元操作包括:
(1) 算术运算符和函数:+, -, *, /(整除), \(ratio), %(mod), pow
(2) 逻辑运算符和函数:<, <=, >, >=, ==, !=, <>, &&, ||, &, |, ^
(3) 滑动窗口函数:mwavg, mwsum, mbeta, mcorr, mcovar
(4) 累计窗口函数:cumwavg, cumwsum, cumbeta, cumcorr, cumcovar
(5) 聚合函数:wavg, wsum, beta, corr, covar
例子
例1. indexedSeries 之间的运算,长度可以相等或者不等,根据 index 对齐。
$ s1 = indexedSeries(2012.01.01..2012.01.04, [10, 20, 30, 40])
$ s2 = indexedSeries(2011.12.30..2012.01.01, [50, 60, 70])
$ res = s1 + s2
col1 |
|
---|---|
2011.12.30 |
|
2011.12.31 |
|
2012.01.01 |
80 |
2012.01.02 |
|
2012.01.03 |
|
2012.01.04 |
例2. indexedSeries 和 vector 之间运算,长度必须相等。
$ s1 = indexedSeries(2012.01.01..2012.01.04, [10, 20, 30, 40])
$ v = [1,2,3,4]
$ res = s1+v
col1 |
|
---|---|
2012.01.01 |
11 |
2012.01.02 |
22 |
2012.01.03 |
33 |
2012.01.04 |
44 |
例3. indexedSeries 和 indexed Matrix 进行运算时,根据 index 对齐。
$ s1 = indexedSeries(2012.01.01 2012.01.03 2012.01.04 2012.01.06, [10, 20, 30, 40])
$ m = matrix(1..6, 11..16).rename!(2012.01.01..2012.01.06,`x`y).setIndexedMatrix!()
$ s1 + m
IBM |
MSFT |
|
---|---|---|
2012.01.01 |
11 |
21 |
2012.01.02 |
||
2012.01.03 |
23 |
33 |
2012.01.04 |
34 |
44 |
2012.01.05 |
||
2012.01.06 |
46 |
56 |
例4. indexedSeries 与没有设置 index 的 matrix 进行运算,长度必须相等。
$ s1 = indexedSeries(2012.01.01..2012.01.04, [10, 20, 30, 40])
$ m = matrix([1,1,1,1], [2,2,2,2])
$ res = s1 pow m
col1 |
col2 |
---|---|
10 |
100 |
20 |
400 |
30 |
900 |
40 |
1,600 |