-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataframe.go
612 lines (525 loc) · 14.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
package dataframe
import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
)
// DataFrame represents a data structure for storing tabular data
type DataFrame struct {
header []string
columns map[string][]interface{}
}
// NewDataFrame creates a new DataFrame with the given column names
func NewDataFrame(columnNames []string) (*DataFrame, error) {
if len(columnNames) == 0 {
return nil, errors.New("column names are required")
}
columns := make(map[string][]interface{})
for _, columnName := range columnNames {
if _, ok := columns[columnName]; ok {
return nil, fmt.Errorf("column name '%s' already exists", columnName)
}
columns[columnName] = []interface{}{}
}
return &DataFrame{
header: columnNames,
columns: columns,
}, nil
}
// AddColumn adds a new column to the DataFrame
func (df *DataFrame) AddColumn(name string, data []interface{}) error {
if len(data) != df.RowCount() {
return errors.New("data length does not match row count")
}
if _, ok := df.columns[name]; ok {
return fmt.Errorf("column name '%s' already exists", name)
}
df.columns[name] = data
return nil
}
// ModifyColumn modifies an existing column in the DataFrame
func (df *DataFrame) ModifyColumn(name string, data []interface{}) error {
if len(data) != df.RowCount() {
return errors.New("data length does not match row count")
}
if _, ok := df.columns[name]; !ok {
return fmt.Errorf("column '%s' does not exist", name)
}
df.columns[name] = data
return nil
}
// ChangeColumnOrder changes the order of columns in the DataFrame
func (df *DataFrame) ChangeColumnOrder(newOrder []string) error {
if len(newOrder) != len(df.header) {
return errors.New("invalid column order")
}
// Check if all new column names exist in the DataFrame
columnExists := make(map[string]bool)
for _, columnName := range df.header {
columnExists[columnName] = true
}
for _, columnName := range newOrder {
if _, ok := columnExists[columnName]; !ok {
return fmt.Errorf("column '%s' does not exist", columnName)
}
}
// Rearrange the columns based on the new order
newColumns := make(map[string][]interface{})
for _, columnName := range newOrder {
newColumns[columnName] = df.columns[columnName]
}
df.header = newOrder
df.columns = newColumns
return nil
}
// RowCount returns the number of rows in the DataFrame
func (df *DataFrame) RowCount() int {
if len(df.columns) == 0 {
return 0
}
return len(df.columns[df.header[0]])
}
// ColumnNames returns the names of the columns in the DataFrame
func (df *DataFrame) ColumnNames() []string {
return df.header
}
// PrintHeader prints the header of the DataFrame
func (df *DataFrame) PrintHeader() {
for _, columnName := range df.header {
fmt.Printf("%v\t", columnName)
}
fmt.Println()
}
// PrintData prints the data in the DataFrame
func (df *DataFrame) PrintData() {
for i := 0; i < df.RowCount(); i++ {
for _, columnName := range df.header {
fmt.Printf("%v\t", df.columns[columnName][i])
}
fmt.Println()
}
}
// Filter applies a filter to the DataFrame based on a given condition
func (df *DataFrame) Filter(condition func(row int) bool) (*DataFrame, error) {
filteredColumns := make(map[string][]interface{})
for columnName, columnData := range df.columns {
filteredColumns[columnName] = make([]interface{}, 0)
for i := 0; i < df.RowCount(); i++ {
if condition(i) {
filteredColumns[columnName] = append(filteredColumns[columnName], columnData[i])
}
}
}
if len(filteredColumns) == 0 {
return nil, errors.New("no columns matched the filter condition")
}
return &DataFrame{
header: df.header,
columns: filteredColumns,
}, nil
}
// Count returns the number of non-nil values in a column
func (df *DataFrame) Count(columnName string) (int, error) {
columnData, ok := df.columns[columnName]
if !ok {
return 0, fmt.Errorf("column '%s' does not exist", columnName)
}
count := 0
for _, value := range columnData {
if value != nil {
count++
}
}
return count, nil
}
// Sum returns the sum of values in a numeric column
func (df *DataFrame) Sum(columnName string) (float64, error) {
columnData, ok := df.columns[columnName]
if !ok {
return 0, fmt.Errorf("column '%s' does not exist", columnName)
}
sum := 0.0
for _, value := range columnData {
if numericValue, ok := value.(float64); ok {
sum += numericValue
} else {
return 0, fmt.Errorf("column '%s' is not numeric", columnName)
}
}
return sum, nil
}
// Mean returns the mean (average) of values in a numeric column
func (df *DataFrame) Mean(columnName string) (float64, error) {
count, err := df.Count(columnName)
if err != nil {
return 0, err
}
sum, err := df.Sum(columnName)
if err != nil {
return 0, err
}
if count == 0 {
return 0, errors.New("no values in column")
}
mean := sum / float64(count)
return mean, nil
}
// Sort sorts the DataFrame based on one or more columns in ascending or descending order
func (df *DataFrame) Sort(columns []string, ascending bool) error {
sort.SliceStable(df.columns[df.header[0]], func(i, j int) bool {
for _, col := range columns {
if df.columns[col][i] != df.columns[col][j] {
switch df.columns[col][i].(type) {
case int:
if ascending {
return df.columns[col][i].(int) < df.columns[col][j].(int)
} else {
return df.columns[col][i].(int) > df.columns[col][j].(int)
}
case float64:
if ascending {
return df.columns[col][i].(float64) < df.columns[col][j].(float64)
} else {
return df.columns[col][i].(float64) > df.columns[col][j].(float64)
}
case string:
if ascending {
return strings.Compare(df.columns[col][i].(string), df.columns[col][j].(string)) < 0
} else {
return strings.Compare(df.columns[col][i].(string), df.columns[col][j].(string)) > 0
}
}
}
}
return i < j
})
return nil
}
// GroupBy groups the DataFrame by one or more columns
func (df *DataFrame) GroupBy(columns []string) (*DataFrame, error) {
groupedColumns := make(map[string][]interface{})
groupedRowCount := make(map[string]int)
for _, col := range columns {
if _, ok := df.columns[col]; !ok {
return nil, fmt.Errorf("column '%s' does not exist", col)
}
groupedColumns[col] = []interface{}{}
groupedRowCount[col] = 0
}
for i := 0; i < df.RowCount(); i++ {
groupKey := ""
for _, col := range columns {
groupKey += fmt.Sprintf("%v-%v", col, df.columns[col][i])
}
if _, ok := groupedColumns[groupKey]; !ok {
for _, col := range columns {
groupedColumns[col] = append(groupedColumns[col], df.columns[col][i])
}
}
groupedRowCount[groupKey]++
}
for col, rowCount := range groupedRowCount {
for i := 0; i < df.RowCount(); i++ {
if groupedRowCount[fmt.Sprintf("%v-%v", col, df.columns[col][i])] > 0 {
groupedColumns[col] = append(groupedColumns[col], rowCount)
groupedRowCount[fmt.Sprintf("%v-%v", col, df.columns[col][i])] = 0
} else {
groupedColumns[col] = append(groupedColumns[col], nil)
}
}
}
return &DataFrame{
header: append(columns, "Count"),
columns: groupedColumns,
}, nil
}
// Join joins multiple DataFrames based on common columns
func Join(dataFrames []*DataFrame, joinColumns []string) (*DataFrame, error) {
if len(dataFrames) == 0 {
return nil, errors.New("no DataFrames provided for join")
}
joinedColumns := make(map[string][]interface{})
for _, df := range dataFrames {
for _, col := range df.header {
if _, ok := joinedColumns[col]; ok {
return nil, fmt.Errorf("column '%s' already exists in the join result", col)
}
joinedColumns[col] = df.columns[col]
}
}
for _, df := range dataFrames[1:] {
for i := 0; i < df.RowCount(); i++ {
rowMatch := make([]bool, len(dataFrames[0].header))
for j, col := range joinColumns {
if _, ok := df.columns[col]; !ok {
return nil, fmt.Errorf("column '%s' does not exist in DataFrame for join", col)
}
if _, ok := joinedColumns[col]; !ok {
return nil, fmt.Errorf("column '%s' does not exist in the join result", col)
}
if df.columns[col][i] != nil {
if idx := findIndex(joinedColumns[col], df.columns[col][i]); idx != -1 {
rowMatch[idx] = true
}
}
}
if allTrue(rowMatch) {
for _, col := range df.header {
if _, ok := joinedColumns[col]; !ok {
return nil, fmt.Errorf("column '%s' does not exist in the join result", col)
}
joinedColumns[col] = append(joinedColumns[col], df.columns[col][i])
}
}
}
}
return &DataFrame{
header: dataFrames[0].header,
columns: joinedColumns,
}, nil
}
// CleanData cleans the DataFrame by handling missing values, duplicates, and data type conversion
func (df *DataFrame) CleanData() error {
// Handle missing values
for columnName, columnData := range df.columns {
for i := 0; i < len(columnData); i++ {
if columnData[i] == nil {
switch df.columns[columnName][0].(type) {
case int:
df.columns[columnName][i] = 0
case float64:
df.columns[columnName][i] = 0.0
case string:
df.columns[columnName][i] = ""
default:
return fmt.Errorf("unknown data type in column '%s'", columnName)
}
}
}
}
// Handle duplicates
duplicateIndexes := make(map[int]bool)
for i := 0; i < df.RowCount(); i++ {
if duplicateIndexes[i] {
continue
}
for j := i + 1; j < df.RowCount(); j++ {
if duplicateIndexes[j] {
continue
}
isDuplicate := true
for _, columnName := range df.header {
if df.columns[columnName][i] != df.columns[columnName][j] {
isDuplicate = false
break
}
}
if isDuplicate {
duplicateIndexes[j] = true
}
}
}
for columnName, columnData := range df.columns {
cleanedColumn := make([]interface{}, 0)
for i := 0; i < len(columnData); i++ {
if !duplicateIndexes[i] {
cleanedColumn = append(cleanedColumn, columnData[i])
}
}
df.columns[columnName] = cleanedColumn
}
return nil
}
// Variance calculates the variance of values in a numeric column
func (df *DataFrame) Variance(columnName string) (float64, error) {
columnData, ok := df.columns[columnName]
if !ok {
return 0, fmt.Errorf("column '%s' does not exist", columnName)
}
count, err := df.Count(columnName)
if err != nil {
return 0, err
}
mean, err := df.Mean(columnName)
if err != nil {
return 0, err
}
if count <= 1 {
return 0, errors.New("insufficient data points for variance calculation")
}
variance := 0.0
for _, value := range columnData {
if numericValue, ok := value.(float64); ok {
variance += (numericValue - mean) * (numericValue - mean)
} else {
return 0, fmt.Errorf("column '%s' is not numeric", columnName)
}
}
variance /= float64(count - 1)
return variance, nil
}
// StandardDeviation calculates the standard deviation of values in a numeric column
func (df *DataFrame) StandardDeviation(columnName string) (float64, error) {
variance, err := df.Variance(columnName)
if err != nil {
return 0, err
}
standardDeviation := 0.0
if variance > 0 {
standardDeviation = �math.Sqrt(variance)
}
return standardDeviation, nil
}
// Correlation calculates the correlation coefficient between two numeric columns
func (df *DataFrame) Correlation(column1, column2 string) (float64, error) {
column1Data, ok1 := df.columns[column1]
column2Data, ok2 := df.columns[column2]
if !ok1 {
return 0, fmt.Errorf("column '%s' does not exist", column1)
}
if !ok2 {
return 0, fmt.Errorf("column '%s' does not exist", column2)
}
count, err := df.Count(column1)
if err != nil {
return 0, err
}
if count <= 1 {
return 0, errors.New("insufficient data points for correlation calculation")
}
var (
sumXY float64
sumX float64
sumY float64
sumXSquare float64
sumYSquare float64
)
for i := 0; i < df.RowCount(); i++ {
if value1, ok := column1Data[i].(float64); ok {
if value2, ok := column2Data[i].(float64); ok {
sumXY += value1 * value2
sumX += value1
sumY += value2
sumXSquare += value1 * value1
sumYSquare += value2 * value2
} else {
return 0, fmt.Errorf("column '%s' is not numeric", column2)
}
} else {
return 0, fmt.Errorf("column '%s' is not numeric", column1)
}
}
numerator := count*sumXY - sumX*sumY
denominator := math.Sqrt((count*sumXSquare - sumX*sumX) * (count*sumYSquare - sumY*sumY))
correlation := 0.0
if denominator != 0 {
correlation = numerator / denominator
}
return correlation, nil
}
// Covariance calculates the covariance between two numeric columns
func (df *DataFrame) Covariance(column1, column2 string) (float64, error) {
column1Data, ok1 := df.columns[column1]
column2Data, ok2 := df.columns[column2]
if !ok1 {
return 0, fmt.Errorf("column '%s' does not exist", column1)
}
if !ok2 {
return 0, fmt.Errorf("column '%s' does not exist", column2)
}
count, err := df.Count(column1)
if err != nil {
return 0, err
}
if count <= 1 {
return 0, errors.New("insufficient data points for covariance calculation")
}
var (
sumXY float64
sumX float64
sumY float64
)
for i := 0; i < df.RowCount(); i++ {
if value1, ok := column1Data[i].(float64); ok {
if value2, ok := column2Data[i].(float64); ok {
sumXY += value1 * value2
sumX += value1
sumY += value2
} else {
return 0, fmt.Errorf("column '%s' is not numeric", column2)
}
} else {
return 0, fmt.Errorf("column '%s' is not numeric", column1)
}
}
covariance := 0.0
if count > 1 {
meanX := sumX / float64(count)
meanY := sumY / float64(count)
covariance = (sumXY - float64(count)*meanX*meanY) / float64(count-1)
}
return covariance, nil
}
// SerializeToJSON serializes the DataFrame to a JSON string
func (df *DataFrame) SerializeToJSON() (string, error) {
jsonData := "[\n"
for i := 0; i < df.RowCount(); i++ {
jsonData += "\t{"
for j, columnName := range df.header {
jsonData += fmt.Sprintf("\"%s\":", columnName)
if value, ok := df.columns[columnName][i].(string); ok {
jsonData += "\"" + value + "\""
} else {
jsonData += fmt.Sprintf("%v", df.columns[columnName][i])
}
if j < len(df.header)-1 {
jsonData += ","
}
}
jsonData += "}"
if i < df.RowCount()-1 {
jsonData += ","
}
jsonData += "\n"
}
jsonData += "]\n"
return jsonData, nil
}
// SerializeToCSV serializes the DataFrame to a CSV string
func (df *DataFrame) SerializeToCSV() (string, error) {
csvData := strings.Join(df.header, ",") + "\n"
for i := 0; i < df.RowCount(); i++ {
for j, columnName := range df.header {
if value, ok := df.columns[columnName][i].(string); ok {
csvData += "\"" + value + "\""
} else {
csvData += fmt.Sprintf("%v", df.columns[columnName][i])
}
if j < len(df.header)-1 {
csvData += ","
}
}
csvData += "\n"
}
return csvData, nil
}
// findIndex returns the index of an element in a slice, or -1 if not found
func findIndex(slice []interface{}, element interface{}) int {
for i, value := range slice {
if value == element {
return i
}
}
return -1
}
// allTrue checks if all elements in a bool slice are true
func allTrue(slice []bool) bool {
for _, value := range slice {
if !value {
return false
}
}
return true
}