-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_map_slice.go
183 lines (163 loc) · 3.79 KB
/
read_map_slice.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package pandat
import (
"math"
"reflect"
"strconv"
)
func ReadMap(data map[string][]string) *DataFrame[any] {
seriess := make([]*Series[any], 0, len(data))
for name, values := range data {
seriess = append(seriess, NewSeries(name, asType(values, determineType(values))...))
}
return NewDataFrame(seriess...)
}
func ReadSlice(arr [][]string, hasHeader bool) *DataFrame[any] {
seriess := make([]*Series[any], 0, len(arr))
for i, values := range arr {
if hasHeader {
header := values[0]
values = values[1:]
seriess = append(seriess, NewSeries[any](header, asType(values, determineType(values))...))
} else {
seriess = append(seriess, NewSeries[any](strconv.Itoa(i), asType(values, determineType(values))...))
}
}
return NewDataFrame(seriess...)
}
func determineType(arr []string) reflect.Kind {
var (
hasBool, hasFloat, hasInt, hasOthers bool
)
for _, val := range arr {
if val == "" {
continue
}
if val == "true" || val == "True" || val == "false" || val == "False" {
hasBool = true
continue
}
if _, err := strconv.ParseInt(val, 10, 64); err == nil {
hasInt = true
continue
}
if val == "NaN" {
hasFloat = true
continue
}
if _, err := strconv.ParseFloat(val, 64); err == nil {
hasFloat = true
continue
}
hasOthers = true
break // fast break on string
}
// mixed dtype, each value has its own go type
if hasOthers {
return reflect.Interface
}
// float64 if has float values or has both float and int values and has not bool
if hasFloat && !hasBool {
return reflect.Float64
}
// int64 if has only int values
if hasInt && !hasBool {
return reflect.Int64
}
// bool if has only bool values
if hasBool && !hasFloat && !hasInt {
return reflect.Bool
}
// otherwise, default interface
return reflect.Interface
}
func asType(arr []string, dtype reflect.Kind) []any {
switch dtype {
case reflect.Float64:
return asFloat64(arr)
case reflect.Int64:
return asInt64(arr)
case reflect.Bool:
return asBool(arr)
case reflect.Interface:
return asInterface(arr)
default:
panic("Unsupported type")
}
}
func asFloat64(arr []string) []any {
values := make([]any, 0, len(arr))
for _, val := range arr {
switch val {
case "", "NaN", "None", "null":
values = append(values, math.NaN())
case "Inf", "inf":
values = append(values, math.Inf(1))
case "-Inf", "-inf":
values = append(values, math.Inf(-1))
default:
if v, err := strconv.ParseFloat(val, 64); err != nil {
panic(err)
} else {
values = append(values, v)
}
}
}
return values
}
func asInt64(arr []string) []any {
values := make([]any, 0, len(arr))
for _, val := range arr {
if val == "" {
values = append(values, nil)
} else if v, err := strconv.ParseInt(val, 10, 64); err == nil {
values = append(values, v)
} else {
panic(err)
}
}
return values
}
func asBool(arr []string) []any {
values := make([]any, 0, len(arr))
for _, val := range arr {
switch val {
case "True", "true":
values = append(values, true)
case "False", "false":
values = append(values, false)
case "":
values = append(values, nil)
default:
panic("not a bool value: " + val)
}
}
return values
}
func asInterface(arr []string) []any {
values := make([]any, 0, len(arr))
for _, val := range arr {
if val == "" || val == "NaN" {
values = append(values, math.NaN())
continue
}
if val == "true" || val == "True" {
values = append(values, true)
continue
}
if val == "false" || val == "False" {
values = append(values, false)
continue
}
if val, err := strconv.ParseInt(val, 10, 64); err == nil {
values = append(values, val)
continue
}
if val, err := strconv.ParseFloat(val, 64); err == nil {
values = append(values, val)
continue
}
// default is string
values = append(values, val)
}
return values
}