-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
dataframe.go
624 lines (515 loc) · 13.9 KB
/
dataframe.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package dataframe
import (
"context"
"errors"
"golang.org/x/exp/rand"
"golang.org/x/sync/errgroup"
"sync"
)
// DataFrame allows you to handle numerous
//series of data conveniently.
type DataFrame struct {
lock sync.RWMutex
Series []Series
n int // Number of rows
}
// NewDataFrame creates a new dataframe.
func NewDataFrame(se ...Series) *DataFrame {
df := &DataFrame{
Series: []Series{},
}
if len(se) > 0 {
var count *int
names := map[string]struct{}{}
for _, s := range se {
if count == nil {
count = &[]int{s.NRows()}[0]
names[s.Name()] = struct{}{}
} else {
if *count != s.NRows() {
panic("different number of rows in series")
}
if _, exists := names[s.Name()]; exists {
panic("names of series must be unique: " + s.Name())
}
names[s.Name()] = struct{}{}
}
df.Series = append(df.Series, s)
}
df.n = *count
}
return df
}
// NRows returns the number of rows of data.
// Each series must contain the same number of rows.
func (df *DataFrame) NRows(opts ...Options) int {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.RLock()
defer df.lock.RUnlock()
}
return df.n
}
// SeriesReturnOpt is used to control if Row/Values method returns
// the series index, series name or both as map keys.
type SeriesReturnOpt uint8
func (s SeriesReturnOpt) has(x SeriesReturnOpt) bool {
return s&x != 0
}
const (
// SeriesIdx forces the series' index to be returned as a map key.
SeriesIdx SeriesReturnOpt = 1 << iota
// SeriesName forces the series' name to be returned as a map key.
SeriesName
)
// Row returns the series' values for a particular row.
//
// Example:
//
// df.Row(5, false, dataframe.SeriesIdx|dataframe.SeriesName)
//
func (df *DataFrame) Row(row int, dontReadLock bool, retOpt ...SeriesReturnOpt) map[interface{}]interface{} {
if !dontReadLock {
df.lock.RLock()
defer df.lock.RUnlock()
}
out := map[interface{}]interface{}{}
for idx, aSeries := range df.Series {
val := aSeries.Value(row)
if len(retOpt) == 0 || retOpt[0].has(SeriesIdx) {
out[idx] = val
}
if len(retOpt) == 0 || retOpt[0].has(SeriesName) {
out[aSeries.Name()] = val
}
}
return out
}
// ValuesOptions is used to modify the behavior of Values().
type ValuesOptions struct {
// InitialRow represents the starting value for iterating.
// Negative values are acceptable. A value of -2 means the second last row.
InitialRow int
// Step represents by how much each iteration should step by.
// It can be negative to represent iterating in reverse direction.
// InitialRow should be adjusted to -1 if Step is negative.
// If Step is 0, the function will panic.
Step int
// Don't apply read lock. This is useful if you intend to Write lock
// the entire dataframe.
DontReadLock bool
}
// ValuesIterator will return a function that can be used to iterate through all the values.
//
// The returned value is a map containing the name of the series (string) and the index of the series (int) as keys.
// You can reduce the keys in the map to only contain the series name (SeriesName) or series index (SeriesIdx).
//
// Example:
//
// iterator := df.ValuesIterator(dataframe.ValuesOptions{0, 1, true})
//
// df.Lock()
// for {
// row, vals, _ := iterator(dataframe.SeriesName)
// if row == nil {
// break
// }
// fmt.Println(*row, vals)
// }
// df.Unlock()
//
// // OR
//
// df.Lock()
// row, vals, _ := iterator()
// for ; row != nil; row, vals, _ = iterator() {
// fmt.Println(*row, vals)
// }
// df.Unlock()
//
func (df *DataFrame) ValuesIterator(opts ...ValuesOptions) func(opts ...SeriesReturnOpt) (*int, map[interface{}]interface{}, int) {
var (
row int
step int = 1
)
var dontReadLock bool
if len(opts) > 0 {
dontReadLock = opts[0].DontReadLock
row = opts[0].InitialRow
if row < 0 {
row = df.n + row
}
if opts[0].Step != 0 {
step = opts[0].Step
}
}
initial := row
return func(opts ...SeriesReturnOpt) (*int, map[interface{}]interface{}, int) {
if !dontReadLock {
df.lock.RLock()
defer df.lock.RUnlock()
}
var t int
if step > 0 {
t = (df.n-initial-1)/step + 1
} else {
t = -initial/step + 1
}
if row > df.n-1 || row < 0 {
// Don't iterate further
return nil, nil, t
}
out := map[interface{}]interface{}{}
for idx, aSeries := range df.Series {
val := aSeries.Value(row)
if len(opts) == 0 || opts[0].has(SeriesIdx) {
out[idx] = val
}
if len(opts) == 0 || opts[0].has(SeriesName) {
out[aSeries.Name()] = val
}
}
row = row + step
return &[]int{row - step}[0], out, t
}
}
// Prepend inserts a row at the beginning.
func (df *DataFrame) Prepend(opts *Options, vals ...interface{}) {
df.Insert(0, opts, vals...)
}
// Append inserts a row at the end.
func (df *DataFrame) Append(opts *Options, vals ...interface{}) {
df.Insert(df.n, opts, vals...)
}
// Insert adds a row to a particular position.
func (df *DataFrame) Insert(row int, opts *Options, vals ...interface{}) {
if opts == nil || !opts.DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
df.insert(row, vals...)
}
func (df *DataFrame) insert(row int, vals ...interface{}) {
if len(vals) > 0 {
switch v := vals[0].(type) {
case map[string]interface{}:
// Check if number of vals is equal to number of series
if len(v) != len(df.Series) {
panic("no. of args not equal to no. of series")
}
for name, val := range v {
col, err := df.NameToColumn(name, dontLock)
if err != nil {
panic(err)
}
df.Series[col].Insert(row, val)
}
case map[interface{}]interface{}:
// Check if number of vals is equal to number of series
names := map[string]struct{}{}
for key := range v {
switch kTyp := key.(type) {
case int:
names[df.Series[kTyp].Name(dontLock)] = struct{}{}
case string:
names[kTyp] = struct{}{}
default:
panic("unknown type in insert argument. Must be an int or string.")
}
}
if len(names) != len(df.Series) {
panic("no. of args not equal to no. of series")
}
for C, val := range v {
switch CTyp := C.(type) {
case int:
df.Series[CTyp].Insert(row, val)
case string:
col, err := df.NameToColumn(CTyp, dontLock)
if err != nil {
panic(err)
}
df.Series[col].Insert(row, val)
default:
panic("unknown type in insert argument. Must be an int or string.")
}
}
default:
// Check if number of vals is equal to number of series
if len(vals) != len(df.Series) {
panic("no. of args not equal to no. of series")
}
for idx, val := range vals {
df.Series[idx].Insert(row, val)
}
}
df.n++
}
}
// ClearRow makes an entire row nil.
func (df *DataFrame) ClearRow(row int, opts ...Options) {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
for i := range df.Series {
df.Series[i].Update(row, nil, dontLock) //???
}
}
// Remove deletes a row.
func (df *DataFrame) Remove(row int, opts ...Options) {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
for i := range df.Series {
df.Series[i].Remove(row)
}
df.n--
}
// Update is used to update a specific entry.
// col can be the name of the series or the column number.
func (df *DataFrame) Update(row int, col interface{}, val interface{}, opts ...Options) {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
switch name := col.(type) {
case string:
_col, err := df.NameToColumn(name, dontLock)
if err != nil {
panic(err)
}
col = _col
}
df.Series[col.(int)].Update(row, val)
}
// UpdateRow will update an entire row.
func (df *DataFrame) UpdateRow(row int, opts *Options, vals ...interface{}) {
if opts == nil || !opts.DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
if len(vals) > 0 {
switch v := vals[0].(type) {
case map[string]interface{}:
for name, val := range v {
col, err := df.NameToColumn(name, dontLock)
if err != nil {
panic(err)
}
df.Series[col].Update(row, val)
}
case map[interface{}]interface{}:
for C, val := range v {
switch CTyp := C.(type) {
case int:
df.Series[CTyp].Update(row, val)
case string:
col, err := df.NameToColumn(CTyp, dontLock)
if err != nil {
panic(err)
}
df.Series[col].Update(row, val)
default:
panic("unknown type in UpdateRow argument. Must be an int or string.")
}
}
default:
// Check if number of vals is equal to number of series
if len(vals) != len(df.Series) {
panic("no. of args not equal to no. of series")
}
for idx, val := range vals {
df.Series[idx].Update(row, val)
}
}
}
}
// Names will return a list of all the series names.
func (df *DataFrame) Names(opts ...Options) []string {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.RLock()
defer df.lock.RUnlock()
}
names := []string{}
for _, aSeries := range df.Series {
names = append(names, aSeries.Name())
}
return names
}
// MustNameToColumn returns the index of the series based on the name.
// The starting index is 0. If seriesName doesn't exist it panics.
func (df *DataFrame) MustNameToColumn(seriesName string, opts ...Options) int {
col, err := df.NameToColumn(seriesName, opts...)
if err != nil {
panic(err)
}
return col
}
// NameToColumn returns the index of the series based on the name.
// The starting index is 0.
func (df *DataFrame) NameToColumn(seriesName string, opts ...Options) (int, error) {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.RLock()
defer df.lock.RUnlock()
}
for idx, aSeries := range df.Series {
if aSeries.Name() == seriesName {
return idx, nil
}
}
return 0, errors.New("no series contains name")
}
// ReorderColumns reorders the columns based on an ordered list of
// column names. The length of newOrder must match the number of columns
// in the Dataframe. The column names in newOrder must be unique.
func (df *DataFrame) ReorderColumns(newOrder []string, opts ...Options) error {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
if len(newOrder) != len(df.Series) {
return errors.New("length of newOrder must match number of columns")
}
// Check if newOrder contains duplicates
fields := map[string]struct{}{}
for _, v := range newOrder {
fields[v] = struct{}{}
}
if len(fields) != len(df.Series) {
return errors.New("newOrder must not contain duplicate values")
}
series := []Series{}
for _, v := range newOrder {
idx, err := df.NameToColumn(v, dontLock)
if err != nil {
return errors.New(err.Error() + ": " + v)
}
series = append(series, df.Series[idx])
}
df.Series = series
return nil
}
// RemoveSeries will remove a Series from the Dataframe.
func (df *DataFrame) RemoveSeries(seriesName string, opts ...Options) error {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
idx, err := df.NameToColumn(seriesName, dontLock)
if err != nil {
return errors.New(err.Error() + ": " + seriesName)
}
df.Series = append(df.Series[:idx], df.Series[idx+1:]...)
return nil
}
// AddSeries will add a Series to the end of the DataFrame, unless set by ColN.
func (df *DataFrame) AddSeries(s Series, colN *int, opts ...Options) error {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
if s.NRows(dontLock) != df.n {
panic("different number of rows in series")
}
if colN == nil {
df.Series = append(df.Series, s)
} else {
df.Series = append(df.Series, nil)
copy(df.Series[*colN+1:], df.Series[*colN:])
df.Series[*colN] = s
}
return nil
}
// Swap is used to swap 2 values based on their row position.
func (df *DataFrame) Swap(row1, row2 int, opts ...Options) {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.Lock()
defer df.lock.Unlock()
}
for idx := range df.Series {
df.Series[idx].Swap(row1, row2)
}
}
// Lock will lock the Dataframe allowing you to directly manipulate
// the underlying Series with confidence.
func (df *DataFrame) Lock(deepLock ...bool) {
df.lock.Lock()
if len(deepLock) > 0 && deepLock[0] {
for i := range df.Series {
df.Series[i].Lock()
}
}
}
// Unlock will unlock the Dataframe that was previously locked.
func (df *DataFrame) Unlock(deepUnlock ...bool) {
if len(deepUnlock) > 0 && deepUnlock[0] {
for i := range df.Series {
df.Series[i].Unlock()
}
}
df.lock.Unlock()
}
// Copy will create a new copy of the Dataframe.
// It is recommended that you lock the Dataframe
// before attempting to Copy.
func (df *DataFrame) Copy(r ...Range) *DataFrame {
if len(r) == 0 {
r = append(r, Range{})
}
seriess := []Series{}
for i := range df.Series {
seriess = append(seriess, df.Series[i].Copy(r...))
}
newDF := &DataFrame{
Series: seriess,
}
if len(seriess) > 0 {
newDF.n = seriess[0].NRows(dontLock)
}
return newDF
}
// FillRand will randomly fill all the Series in the Dataframe.
func (df *DataFrame) FillRand(src rand.Source, probNil float64, rander Rander, opts ...FillRandOptions) {
for _, s := range df.Series {
if sfr, ok := s.(FillRander); ok {
sfr.FillRand(src, probNil, rander, opts...)
}
}
}
var errNotEqual = errors.New("not equal")
// IsEqual returns true if df2's values are equal to df.
func (df *DataFrame) IsEqual(ctx context.Context, df2 *DataFrame, opts ...IsEqualOptions) (bool, error) {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.RLock()
defer df.lock.RUnlock()
}
// Check if number of columns are the same
if len(df.Series) != len(df2.Series) {
return false, nil
}
// Check values
g, newCtx := errgroup.WithContext(ctx)
for i := range df.Series {
i := i
g.Go(func() error {
eq, err := df.Series[i].IsEqual(newCtx, df2.Series[i], opts...)
if err != nil {
return err
}
if !eq {
return errNotEqual
}
return nil
})
}
err := g.Wait()
if err != nil {
if err == errNotEqual {
return false, nil
}
return false, err
}
return true, nil
}