From c54e5c5d94bdd0fd68e834d1fdcf472787cbc362 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Sun, 5 Feb 2023 21:44:27 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E4=BF=AE=E8=AE=A2=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/ref.go | 1 + formula/ref_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/formula/ref.go b/formula/ref.go index e04ea3b..c50e838 100644 --- a/formula/ref.go +++ b/formula/ref.go @@ -6,6 +6,7 @@ import ( "gitee.com/quant1x/pandas/stat" ) +// REF 引用前N的序列 func REF(S pandas.Series, N any) any { var X []float32 switch v := N.(type) { diff --git a/formula/ref_test.go b/formula/ref_test.go index 82f2e25..9e3ab56 100644 --- a/formula/ref_test.go +++ b/formula/ref_test.go @@ -30,7 +30,7 @@ func TestREF(t *testing.T) { C := df2.Col("c") D := df2.Col("d") - // 2日均线 + // 2日前的D值 r2 := REF(D, 2) fmt.Println(r2) -- Gitee From e190ee03ac300a68fd65f113bad1c9cf1a009349 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:21:44 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sort.Interface=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E9=99=A4Len()=E4=B9=8B=E5=A4=96=E7=9A=84Less?= =?UTF-8?q?()=E5=92=8CSwap=E4=B8=A4=E4=B8=AA=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- series.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/series.go b/series.go index 7135d14..e4afc96 100644 --- a/series.go +++ b/series.go @@ -32,10 +32,21 @@ type Series interface { // Type returns the type of data the series holds. // 返回series的数据类型 Type() Type - // Len 获得行数 - Len() int // Values 获得全部数据集 Values() any + + // NaN 输出默认的NaN + NaN() any + + // sort.Interface + + // Len 获得行数, 实现sort.Interface接口的获取元素数量方法 + Len() int + // Less 实现sort.Interface接口的比较元素方法 + Less(i, j int) bool + // Swap 实现sort.Interface接口的交换元素方法 + Swap(i, j int) + // Empty returns an empty Series of the same type Empty() Series // Copy 复制 -- Gitee From 098ab915719f6a7bf035be3d39a1bd0190dbe368 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:22:57 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E6=95=B0=E7=BB=84=E7=9A=84=E6=8E=92=E5=BA=8F=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_string.go | 18 ++++++++++++++++++ type_string_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/type_string.go b/type_string.go index 15441b9..5fdd079 100644 --- a/type_string.go +++ b/type_string.go @@ -95,3 +95,21 @@ func StringIsNaN(s string) bool { } return false } + +// Arraystring 将[]string定义为Arraystring类型 +type Arraystring []string + +// Len 实现sort.Interface接口的获取元素数量方法 +func (m Arraystring) Len() int { + return len(m) +} + +// Less 实现sort.Interface接口的比较元素方法 +func (m Arraystring) Less(i, j int) bool { + return m[i] < m[j] +} + +// Swap 实现sort.Interface接口的交换元素方法 +func (m Arraystring) Swap(i, j int) { + m[i], m[j] = m[j], m[i] +} diff --git a/type_string_test.go b/type_string_test.go index 562a791..960ac22 100644 --- a/type_string_test.go +++ b/type_string_test.go @@ -1,7 +1,9 @@ package pandas import ( + "fmt" "math" + "sort" "testing" ) @@ -44,3 +46,30 @@ func TestAnyToString(t *testing.T) { }) } } + +func TestStringListSort(t *testing.T) { + // 准备一个内容被打乱顺序的字符串切片 + names := []string{ + "3. Triple Kill", + "5. Penta Kill", + "2. Double Kill", + "4. Quadra Kill", + "1. First Blood", + } + sa := Arraystring(names) + // 使用sort包进行排序 + sort.Sort(sa) + // 遍历打印结果 + for _, v := range names { + fmt.Printf("%s\n", v) + } + n1 := sort.StringSlice{ + "3. Triple Kill", + "5. Penta Kill", + "2. Double Kill", + "4. Quadra Kill", + "1. First Blood", + } + sort.Sort(n1) + fmt.Println(n1) +} -- Gitee From b0f577cbe0cfcdc0176f094dd4f1a824690b20f3 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:23:45 +0800 Subject: [PATCH 04/14] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E8=BE=93=E5=87=BANaN=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/generic.go b/generic.go index ddf9b17..c031038 100644 --- a/generic.go +++ b/generic.go @@ -18,6 +18,7 @@ type NDFrame struct { formatter StringFormatter // 字符串格式化工具 name string // 帧名称 type_ Type // values元素类型 + copy_ bool // 是否副本 nilCount int // nil和nan的元素有多少, 这种统计在bool和int64类型中不会大于0, 只对float64及string有效 rows int // 行数 values any // 只能是一个一维slice, 在所有的运算中, values强制转换成float64切片 @@ -128,14 +129,28 @@ func (self *NDFrame) Type() Type { return self.type_ } -func (self *NDFrame) Len() int { - return self.rows -} - func (self *NDFrame) Values() any { return self.values } +// NaN 输出默认的NaN +func (self *NDFrame) NaN() any { + switch self.values.(type) { + case []bool: + return BoolNaN + case []string: + return StringNaN + case []int64: + return Nil2Int64 + case []float32: + return Nil2Float32 + case []float64: + return Nil2Float64 + default: + panic(ErrUnsupportedType) + } +} + func (self *NDFrame) Empty() Series { var frame NDFrame if self.type_ == SERIES_TYPE_STRING { @@ -184,7 +199,7 @@ func (self *NDFrame) Empty() Series { values: []float64{}, } } else { - panic("无法识别的类型") + panic(ErrUnsupportedType) } return &frame } -- Gitee From c3352b6ab991661bd69b3cb1222d99107bdae09b Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:31:04 +0800 Subject: [PATCH 05/14] =?UTF-8?q?=E4=BC=98=E5=8C=96max?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic_max.go | 73 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/generic_max.go b/generic_max.go index b5fcdd7..da8c166 100644 --- a/generic_max.go +++ b/generic_max.go @@ -1,8 +1,24 @@ package pandas +import "gitee.com/quant1x/pandas/stat" + func (self *NDFrame) Max() any { values := self.Values() switch rows := values.(type) { + case []bool: + max := false + i := 0 + for idx, iv := range rows { + if iv && !max { + max = iv + i += 1 + } + _ = idx + } + if i > 0 { + return max + } + return false case []string: max := "" i := 0 @@ -34,23 +50,46 @@ func (self *NDFrame) Max() any { _ = idx } return max + //case []float32: + // max := float32(0) + // i := 0 + // for idx, iv := range rows { + // if Float32IsNaN(iv) { + // continue + // } + // if iv > max { + // max = iv + // i += 1 + // } + // _ = idx + // } + // if i > 0 { + // return max + // } + // return Nil2Float32 + case []float32: + return stat.Max(rows) + //case []float64: + // max := float64(0) + // i := 0 + // for idx, iv := range rows { + // if Float64IsNaN(iv) { + // continue + // } + // if iv > max { + // max = iv + // i += 1 + // } + // _ = idx + // } + // if i > 0 { + // return max + // } + // return Nil2Float64 case []float64: - max := float64(0) - i := 0 - for idx, iv := range rows { - if Float64IsNaN(iv) { - continue - } - if iv > max { - max = iv - i += 1 - } - _ = idx - } - if i > 0 { - return max - } - return Nil2Float64 + return stat.Max(rows) + default: + panic(ErrUnsupportedType) } - return Nil2Float64 + //return Nil2Float64 } -- Gitee From 576c979f6a8799d284e6c431b28d4309038961d3 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:31:34 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sort.Interface=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E9=99=A4Len()=E4=B9=8B=E5=A4=96=E7=9A=84Less?= =?UTF-8?q?()=E5=92=8CSwap=E4=B8=A4=E4=B8=AA=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic_sort.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 generic_sort.go diff --git a/generic_sort.go b/generic_sort.go new file mode 100644 index 0000000..a7d625a --- /dev/null +++ b/generic_sort.go @@ -0,0 +1,66 @@ +package pandas + +// Len 获得行数, 实现sort.Interface接口的获取元素数量方法 +func (self *NDFrame) Len() int { + return self.rows +} + +// Less 实现sort.Interface接口的比较元素方法 +func (self *NDFrame) Less(i, j int) bool { + if self.type_ == SERIES_TYPE_BOOL { + values := self.Values().([]bool) + var ( + a = int(0) + b = int(0) + ) + if values[i] { + a = 1 + } + if values[j] { + b = 1 + } + return a < b + } else if self.type_ == SERIES_TYPE_INT64 { + values := self.Values().([]int64) + return values[i] < values[j] + } else if self.type_ == SERIES_TYPE_FLOAT32 { + values := self.Values().([]float32) + return values[i] < values[j] + } else if self.type_ == SERIES_TYPE_FLOAT64 { + values := self.Values().([]float64) + return values[i] < values[j] + } else if self.type_ == SERIES_TYPE_STRING { + values := self.Values().([]string) + return values[i] < values[j] + } else { + // SERIES_TYPE_INVAILD + // 应该到不了这里, Len()会返回0 + panic(ErrUnsupportedType) + } + return false + +} + +// Swap 实现sort.Interface接口的交换元素方法 +func (self *NDFrame) Swap(i, j int) { + if self.type_ == SERIES_TYPE_BOOL { + values := self.Values().([]bool) + values[i], values[j] = values[j], values[i] + } else if self.type_ == SERIES_TYPE_INT64 { + values := self.Values().([]int64) + values[i], values[j] = values[j], values[i] + } else if self.type_ == SERIES_TYPE_FLOAT32 { + values := self.Values().([]float32) + values[i], values[j] = values[j], values[i] + } else if self.type_ == SERIES_TYPE_FLOAT64 { + values := self.Values().([]float64) + values[i], values[j] = values[j], values[i] + } else if self.type_ == SERIES_TYPE_STRING { + values := self.Values().([]string) + values[i], values[j] = values[j], values[i] + } else { + // SERIES_TYPE_INVAILD + // 应该到不了这里, Len()会返回0 + panic(ErrUnsupportedType) + } +} -- Gitee From b706d44c012e5056b3ff5e2e98054d3b309a1a5a Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:32:15 +0800 Subject: [PATCH 07/14] =?UTF-8?q?=E9=80=89=E6=8B=A9=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E9=9B=86=E6=97=B6=E4=BD=BF=E7=94=A8copy=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic_rolling.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generic_rolling.go b/generic_rolling.go index 72e7d22..3c1eb6e 100644 --- a/generic_rolling.go +++ b/generic_rolling.go @@ -51,7 +51,7 @@ func (r RollingAndExpandingMixin) getBlocks() (blocks []Series) { window := int(N) start := i + 1 - window end := i + 1 - blocks = append(blocks, r.series.Subset(start, end)) + blocks = append(blocks, r.series.Subset(start, end, true)) } return -- Gitee From 05ff84a5ded2791ac418e888063d7eaf523243bb Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:32:43 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=9B=E5=9E=8B?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B,=20=E5=BC=BA=E5=8C=96=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic_number.go | 61 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/generic_number.go b/generic_number.go index a425471..243467d 100644 --- a/generic_number.go +++ b/generic_number.go @@ -20,16 +20,69 @@ type Number32 interface { } type Number64 interface { - ~int64 | ~uint64 | float64 + ~int64 | ~uint64 | float64 | int | uint } -type Float interface { - ~float32 | ~float64 +// NumberOfCPUBits The number of CPU bits is related +type NumberOfCPUBitsRelated interface { + ~int | ~uint | ~uintptr +} + +type Integer interface { + Number8 | Number16 | Number32 | Number64 } // Number int和uint的长度取决于CPU是多少位 type Number interface { - Number8 | Number16 | Number32 | Number64 | Float | int | uint + Integer | Float +} + +//type Number interface { +// constraints.Float | constraints.Integer +//} + +// Signed is a constraint that permits any signed integer type. +// If future releases of Go add new predeclared signed integer types, +// this constraint will be modified to include them. +type Signed interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 +} + +// Unsigned is a constraint that permits any unsigned integer type. +// If future releases of Go add new predeclared unsigned integer types, +// this constraint will be modified to include them. +// TODO:~uintptr应该是没有应用场景 +type Unsigned interface { + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr +} + +// Integer_old Integer is a constraint that permits any integer type. +// If future releases of Go add new predeclared integer types, +// this constraint will be modified to include them. +type Integer_old interface { + Signed | Unsigned +} + +// Float is a constraint that permits any floating-point type. +// If future releases of Go add new predeclared floating-point types, +// this constraint will be modified to include them. +type Float interface { + ~float32 | ~float64 +} + +// Complex is a constraint that permits any complex numeric type. +// If future releases of Go add new predeclared complex numeric types, +// this constraint will be modified to include them. +type Complex interface { + ~complex64 | ~complex128 +} + +// Ordered is a constraint that permits any ordered type: any type +// that supports the operators < <= >= >. +// If future releases of Go add new ordered types, +// this constraint will be modified to include them. +type Ordered interface { + Integer | Float | ~string } //const ( -- Gitee From 80805ce34be37f3c69bc98615e272c04a3f197b4 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:33:11 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E8=BD=AC=E6=8D=A2=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frame_type.go | 115 ---------------------------------------- generic_convert.go | 93 ++++++++++++++++++++++++++++++++ generic_convert_test.go | 9 ++++ 3 files changed, 102 insertions(+), 115 deletions(-) delete mode 100644 frame_type.go create mode 100644 generic_convert.go create mode 100644 generic_convert_test.go diff --git a/frame_type.go b/frame_type.go deleted file mode 100644 index 1eff54a..0000000 --- a/frame_type.go +++ /dev/null @@ -1,115 +0,0 @@ -package pandas - -import ( - "reflect" - "strings" -) - -var ( - //kindNames = []string{ - // reflect.Invalid: "invalid", - // reflect.Bool: "bool", - // reflect.Int: "int", - // reflect.Int8: "int8", - // reflect.Int16: "int16", - // reflect.Int32: "int32", - // reflect.Int64: "int64", - // reflect.Uint: "uint", - // reflect.Uint8: "uint8", - // reflect.Uint16: "uint16", - // reflect.Uint32: "uint32", - // reflect.Uint64: "uint64", - // reflect.Uintptr: "uintptr", - // reflect.Float32: "float32", - // reflect.Float64: "float64", - // reflect.Complex64: "complex64", - // reflect.Complex128: "complex128", - // reflect.Array: "array", - // reflect.Chan: "chan", - // reflect.Func: "func", - // reflect.Interface: "interface", - // reflect.Map: "map", - // reflect.Pointer: "ptr", - // reflect.Slice: "slice", - // reflect.String: "string", - // reflect.UnsafePointer: "unsafe.Pointer", - //} - - sKindInvalid = "invalid" - sKindBool = "bool" - sKindInt = "int" - sKindInt8 = "int8" - sKindInt16 = "int16" - sKindInt32 = "int32" - sKindInt64 = "int64" - sKindUint = "uint" - sKindUint8 = "uint8" - sKindUint16 = "uint16" - sKindUint32 = "uint32" - sKindUint64 = "uint64" - sKindUintptr = "uintptr" - sKindFloat32 = "float32" - sKindFloat64 = "float64" - sKindComplex64 = "complex64" - sKindComplex128 = "complex128" - sKindArray = "array" - sKindChan = "chan" - sKindFunc = "func" - sKindInterface = "interface" - sKindMap = "map" - sKindPointer = "ptr" - sKindSlice = "slice" - sKindString = "string" - sKindUnsafePointer = "unsafe.Pointer" - // 缓存Kind对应关系 - mapKind = map[string]reflect.Kind{ - sKindInvalid: reflect.Invalid, - sKindBool: reflect.Bool, - sKindInt: reflect.Int, - sKindInt8: reflect.Int8, - sKindInt16: reflect.Int16, - sKindInt32: reflect.Int32, - sKindInt64: reflect.Int64, - sKindUint: reflect.Uint, - sKindUint8: reflect.Uint8, - sKindUint16: reflect.Uint16, - sKindUint32: reflect.Uint32, - sKindUint64: reflect.Uint64, - sKindUintptr: reflect.Uintptr, - sKindFloat32: reflect.Float32, - sKindFloat64: reflect.Float64, - sKindComplex64: reflect.Complex64, - sKindComplex128: reflect.Complex128, - sKindArray: reflect.Array, - sKindChan: reflect.Chan, - sKindFunc: reflect.Func, - sKindInterface: reflect.Interface, - sKindMap: reflect.Map, - sKindPointer: reflect.Pointer, - sKindSlice: reflect.Slice, - sKindString: reflect.String, - sKindUnsafePointer: reflect.UnsafePointer, - } -) - -func checkoutRawType(frame any) Type { - ft := reflect.TypeOf(frame) - strType := ft.String() - pos := strings.LastIndexByte(strType, '[') - if pos < 0 { - return SERIES_TYPE_INVAILD - } - strType = strType[pos+1:] - pos = strings.LastIndexByte(strType, ']') - if pos < 0 { - return SERIES_TYPE_INVAILD - } - strType = strings.TrimSpace(strType[:pos]) - if len(strType) < 1 { - return SERIES_TYPE_INVAILD - } - if t, ok := mapKind[strType]; ok { - return t - } - return SERIES_TYPE_INVAILD -} diff --git a/generic_convert.go b/generic_convert.go new file mode 100644 index 0000000..d51e6fb --- /dev/null +++ b/generic_convert.go @@ -0,0 +1,93 @@ +package pandas + +import ( + "github.com/viterin/vek" + "github.com/viterin/vek/vek32" + "reflect" +) + +// 这里做数组统一转换 +func convert[T GenericType](s Series, v T) { + + values := s.Values() + rawType := checkoutRawType(values) + values, ok := values.([]T) + _ = rawType + _ = ok + +} + +func ToFloat32(s Series) []float32 { + length := s.Len() + defaultSlice := vek32.Repeat(Nil2Float32, length) + values := s.Values() + __type := s.Type() + if __type == SERIES_TYPE_INVAILD { + return defaultSlice + } else if __type == SERIES_TYPE_BOOL { + return vek32.FromBool(values.([]bool)) + } else if __type == SERIES_TYPE_INT64 { + return vek32.FromInt64(values.([]int64)) + } else if __type == SERIES_TYPE_FLOAT32 { + return values.([]float32) + } else if __type == SERIES_TYPE_FLOAT64 { + return vek32.FromFloat64(values.([]float64)) + } else if __type == reflect.Int32 { + return vek32.FromInt32(values.([]int32)) + } else { + return defaultSlice + } +} + +func ToFloat64(s Series) []float64 { + length := s.Len() + defaultSlice := vek.Repeat(Nil2Float64, length) + values := s.Values() + __type := s.Type() + if __type == SERIES_TYPE_INVAILD { + return defaultSlice + } else if __type == SERIES_TYPE_BOOL { + return vek.FromBool(values.([]bool)) + } else if __type == SERIES_TYPE_INT64 { + return vek.FromInt64(values.([]int64)) + } else if __type == SERIES_TYPE_FLOAT32 { + return vek.FromFloat32(values.([]float32)) + } else if __type == SERIES_TYPE_FLOAT64 { + return values.([]float64) // 是否复制 + } else if __type == reflect.Int32 { + return vek.FromInt32(values.([]int32)) + } else { + return defaultSlice + } +} + +func ToBool(s Series) []bool { + length := s.Len() + defaultSlice := make([]bool, length) + values := s.Values() + __type := s.Type() + if __type == SERIES_TYPE_INVAILD { + return defaultSlice + } else if __type == SERIES_TYPE_BOOL { + return values.([]bool) + } else if __type == SERIES_TYPE_INT64 { + return __toBool(values.([]int64)) + } else if __type == SERIES_TYPE_FLOAT32 { + return __toBool(values.([]float32)) + } else { + return defaultSlice + } +} + +func __toBool[T Number](values []T) []bool { + length := len(values) + vs := make([]bool, length) + for i, v := range values { + if v != 0 { + vs[i] = true + } else { + vs[i] = false + } + } + return vs +} diff --git a/generic_convert_test.go b/generic_convert_test.go new file mode 100644 index 0000000..303bd73 --- /dev/null +++ b/generic_convert_test.go @@ -0,0 +1,9 @@ +package pandas + +import ( + "testing" +) + +func Test_convert(t *testing.T) { + +} -- Gitee From 3fdf0353e3b5eb87ffc22ed1edf32b6c7a7a4cfb Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:34:06 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=E8=A7=84=E5=88=B6=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E5=BD=92=E5=B1=9E=E5=88=B0=E5=86=85=E5=BB=BA=E6=BA=90?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builtin.go | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/builtin.go b/builtin.go index 342d9ef..7af328d 100644 --- a/builtin.go +++ b/builtin.go @@ -56,3 +56,112 @@ func isPoint(v any) bool { kind := reflect.ValueOf(v).Kind() return reflect.Pointer == kind } + +var ( + //kindNames = []string{ + // reflect.Invalid: "invalid", + // reflect.Bool: "bool", + // reflect.Int: "int", + // reflect.Int8: "int8", + // reflect.Int16: "int16", + // reflect.Int32: "int32", + // reflect.Int64: "int64", + // reflect.Uint: "uint", + // reflect.Uint8: "uint8", + // reflect.Uint16: "uint16", + // reflect.Uint32: "uint32", + // reflect.Uint64: "uint64", + // reflect.Uintptr: "uintptr", + // reflect.Float32: "float32", + // reflect.Float64: "float64", + // reflect.Complex64: "complex64", + // reflect.Complex128: "complex128", + // reflect.Array: "array", + // reflect.Chan: "chan", + // reflect.Func: "func", + // reflect.Interface: "interface", + // reflect.Map: "map", + // reflect.Pointer: "ptr", + // reflect.Slice: "slice", + // reflect.String: "string", + // reflect.UnsafePointer: "unsafe.Pointer", + //} + + sKindInvalid = "invalid" + sKindBool = "bool" + sKindInt = "int" + sKindInt8 = "int8" + sKindInt16 = "int16" + sKindInt32 = "int32" + sKindInt64 = "int64" + sKindUint = "uint" + sKindUint8 = "uint8" + sKindUint16 = "uint16" + sKindUint32 = "uint32" + sKindUint64 = "uint64" + sKindUintptr = "uintptr" + sKindFloat32 = "float32" + sKindFloat64 = "float64" + sKindComplex64 = "complex64" + sKindComplex128 = "complex128" + sKindArray = "array" + sKindChan = "chan" + sKindFunc = "func" + sKindInterface = "interface" + sKindMap = "map" + sKindPointer = "ptr" + sKindSlice = "slice" + sKindString = "string" + sKindUnsafePointer = "unsafe.Pointer" + // 缓存Kind对应关系 + mapKind = map[string]reflect.Kind{ + sKindInvalid: reflect.Invalid, + sKindBool: reflect.Bool, + sKindInt: reflect.Int, + sKindInt8: reflect.Int8, + sKindInt16: reflect.Int16, + sKindInt32: reflect.Int32, + sKindInt64: reflect.Int64, + sKindUint: reflect.Uint, + sKindUint8: reflect.Uint8, + sKindUint16: reflect.Uint16, + sKindUint32: reflect.Uint32, + sKindUint64: reflect.Uint64, + sKindUintptr: reflect.Uintptr, + sKindFloat32: reflect.Float32, + sKindFloat64: reflect.Float64, + sKindComplex64: reflect.Complex64, + sKindComplex128: reflect.Complex128, + sKindArray: reflect.Array, + sKindChan: reflect.Chan, + sKindFunc: reflect.Func, + sKindInterface: reflect.Interface, + sKindMap: reflect.Map, + sKindPointer: reflect.Pointer, + sKindSlice: reflect.Slice, + sKindString: reflect.String, + sKindUnsafePointer: reflect.UnsafePointer, + } +) + +func checkoutRawType(frame any) Type { + ft := reflect.TypeOf(frame) + strType := ft.String() + pos := strings.LastIndexByte(strType, '[') + if pos < 0 { + return SERIES_TYPE_INVAILD + } + strType = strType[pos+1:] + pos = strings.LastIndexByte(strType, ']') + if pos < 0 { + return SERIES_TYPE_INVAILD + } + strType = strings.TrimSpace(strType[:pos]) + if len(strType) < 1 { + return SERIES_TYPE_INVAILD + } + if t, ok := mapKind[strType]; ok { + return t + } + return SERIES_TYPE_INVAILD +} -- Gitee From 36f9b9172cfaf58a420610122f66ac8fed348b5a Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:34:34 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic_type.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/generic_type.go b/generic_type.go index 2389f98..d185d21 100644 --- a/generic_type.go +++ b/generic_type.go @@ -2,6 +2,7 @@ package pandas import ( "fmt" + "gitee.com/quant1x/pandas/exception" "reflect" "strconv" "strings" @@ -11,6 +12,10 @@ const ( MAX_FLOAT32_PRICE = float32(9999.9999) // float32的价最大阀值触发扩展到float64 ) +var ( + ErrUnsupportedType = exception.New(0, "Unsupported type") +) + func mustFloat64(f float32) bool { if f > MAX_FLOAT32_PRICE { return true -- Gitee From eaf0703e53777c1ed99d11445d08619de9e06782 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:34:52 +0800 Subject: [PATCH 12/14] =?UTF-8?q?rolling=E5=A2=9E=E5=8A=A0max=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rolling_max.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rolling_max.go diff --git a/rolling_max.go b/rolling_max.go new file mode 100644 index 0000000..bac9f1a --- /dev/null +++ b/rolling_max.go @@ -0,0 +1,21 @@ +package pandas + +import "sort" + +func (r RollingAndExpandingMixin) Max() (s Series) { + s = r.series.Empty() + for _, block := range r.getBlocks() { + // 1. 排序处理方式 + if block.Len() == 0 { + s.Append(s.NaN()) + continue + } + sort.Sort(block) + r := RangeFinite(-1) + _s := block.Select(r) + s.Append(_s.Values()) + // 2. Series.Max方法 + //s.Append(block.Max()) + } + return +} -- Gitee From 69938c983e9b027782b780204b2abdbfe4f32653 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:35:35 +0800 Subject: [PATCH 13/14] =?UTF-8?q?#I6CYPC=20=E5=A2=9E=E5=8A=A0HHV=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/hhv.go | 11 ++++++++++ formula/hhv_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 formula/hhv.go create mode 100644 formula/hhv_test.go diff --git a/formula/hhv.go b/formula/hhv.go new file mode 100644 index 0000000..d688268 --- /dev/null +++ b/formula/hhv.go @@ -0,0 +1,11 @@ +package formula + +import "gitee.com/quant1x/pandas" + +type InputType interface { + ~int | ~[]float32 | ~[]float64 +} + +func HHV(S pandas.Series, N any) pandas.Series { + return S.Rolling2(N).Max() +} diff --git a/formula/hhv_test.go b/formula/hhv_test.go new file mode 100644 index 0000000..12d8643 --- /dev/null +++ b/formula/hhv_test.go @@ -0,0 +1,49 @@ +package formula + +import ( + "fmt" + "gitee.com/quant1x/pandas" + "testing" +) + +func TestHHV(t *testing.T) { + type testStruct struct { + A string + B int + C bool + D float32 + } + data := []testStruct{ + {"a", 1, true, 0.0}, + {"b", 2, false, 0.5}, + } + df1 := pandas.LoadStructs(data) + fmt.Println(df1) + // 修改列名 + _ = df1.SetNames("a", "b", "c", "d") + // 增加1列 + s_e := pandas.GenericSeries[string]("", "a0", "a1", "a2", "a3") + df2 := df1.Join(s_e) + fmt.Println(df2) + A := df2.Col("a") + B := df2.Col("b") + C := df2.Col("c") + D := df2.Col("d") + + r2 := HHV(D, 2) + fmt.Println(r2) + + r2 = HHV(A, 2) + fmt.Println(r2) + + r2 = HHV(df2.Col("X0"), 2) + fmt.Println(r2) + + r2 = HHV(C, 2) + fmt.Println(r2) + + _ = A + _ = B + _ = C + _ = D +} -- Gitee From b04e7a0ba199e049c9159d4469ec9a5bbdbf97cb Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 6 Feb 2023 11:35:35 +0800 Subject: [PATCH 14/14] =?UTF-8?q?#I6CYOP=20=E5=A2=9E=E5=8A=A0HHV=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/hhv.go | 11 ++++++++++ formula/hhv_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 formula/hhv.go create mode 100644 formula/hhv_test.go diff --git a/formula/hhv.go b/formula/hhv.go new file mode 100644 index 0000000..d688268 --- /dev/null +++ b/formula/hhv.go @@ -0,0 +1,11 @@ +package formula + +import "gitee.com/quant1x/pandas" + +type InputType interface { + ~int | ~[]float32 | ~[]float64 +} + +func HHV(S pandas.Series, N any) pandas.Series { + return S.Rolling2(N).Max() +} diff --git a/formula/hhv_test.go b/formula/hhv_test.go new file mode 100644 index 0000000..12d8643 --- /dev/null +++ b/formula/hhv_test.go @@ -0,0 +1,49 @@ +package formula + +import ( + "fmt" + "gitee.com/quant1x/pandas" + "testing" +) + +func TestHHV(t *testing.T) { + type testStruct struct { + A string + B int + C bool + D float32 + } + data := []testStruct{ + {"a", 1, true, 0.0}, + {"b", 2, false, 0.5}, + } + df1 := pandas.LoadStructs(data) + fmt.Println(df1) + // 修改列名 + _ = df1.SetNames("a", "b", "c", "d") + // 增加1列 + s_e := pandas.GenericSeries[string]("", "a0", "a1", "a2", "a3") + df2 := df1.Join(s_e) + fmt.Println(df2) + A := df2.Col("a") + B := df2.Col("b") + C := df2.Col("c") + D := df2.Col("d") + + r2 := HHV(D, 2) + fmt.Println(r2) + + r2 = HHV(A, 2) + fmt.Println(r2) + + r2 = HHV(df2.Col("X0"), 2) + fmt.Println(r2) + + r2 = HHV(C, 2) + fmt.Println(r2) + + _ = A + _ = B + _ = C + _ = D +} -- Gitee