-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataframe_type.go
140 lines (132 loc) · 3.12 KB
/
dataframe_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package pandas
import (
"errors"
"fmt"
"gitee.com/quant1x/num"
"reflect"
"strconv"
"strings"
)
const (
//MAX_FLOAT32_PRICE = float32(9999.9999) // float32的价最大阀值触发扩展到float64
MAX_FLOAT32_PRICE = float32(0) // float32的价最大阀值触发扩展到float64
)
var (
ErrUnsupportedType = errors.New("unsupported type")
ErrCouldNotDetectType = errors.New("couldn't detect type")
)
func mustFloat64(f float32) bool {
if f > MAX_FLOAT32_PRICE {
return true
}
return false
}
func findTypeByString(arr []string) (Type, error) {
var hasFloats, hasInts, hasBools, hasStrings bool
var useInt32, useInt64 bool
var useFloat32, useFloat64 bool
var stringLengthEqual = -1
var stringLenth = -1
for _, str := range arr {
if str == "" || str == "NaN" {
continue
}
tLen := len(str)
if strings.HasPrefix(str, "0") {
stringLengthEqual = 0
}
if stringLenth < 1 {
if stringLengthEqual <= 0 {
stringLenth = tLen
}
} else if stringLengthEqual >= 0 && tLen != stringLenth {
stringLengthEqual += 1
}
// 整型
if d, err := strconv.ParseInt(str, 10, 64); err == nil {
hasInts = true
//if int32(d) <= num.MaxInt32 {
// useInt32 = true
//} else {
// useInt64 = true
//}
_ = d
}
// 浮点
if f, err := strconv.ParseFloat(str, 64); err == nil {
hasFloats = true
if float32(f) < num.MaxFloat32 {
if mustFloat64(float32(f)) {
useFloat64 = true
} else {
useFloat32 = true
}
}
continue
}
if str == "true" || str == "false" {
hasBools = true
continue
}
hasStrings = true
}
if !hasFloats && stringLengthEqual == 0 {
hasStrings = true
}
// 类型优先级, string > bool > float > int, string 为默认类型
switch {
case hasStrings:
return SERIES_TYPE_STRING, nil
case hasBools:
return SERIES_TYPE_BOOL, nil
case useFloat32 && !useFloat64:
return SERIES_TYPE_FLOAT32, nil
case hasFloats:
return SERIES_TYPE_FLOAT64, nil
case useInt32 && !useInt64:
return SERIES_TYPE_INT32, nil
case hasInts:
return SERIES_TYPE_INT64, nil
default:
return SERIES_TYPE_STRING, ErrCouldNotDetectType
}
}
func parseType(s string) (Type, error) {
switch s {
case "float", "float32":
return SERIES_TYPE_FLOAT32, nil
case "float64":
return SERIES_TYPE_FLOAT64, nil
case "int", "int64", "int32", "int16", "int8":
return SERIES_TYPE_INT64, nil
case "uint", "uint64", "uint32", "uint16", "uint8", "byte":
return SERIES_TYPE_INT64, nil
case "string":
return SERIES_TYPE_STRING, nil
case "bool":
return SERIES_TYPE_BOOL, nil
}
return SERIES_TYPE_INVAILD, fmt.Errorf("type (%s) is not supported", s)
}
func detectTypes[T num.GenericType](v T) (Type, any) {
var _type = SERIES_TYPE_STRING
vv := reflect.ValueOf(v)
vk := vv.Kind()
switch vk {
case reflect.Invalid:
_type = SERIES_TYPE_INVAILD
case reflect.Bool:
_type = SERIES_TYPE_BOOL
case reflect.Int64:
_type = SERIES_TYPE_INT64
case reflect.Float32:
_type = SERIES_TYPE_FLOAT32
case reflect.Float64:
_type = SERIES_TYPE_FLOAT64
case reflect.String:
_type = SERIES_TYPE_STRING
default:
panic(fmt.Errorf("unknown type, %+v", v))
}
return _type, vv.Interface()
}