1 Star 4 Fork 11

王布衣 / pandas

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
rolling.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-05-13 08:32 . 更新依赖库版本
package stat
import (
"gitee.com/quant1x/gox/exception"
)
// Rolling returns an array with elements that roll beyond the last position are re-introduced at the first.
// 滑动窗口, 数据不足是用空数组占位
func Rolling[T BaseType](S []T, N any) [][]T {
sLen := len(S)
// 这样就具备了序列化滑动窗口的特性了
var window []DType
switch vn := N.(type) {
case int:
window = Repeat(DType(vn), sLen)
case []int:
_N := Slice2DType(vn)
//nd := _N[len(_N) - 1]
window = Align[DType](_N, DTypeNaN, sLen)
case []DType:
window = Align(vn, DTypeNaN, sLen)
case []T: // 这块到不了, N和S不是同一个泛型类型
window = Slice2DType(vn)
window = Align[DType](window, DTypeNaN, sLen)
default:
panic(exception.New(1, "error window"))
}
blocks := make([][]T, sLen)
for i := 0; i < sLen; i++ {
n := window[i]
shift := int(n)
if DTypeIsNaN(n) || shift > i+1 {
blocks[i] = []T{}
continue
}
start := i + 1 - shift
end := i + 1
subSet := S[start:end]
blocks[i] = subSet
}
return blocks
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/pandas.git
git@gitee.com:quant1x/pandas.git
quant1x
pandas
pandas
v1.0.6

搜索帮助

344bd9b3 5694891 D2dac590 5694891