-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_csv.go
281 lines (231 loc) · 5.91 KB
/
io_csv.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
package gandalff
import (
"encoding/csv"
"fmt"
"os"
"io"
"github.com/caerbannogwhite/preludiometa"
)
type CsvReader struct {
header bool
rows int
delimiter rune
guessDataTypeLen int
path string
nullValues bool
reader io.Reader
schema *preludiometa.Schema
ctx *Context
}
func NewCsvReader(ctx *Context) *CsvReader {
return &CsvReader{
header: CSV_READER_DEFAULT_HEADER,
rows: -1,
delimiter: CSV_READER_DEFAULT_DELIMITER,
guessDataTypeLen: CSV_READER_DEFAULT_GUESS_DATA_TYPE_LEN,
path: "",
nullValues: false,
reader: nil,
schema: nil,
ctx: ctx,
}
}
func (r *CsvReader) SetHeader(header bool) *CsvReader {
r.header = header
return r
}
func (r *CsvReader) SetDelimiter(delimiter rune) *CsvReader {
r.delimiter = delimiter
return r
}
func (r *CsvReader) SetGuessDataTypeLen(guessDataTypeLen int) *CsvReader {
r.guessDataTypeLen = guessDataTypeLen
return r
}
func (r *CsvReader) SetRows(rows int) *CsvReader {
r.rows = rows
return r
}
func (r *CsvReader) SetPath(path string) *CsvReader {
r.path = path
return r
}
func (r *CsvReader) SetNullValues(nullValues bool) *CsvReader {
r.nullValues = nullValues
return r
}
func (r *CsvReader) SetReader(reader io.Reader) *CsvReader {
r.reader = reader
return r
}
func (r *CsvReader) SetSchema(schema *preludiometa.Schema) *CsvReader {
r.schema = schema
return r
}
func (r *CsvReader) SetContext(ctx *Context) *CsvReader {
r.ctx = ctx
return r
}
func (r *CsvReader) Read() DataFrame {
if r.path != "" {
file, err := os.OpenFile(r.path, os.O_RDONLY, 0666)
if err != nil {
return BaseDataFrame{err: err, ctx: r.ctx}
}
defer file.Close()
r.reader = file
}
if r.reader == nil {
return BaseDataFrame{err: fmt.Errorf("CsvReader: no reader specified"), ctx: r.ctx}
}
if r.ctx == nil {
return BaseDataFrame{err: fmt.Errorf("CsvReader: no context specified"), ctx: r.ctx}
}
names, series, err := readCsv(r.reader, r.delimiter, r.header, r.rows, r.nullValues, r.guessDataTypeLen, r.schema, r.ctx)
if err != nil {
return BaseDataFrame{err: err, ctx: r.ctx}
}
df := NewBaseDataFrame(r.ctx)
for i, name := range names {
df = df.AddSeries(name, series[i])
}
return df
}
// ReadCsv reads a CSV file and returns a GDLDataFrame.
func readCsv(
reader io.Reader, delimiter rune, header bool, rows int, nullValues bool,
guessDataTypeLen int, schema *preludiometa.Schema, ctx *Context,
) ([]string, []Series, error) {
// TODO: Add support for Time and Duration types (defined in a schema)
// TODO: Optimize null masks (use bit vectors)?
// TODO: Try to optimize this function by using goroutines: read the rows (like 1000)
// and guess the data types in parallel
if ctx == nil {
return nil, nil, fmt.Errorf("readCsv: no context specified")
}
// Initialize CSV reader
csvReader := csv.NewReader(reader)
csvReader.Comma = delimiter
csvReader.FieldsPerRecord = -1
// Read header if present
var names []string
var err error
if header {
names, err = csvReader.Read()
if err != nil {
return nil, nil, err
}
}
series, err := readRowData(csvReader, nullValues, guessDataTypeLen, rows, schema, ctx)
if err != nil {
return nil, nil, err
}
// Generate names if not present
if !header {
for i := 0; i < len(series); i++ {
names = append(names, fmt.Sprintf("Column %d", i+1))
}
}
return names, series, nil
}
type CsvWriter struct {
delimiter rune
header bool
format bool // TODO: Implement this
path string
naText string
writer io.Writer
dataframe DataFrame
}
func NewCsvWriter() *CsvWriter {
return &CsvWriter{
delimiter: CSV_READER_DEFAULT_DELIMITER,
header: CSV_READER_DEFAULT_HEADER,
format: true,
path: "",
naText: NA_TEXT,
writer: nil,
dataframe: nil,
}
}
func (w *CsvWriter) SetDelimiter(delimiter rune) *CsvWriter {
w.delimiter = delimiter
return w
}
func (w *CsvWriter) SetHeader(header bool) *CsvWriter {
w.header = header
return w
}
func (w *CsvWriter) SetFormat(format bool) *CsvWriter {
w.format = format
return w
}
func (w *CsvWriter) SetPath(path string) *CsvWriter {
w.path = path
return w
}
func (w *CsvWriter) SetNaText(naText string) *CsvWriter {
w.naText = naText
return w
}
func (w *CsvWriter) SetWriter(writer io.Writer) *CsvWriter {
w.writer = writer
return w
}
func (w *CsvWriter) SetDataFrame(dataframe DataFrame) *CsvWriter {
w.dataframe = dataframe
return w
}
func (w *CsvWriter) Write() DataFrame {
if w.dataframe == nil {
return BaseDataFrame{err: fmt.Errorf("CsvWriter: no dataframe specified"), ctx: w.dataframe.GetContext()}
}
if w.dataframe.IsErrored() {
return w.dataframe
}
if w.path != "" {
file, err := os.OpenFile(w.path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return BaseDataFrame{err: err, ctx: w.dataframe.GetContext()}
}
defer file.Close()
w.writer = file
}
if w.writer == nil {
return BaseDataFrame{err: fmt.Errorf("CsvWriter: no writer specified"), ctx: w.dataframe.GetContext()}
}
err := writeCsv(w.dataframe, w.writer, w.delimiter, w.header, w.format, w.naText)
if err != nil {
w.dataframe = BaseDataFrame{err: err, ctx: w.dataframe.GetContext()}
}
return w.dataframe
}
func writeCsv(df DataFrame, writer io.Writer, delimiter rune, header bool, format bool, naText string) error {
series := make([]Series, df.NCols())
for i := 0; i < df.NCols(); i++ {
series[i] = df.At(i)
}
if header {
for i, name := range df.Names() {
if i > 0 {
fmt.Fprintf(writer, "%c", delimiter)
}
fmt.Fprintf(writer, "%s", name)
}
fmt.Fprintf(writer, "\n")
}
for i := 0; i < df.NRows(); i++ {
for j, s := range series {
if j > 0 {
fmt.Fprintf(writer, "%c", delimiter)
}
if s.IsNull(i) {
fmt.Fprintf(writer, "%s", naText)
} else {
fmt.Fprintf(writer, "%s", s.GetAsString(i))
}
}
fmt.Fprintf(writer, "\n")
}
return nil
}