-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
dataframe_fmt.go
181 lines (141 loc) · 3.82 KB
/
dataframe_fmt.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package dataframe
import (
"bytes"
"fmt"
"github.com/olekukonko/tablewriter"
)
// TableOptions can be used to limit the number of rows and which Series
// are used when generating the table.
type TableOptions struct {
// Series is used to display a given set of Series. When nil (default), all Series are displayed.
// An index of the Series or the name of the Series can be provided.
//
// NOTE: This option only applies to DataFrames.
//
// Example:
//
// opts := TableOptions{Series: []interface{}{1, "time"}}
//
Series []interface{}
// R is used to limit the range of rows.
R *Range
// DontLock can be set to true if the DataFrame or Series should not be locked.
DontLock bool
}
// Table will produce the DataFrame in a table.
func (df *DataFrame) Table(opts ...TableOptions) string {
if len(opts) == 0 || !opts[0].DontLock {
df.lock.RLock()
defer df.lock.RUnlock()
}
if len(opts) == 0 {
opts = append(opts, TableOptions{R: &Range{}})
} else if opts[0].R == nil {
opts[0].R = &Range{}
}
columns := map[interface{}]struct{}{}
for _, v := range opts[0].Series {
columns[v] = struct{}{}
}
data := [][]string{}
headers := []string{""} // row header is blank
footers := []string{fmt.Sprintf("%dx%d", df.n, len(df.Series))}
for idx, aSeries := range df.Series {
if len(columns) == 0 {
headers = append(headers, aSeries.Name())
footers = append(footers, aSeries.Type())
} else {
// Check idx
_, exists := columns[idx]
if exists {
headers = append(headers, aSeries.Name())
footers = append(footers, aSeries.Type())
continue
}
// Check series name
_, exists = columns[aSeries.Name()]
if exists {
headers = append(headers, aSeries.Name())
footers = append(footers, aSeries.Type())
continue
}
}
}
if df.n > 0 {
s, e, err := opts[0].R.Limits(df.n)
if err != nil {
panic(err)
}
for row := s; row <= e; row++ {
sVals := []string{fmt.Sprintf("%d:", row)}
for idx, aSeries := range df.Series {
if len(columns) == 0 {
sVals = append(sVals, aSeries.ValueString(row))
} else {
// Check idx
_, exists := columns[idx]
if exists {
sVals = append(sVals, aSeries.ValueString(row))
continue
}
// Check series name
_, exists = columns[aSeries.Name()]
if exists {
sVals = append(sVals, aSeries.ValueString(row))
continue
}
}
}
data = append(data, sVals)
}
}
var buf bytes.Buffer
table := tablewriter.NewWriter(&buf)
table.SetHeader(headers)
for _, v := range data {
table.Append(v)
}
table.SetFooter(footers)
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.Render()
return buf.String()
}
// String implements the fmt.Stringer interface. It does not lock the DataFrame.
func (df *DataFrame) String() string {
if df.NRows() <= 6 {
return df.Table(TableOptions{DontLock: true})
}
idx := []int{0, 1, 2, df.n - 3, df.n - 2, df.n - 1}
data := [][]string{}
headers := []string{""} // row header is blank
footers := []string{fmt.Sprintf("%dx%d", df.n, len(df.Series))}
for _, aSeries := range df.Series {
headers = append(headers, aSeries.Name())
footers = append(footers, aSeries.Type())
}
for j, row := range idx {
if j == 3 {
sVals := []string{"⋮"}
for range df.Series {
sVals = append(sVals, "⋮")
}
data = append(data, sVals)
}
sVals := []string{fmt.Sprintf("%d:", row)}
for _, aSeries := range df.Series {
sVals = append(sVals, aSeries.ValueString(row))
}
data = append(data, sVals)
}
var buf bytes.Buffer
table := tablewriter.NewWriter(&buf)
table.SetHeader(headers)
for _, v := range data {
table.Append(v)
}
table.SetFooter(footers)
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.Render()
return buf.String()
}