@@ -57,7 +57,6 @@ func (df *DataFrame) NRows(options ...Options) int {
57
57
}
58
58
59
59
return df .n
60
-
61
60
}
62
61
63
62
// SeriesReturnOpt is used to control if Row/Values method returns
@@ -187,56 +186,21 @@ func (df *DataFrame) ValuesIterator(options ...ValuesOptions) func(retOpt ...Ser
187
186
}
188
187
189
188
// Prepend inserts a row at the beginning.
190
- func (df * DataFrame ) Prepend (vals ... interface {}) {
191
- df .lock .Lock ()
192
- defer df .lock .Unlock ()
193
-
194
- if len (vals ) > 0 {
195
-
196
- switch v := vals [0 ].(type ) {
197
- case map [string ]interface {}:
198
-
199
- names := map [string ]struct {}{}
200
- for name := range v {
201
- names [name ] = struct {}{}
202
- }
203
-
204
- // Check if number of vals is equal to number of series
205
- if len (names ) != len (df .Series ) {
206
- panic ("no. of args not equal to no. of series" )
207
- }
208
-
209
- for name , val := range v {
210
- col , err := df .NameToColumn (name )
211
- if err != nil {
212
- panic (err )
213
- }
214
- df .Series [col ].Prepend (val )
215
- }
216
- default :
217
- // Check if number of vals is equal to number of series
218
- if len (vals ) != len (df .Series ) {
219
- panic ("no. of args not equal to no. of series" )
220
- }
221
-
222
- for idx , val := range vals {
223
- df .Series [idx ].Prepend (val )
224
- }
225
- }
226
-
227
- df .n ++
228
- }
189
+ func (df * DataFrame ) Prepend (opts * Options , vals ... interface {}) {
190
+ df .Insert (0 , opts , vals ... )
229
191
}
230
192
231
193
// Append inserts a row at the end.
232
- func (df * DataFrame ) Append (vals ... interface {}) {
233
- df .Insert (df .n , vals ... )
194
+ func (df * DataFrame ) Append (opts * Options , vals ... interface {}) {
195
+ df .Insert (df .n , opts , vals ... )
234
196
}
235
197
236
198
// Insert adds a row to a particular position.
237
- func (df * DataFrame ) Insert (row int , vals ... interface {}) {
238
- df .lock .Lock ()
239
- defer df .lock .Unlock ()
199
+ func (df * DataFrame ) Insert (row int , opts * Options , vals ... interface {}) {
200
+ if opts == nil || ! opts .DontLock {
201
+ df .lock .Lock ()
202
+ defer df .lock .Unlock ()
203
+ }
240
204
241
205
df .insert (row , vals ... )
242
206
}
@@ -248,26 +212,35 @@ func (df *DataFrame) insert(row int, vals ...interface{}) {
248
212
switch v := vals [0 ].(type ) {
249
213
case map [string ]interface {}:
250
214
251
- names := map [string ]struct {}{}
252
- for name := range v {
253
- names [name ] = struct {}{}
254
- }
255
-
256
215
// Check if number of vals is equal to number of series
257
- if len (names ) != len (df .Series ) {
216
+ if len (v ) != len (df .Series ) {
258
217
panic ("no. of args not equal to no. of series" )
259
218
}
260
219
261
220
for name , val := range v {
262
- col , err := df .NameToColumn (name )
221
+ col , err := df .NameToColumn (name , dontLock )
263
222
if err != nil {
264
223
panic (err )
265
224
}
266
225
df .Series [col ].Insert (row , val )
267
226
}
268
227
case map [interface {}]interface {}:
269
228
270
- if len (v ) != len (df .Series ) {
229
+ // Check if number of vals is equal to number of series
230
+ names := map [string ]struct {}{}
231
+
232
+ for key := range v {
233
+ switch kTyp := key .(type ) {
234
+ case int :
235
+ names [df .Series [kTyp ].Name (dontLock )] = struct {}{}
236
+ case string :
237
+ names [kTyp ] = struct {}{}
238
+ default :
239
+ panic ("unknown type in insert argument. Must be an int or string." )
240
+ }
241
+ }
242
+
243
+ if len (names ) != len (df .Series ) {
271
244
panic ("no. of args not equal to no. of series" )
272
245
}
273
246
@@ -276,7 +249,7 @@ func (df *DataFrame) insert(row int, vals ...interface{}) {
276
249
case int :
277
250
df .Series [CTyp ].Insert (row , val )
278
251
case string :
279
- col , err := df .NameToColumn (CTyp )
252
+ col , err := df .NameToColumn (CTyp , dontLock )
280
253
if err != nil {
281
254
panic (err )
282
255
}
@@ -301,19 +274,23 @@ func (df *DataFrame) insert(row int, vals ...interface{}) {
301
274
}
302
275
303
276
// ClearRow makes an entire row nil.
304
- func (df * DataFrame ) ClearRow (row int ) {
305
- df .lock .Lock ()
306
- defer df .lock .Unlock ()
277
+ func (df * DataFrame ) ClearRow (row int , opts ... Options ) {
278
+ if len (opts ) == 0 || (len (opts ) > 0 && ! opts [0 ].DontLock ) {
279
+ df .lock .Lock ()
280
+ defer df .lock .Unlock ()
281
+ }
307
282
308
283
for i := range df .Series {
309
284
df .Series [i ].Update (row , nil , dontLock ) //???
310
285
}
311
286
}
312
287
313
288
// Remove deletes a row.
314
- func (df * DataFrame ) Remove (row int ) {
315
- df .lock .Lock ()
316
- defer df .lock .Unlock ()
289
+ func (df * DataFrame ) Remove (row int , opts ... Options ) {
290
+ if len (opts ) == 0 || (len (opts ) > 0 && ! opts [0 ].DontLock ) {
291
+ df .lock .Lock ()
292
+ defer df .lock .Unlock ()
293
+ }
317
294
318
295
for i := range df .Series {
319
296
df .Series [i ].Remove (row )
@@ -323,15 +300,15 @@ func (df *DataFrame) Remove(row int) {
323
300
324
301
// Update is used to update a specific entry.
325
302
// col can be the name of the series or the column number.
326
- func (df * DataFrame ) Update (row int , col interface {}, val interface {}, options ... Options ) {
327
- if len (options ) == 0 || (len (options ) > 0 && ! options [0 ].DontLock ) {
303
+ func (df * DataFrame ) Update (row int , col interface {}, val interface {}, opts ... Options ) {
304
+ if len (opts ) == 0 || (len (opts ) > 0 && ! opts [0 ].DontLock ) {
328
305
df .lock .Lock ()
329
306
defer df .lock .Unlock ()
330
307
}
331
308
332
309
switch name := col .(type ) {
333
310
case string :
334
- _col , err := df .NameToColumn (name )
311
+ _col , err := df .NameToColumn (name , dontLock )
335
312
if err != nil {
336
313
panic (err )
337
314
}
@@ -342,16 +319,18 @@ func (df *DataFrame) Update(row int, col interface{}, val interface{}, options .
342
319
}
343
320
344
321
// UpdateRow will update an entire row.
345
- func (df * DataFrame ) UpdateRow (row int , vals ... interface {}) {
346
- df .lock .Lock ()
347
- defer df .lock .Unlock ()
322
+ func (df * DataFrame ) UpdateRow (row int , opts * Options , vals ... interface {}) {
323
+ if opts == nil || ! opts .DontLock {
324
+ df .lock .Lock ()
325
+ defer df .lock .Unlock ()
326
+ }
348
327
349
328
if len (vals ) > 0 {
350
329
351
330
switch v := vals [0 ].(type ) {
352
331
case map [string ]interface {}:
353
332
for name , val := range v {
354
- col , err := df .NameToColumn (name )
333
+ col , err := df .NameToColumn (name , dontLock )
355
334
if err != nil {
356
335
panic (err )
357
336
}
@@ -363,7 +342,7 @@ func (df *DataFrame) UpdateRow(row int, vals ...interface{}) {
363
342
case int :
364
343
df .Series [CTyp ].Update (row , val )
365
344
case string :
366
- col , err := df .NameToColumn (CTyp )
345
+ col , err := df .NameToColumn (CTyp , dontLock )
367
346
if err != nil {
368
347
panic (err )
369
348
}
@@ -386,9 +365,13 @@ func (df *DataFrame) UpdateRow(row int, vals ...interface{}) {
386
365
}
387
366
388
367
// Names will return a list of all the series names.
389
- func (df * DataFrame ) Names () []string {
390
- names := []string {}
368
+ func (df * DataFrame ) Names (opts ... Options ) []string {
369
+ if len (opts ) == 0 || (len (opts ) > 0 && ! opts [0 ].DontLock ) {
370
+ df .lock .RLock ()
371
+ defer df .lock .RUnlock ()
372
+ }
391
373
374
+ names := []string {}
392
375
for _ , aSeries := range df .Series {
393
376
names = append (names , aSeries .Name ())
394
377
}
@@ -398,7 +381,12 @@ func (df *DataFrame) Names() []string {
398
381
399
382
// NameToColumn returns the index of the series based on the name.
400
383
// The starting index is 0.
401
- func (df * DataFrame ) NameToColumn (seriesName string ) (int , error ) {
384
+ func (df * DataFrame ) NameToColumn (seriesName string , opts ... Options ) (int , error ) {
385
+ if len (opts ) == 0 || (len (opts ) > 0 && ! opts [0 ].DontLock ) {
386
+ df .lock .RLock ()
387
+ defer df .lock .RUnlock ()
388
+ }
389
+
402
390
for idx , aSeries := range df .Series {
403
391
if aSeries .Name () == seriesName {
404
392
return idx , nil
@@ -411,9 +399,11 @@ func (df *DataFrame) NameToColumn(seriesName string) (int, error) {
411
399
// ReorderColumns reorders the columns based on an ordered list of
412
400
// column names. The length of newOrder must match the number of columns
413
401
// in the Dataframe. The column names in newOrder must be unique.
414
- func (df * DataFrame ) ReorderColumns (newOrder []string ) error {
415
- df .lock .Lock ()
416
- defer df .lock .Unlock ()
402
+ func (df * DataFrame ) ReorderColumns (newOrder []string , opts ... Options ) error {
403
+ if len (opts ) == 0 || (len (opts ) > 0 && ! opts [0 ].DontLock ) {
404
+ df .lock .Lock ()
405
+ defer df .lock .Unlock ()
406
+ }
417
407
418
408
if len (newOrder ) != len (df .Series ) {
419
409
return errors .New ("length of newOrder must match number of columns" )
@@ -432,7 +422,7 @@ func (df *DataFrame) ReorderColumns(newOrder []string) error {
432
422
series := []Series {}
433
423
434
424
for _ , v := range newOrder {
435
- idx , err := df .NameToColumn (v )
425
+ idx , err := df .NameToColumn (v , dontLock )
436
426
if err != nil {
437
427
return errors .New (err .Error () + ": " + v )
438
428
}
@@ -446,11 +436,13 @@ func (df *DataFrame) ReorderColumns(newOrder []string) error {
446
436
}
447
437
448
438
// RemoveSeries will remove a Series from the Dataframe.
449
- func (df * DataFrame ) RemoveSeries (seriesName string ) error {
450
- df .lock .Lock ()
451
- defer df .lock .Unlock ()
439
+ func (df * DataFrame ) RemoveSeries (seriesName string , opts ... Options ) error {
440
+ if len (opts ) == 0 || (len (opts ) > 0 && ! opts [0 ].DontLock ) {
441
+ df .lock .Lock ()
442
+ defer df .lock .Unlock ()
443
+ }
452
444
453
- idx , err := df .NameToColumn (seriesName )
445
+ idx , err := df .NameToColumn (seriesName , dontLock )
454
446
if err != nil {
455
447
return errors .New (err .Error () + ": " + seriesName )
456
448
}
@@ -460,8 +452,8 @@ func (df *DataFrame) RemoveSeries(seriesName string) error {
460
452
}
461
453
462
454
// Swap is used to swap 2 values based on their row position.
463
- func (df * DataFrame ) Swap (row1 , row2 int , options ... Options ) {
464
- if len (options ) == 0 || (len (options ) > 0 && ! options [0 ].DontLock ) {
455
+ func (df * DataFrame ) Swap (row1 , row2 int , opts ... Options ) {
456
+ if len (opts ) == 0 || (len (opts ) > 0 && ! opts [0 ].DontLock ) {
465
457
df .lock .Lock ()
466
458
defer df .lock .Unlock ()
467
459
}
0 commit comments