1 Star 4 Fork 11

王布衣 / pandas

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
series_diff.go 1.42 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-02-16 12:02 . !61#I6CYP6实现通达信LLVBARS函数
package pandas
import (
"gitee.com/quant1x/pandas/stat"
"reflect"
)
// Diff 元素的第一个离散差
// First discrete difference of element.
// Calculates the difference of a {klass} element compared with another
// element in the {klass} (default is element in previous row).
func (self *NDFrame) Diff(param any) (s stat.Series) {
if !(self.type_ == stat.SERIES_TYPE_INT64 || self.type_ == stat.SERIES_TYPE_FLOAT32 || self.type_ == stat.SERIES_TYPE_FLOAT64) {
return NewSeries(stat.SERIES_TYPE_INVAILD, "", "")
}
var N []stat.DType
switch v := param.(type) {
case int:
N = stat.Repeat[stat.DType](stat.DType(v), self.Len())
case stat.Series:
vs := v.DTypes()
N = stat.Align(vs, stat.DTypeNaN, self.Len())
default:
//periods = 1
N = stat.Repeat[stat.DType](stat.DType(1), self.Len())
}
r := stat.RollingAndExpandingMixin{
Window: N,
Series: self,
}
var d []stat.DType
var front = stat.DTypeNaN
for _, block := range r.GetBlocks() {
vs := reflect.ValueOf(block.Values())
vl := vs.Len()
if vl == 0 {
d = append(d, stat.DTypeNaN)
continue
}
vf := vs.Index(0).Interface()
vc := vs.Index(vl - 1).Interface()
cu := stat.Any2DType(vc)
cf := stat.Any2DType(vf)
if stat.DTypeIsNaN(cu) || stat.DTypeIsNaN(front) {
front = cf
d = append(d, stat.DTypeNaN)
continue
}
diff := cu - front
d = append(d, diff)
front = cf
}
s = NewSeries(stat.SERIES_TYPE_DTYPE, r.Series.Name(), d)
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/pandas.git
git@gitee.com:quant1x/pandas.git
quant1x
pandas
pandas
v0.9.13

搜索帮助

344bd9b3 5694891 D2dac590 5694891