-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathapply.go
188 lines (144 loc) · 4.16 KB
/
apply.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
184
185
186
187
188
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package dataframe
import (
"context"
)
// ApplyDataFrameFn is used by the Apply function when used with DataFrames.
// vals contains the values for the current row. The keys contain ints (index of Series) and strings (name of Series).
// The returned map must only contain what values you intend to update. The key can be a string (name of Series) or int (index of Series).
// If nil is returned, the existing values for the row are unchanged.
type ApplyDataFrameFn func(vals map[interface{}]interface{}, row, nRows int) map[interface{}]interface{}
// ApplySeriesFn is used by the Apply function when used with Series.
// val contains the value of the current row. The returned value is the updated value.
type ApplySeriesFn func(val interface{}, row, nRows int) interface{}
// Apply will call fn for each row in the Series or DataFrame and replace the existing value with the new
// value returned by fn. When sdf is a DataFrame, fn must be of type ApplyDataFrameFn. When sdf is a Series, fn must be of type ApplySeriesFn.
func Apply(ctx context.Context, sdf interface{}, fn interface{}, opts ...FilterOptions) (interface{}, error) {
switch typ := sdf.(type) {
case Series:
var x ApplySeriesFn
switch v := fn.(type) {
case func(val interface{}, row, nRows int) interface{}:
x = ApplySeriesFn(v)
default:
x = fn.(ApplySeriesFn)
}
s, err := applySeries(ctx, typ, x, opts...)
if s == nil {
return nil, err
}
return s, err
case *DataFrame:
var x ApplyDataFrameFn
switch v := fn.(type) {
case func(vals map[interface{}]interface{}, row, nRows int) map[interface{}]interface{}:
x = ApplyDataFrameFn(v)
default:
x = fn.(ApplyDataFrameFn)
}
df, err := applyDataFrame(ctx, typ, x, opts...)
if df == nil {
return nil, err
}
return df, err
default:
panic("sdf must be a Series or DataFrame")
}
return nil, nil
}
func applySeries(ctx context.Context, s Series, fn ApplySeriesFn, opts ...FilterOptions) (Series, error) {
if fn == nil {
panic("fn is required")
}
if len(opts) == 0 {
opts = append(opts, FilterOptions{})
}
if !opts[0].DontLock {
s.Lock()
defer s.Unlock()
}
nRows := s.NRows(dontLock)
var ns Series
if !opts[0].InPlace {
x, ok := s.(NewSerieser)
if !ok {
panic("s must implement NewSerieser interface if InPlace is false")
}
// Create a New Series
ns = x.NewSeries(s.Name(dontLock), &SeriesInit{Capacity: nRows})
}
iterator := s.ValuesIterator(ValuesOptions{InitialRow: 0, Step: 1, DontReadLock: true})
for {
if err := ctx.Err(); err != nil {
return nil, err
}
row, val, nRows := iterator()
if row == nil {
break
}
newVal := fn(val, *row, nRows)
if opts[0].InPlace {
s.Update(*row, newVal, dontLock)
} else {
ns.Append(newVal, dontLock)
}
}
if !opts[0].InPlace {
return ns, nil
}
return nil, nil
}
func applyDataFrame(ctx context.Context, df *DataFrame, fn ApplyDataFrameFn, opts ...FilterOptions) (*DataFrame, error) {
if fn == nil {
panic("fn is required")
}
if len(opts) == 0 {
opts = append(opts, FilterOptions{})
}
if !opts[0].DontLock {
df.Lock()
defer df.Unlock()
}
nRows := df.n
var ndf *DataFrame
if !opts[0].InPlace {
// Create all series
seriess := []Series{}
for i := range df.Series {
s := df.Series[i]
x, ok := s.(NewSerieser)
if !ok {
panic("all Series in DataFrame must implement NewSerieser interface if InPlace is false")
}
seriess = append(seriess, x.NewSeries(df.Series[i].Name(dontLock), &SeriesInit{Capacity: nRows}))
}
// Create a new dataframe
ndf = NewDataFrame(seriess...)
}
iterator := df.ValuesIterator(ValuesOptions{InitialRow: 0, Step: 1, DontReadLock: true})
for {
if err := ctx.Err(); err != nil {
return nil, err
}
row, vals, nRows := iterator()
if row == nil {
break
}
newVals := fn(vals, *row, nRows)
if opts[0].InPlace {
if newVals != nil {
df.UpdateRow(*row, &dontLock, newVals)
}
} else {
if newVals != nil {
ndf.Append(&dontLock, newVals)
} else {
ndf.Append(&dontLock, vals)
}
}
}
if !opts[0].InPlace {
return ndf, nil
}
return nil, nil
}