MCPcopy Create free account
hub / github.com/datasweet/datatable / Where

Method Where

where.go:4–37  ·  view source on GitHub ↗

Where filters the datatable based on a predicate

(predicate func(row Row) bool)

Source from the content-addressed store, hash-verified

2
3// Where filters the datatable based on a predicate
4func (t *DataTable) Where(predicate func(row Row) bool) *DataTable {
5 if predicate == nil {
6 return t.EmptyCopy()
7 }
8
9 if err := t.evaluateExpressions(); err != nil {
10 panic(err)
11 }
12
13 subset := make([]int, 0, t.nrows) // max
14
15 for i := 0; i < t.nrows; i++ {
16 r := make(Row, len(t.cols))
17 for _, col := range t.cols {
18 r[col.name] = col.serie.Get(i)
19 }
20 if predicate(r) {
21 subset = append(subset, i)
22 }
23 }
24
25 cpy := t.EmptyCopy()
26
27 if len(subset) == 0 {
28 return cpy
29 }
30
31 cpy.nrows = len(subset)
32 for i, col := range t.cols {
33 cpy.cols[i].serie = col.serie.Pick(subset...)
34 }
35
36 return cpy
37}

Callers

nothing calls this directly

Calls 4

EmptyCopyMethod · 0.95
evaluateExpressionsMethod · 0.95
GetMethod · 0.65
PickMethod · 0.65

Tested by

no test coverage detected