github.com/tobgu/qframe@v0.4.0/examples_test.go (about) 1 package qframe_test 2 3 import ( 4 "fmt" 5 "math" 6 "strings" 7 8 "github.com/tobgu/qframe" 9 "github.com/tobgu/qframe/config/csv" 10 "github.com/tobgu/qframe/config/groupby" 11 "github.com/tobgu/qframe/config/newqf" 12 "github.com/tobgu/qframe/function" 13 "github.com/tobgu/qframe/types" 14 ) 15 16 func ExampleQFrame_filterBuiltin() { 17 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 3}, "COL2": []string{"a", "b", "c"}}) 18 newF := f.Filter(qframe.Filter{Column: "COL1", Comparator: ">", Arg: 1}) 19 fmt.Println(newF) 20 21 // Output: 22 // COL1(i) COL2(s) 23 // ------- ------- 24 // 2 b 25 // 3 c 26 // 27 // Dims = 2 x 2 28 } 29 30 func ExampleQFrame_filterCustomFunc() { 31 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 3}, "COL2": []string{"a", "b", "c"}}) 32 isOdd := func(x int) bool { return x&1 > 0 } 33 newF := f.Filter(qframe.Filter{Column: "COL1", Comparator: isOdd}) 34 fmt.Println(newF) 35 36 // Output: 37 // COL1(i) COL2(s) 38 // ------- ------- 39 // 1 a 40 // 3 c 41 // 42 // Dims = 2 x 2 43 } 44 45 func ExampleQFrame_filterWithOrClause() { 46 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 3}, "COL2": []string{"a", "b", "c"}}) 47 newF := f.Filter(qframe.Or( 48 qframe.Filter{Column: "COL1", Comparator: ">", Arg: 2}, 49 qframe.Filter{Column: "COL2", Comparator: "=", Arg: "a"})) 50 fmt.Println(newF) 51 52 // Output: 53 // COL1(i) COL2(s) 54 // ------- ------- 55 // 1 a 56 // 3 c 57 // 58 // Dims = 2 x 2 59 } 60 61 func ExampleQFrame_sortWithEnum() { 62 f := qframe.New( 63 map[string]interface{}{"COL1": []string{"abc", "def", "ghi"}, "COL2": []string{"a", "b", "c"}}, 64 newqf.Enums(map[string][]string{"COL2": {"c", "b", "a"}})) 65 fmt.Println(f) 66 fmt.Println("\nSorted according to enum spec:") 67 fmt.Println(f.Sort(qframe.Order{Column: "COL2"})) 68 // Output: 69 // COL1(s) COL2(e) 70 // ------- ------- 71 // abc a 72 // def b 73 // ghi c 74 // 75 // Dims = 2 x 3 76 // 77 // Sorted according to enum spec: 78 // COL1(s) COL2(e) 79 // ------- ------- 80 // ghi c 81 // def b 82 // abc a 83 // 84 // Dims = 2 x 3 85 } 86 87 func ExampleReadCSV() { 88 input := `COL1,COL2 89 a,1.5 90 b,2.25 91 c,3.0` 92 93 f := qframe.ReadCSV(strings.NewReader(input)) 94 fmt.Println(f) 95 // Output: 96 // COL1(s) COL2(f) 97 // ------- ------- 98 // a 1.5 99 // b 2.25 100 // c 3 101 // 102 // Dims = 2 x 3 103 } 104 105 func ExampleReadCSV_configRenameDuplicateColumns() { 106 input := `COL,COL 107 a,1.5 108 b,2.25` 109 110 f := qframe.ReadCSV(strings.NewReader(input), csv.RenameDuplicateColumns(true)) 111 fmt.Println(f) 112 // Output: 113 // COL(s) COL0(f) 114 // ------ ------- 115 // a 1.5 116 // b 2.25 117 // 118 // Dims = 2 x 2 119 } 120 121 func ExampleReadCSV_configMissingColumnNameAlias() { 122 input := `,COL1 123 a,1.5 124 b,2.25` 125 126 f := qframe.ReadCSV(strings.NewReader(input), csv.MissingColumnNameAlias("COL")) 127 fmt.Println(f) 128 // Output: 129 // COL(s) COL1(f) 130 // ------ ------- 131 // a 1.5 132 // b 2.25 133 // 134 // Dims = 2 x 2 135 } 136 137 func ExampleNew() { 138 a, c := "a", "c" 139 f := qframe.New(map[string]interface{}{ 140 "COL1": []int{1, 2, 3}, 141 "COL2": []float64{1.5, 2.5, math.NaN()}, 142 "COL3": []string{"a", "b", "c"}, 143 "COL4": []*string{&a, nil, &c}, 144 "COL5": []bool{false, false, true}}, 145 newqf.ColumnOrder("COL5", "COL4", "COL3", "COL2", "COL1")) 146 fmt.Println(f) 147 // Output: 148 // COL5(b) COL4(s) COL3(s) COL2(f) COL1(i) 149 // ------- ------- ------- ------- ------- 150 // false a a 1.5 1 151 // false null b 2.5 2 152 // true c c null 3 153 // 154 // Dims = 5 x 3 155 } 156 157 func ExampleQFrame_applyStrConcat() { 158 // String concatenating COL2 and COL1. 159 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 3}, "COL2": []string{"a", "b", "c"}}) 160 f = f.Apply( 161 qframe.Instruction{Fn: function.StrI, DstCol: "COL1", SrcCol1: "COL1"}, 162 qframe.Instruction{Fn: function.ConcatS, DstCol: "COL3", SrcCol1: "COL1", SrcCol2: "COL2"}) 163 fmt.Println(f.Select("COL3")) 164 165 // Output: 166 // COL3(s) 167 // ------- 168 // 1a 169 // 2b 170 // 3c 171 // 172 // Dims = 1 x 3 173 } 174 175 func ExampleQFrame_applyConstant() { 176 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 3}}) 177 f = f.Apply(qframe.Instruction{Fn: 1.5, DstCol: "COL2"}) 178 fmt.Println(f) 179 180 // COL1(i) COL2(f) 181 // ------- ------- 182 // 1 1.5 183 // 2 1.5 184 // 3 1.5 185 // 186 // Dims = 2 x 3 187 } 188 189 func ExampleQFrame_applyGenerator() { 190 val := -1 191 generator := func() int { 192 val++ 193 return val 194 } 195 196 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 3}}) 197 f = f.Apply(qframe.Instruction{Fn: generator, DstCol: "COL2"}) 198 fmt.Println(f) 199 200 // COL1(i) COL2(i) 201 // ------- ------- 202 // 1 0 203 // 2 1 204 // 3 2 205 // 206 // Dims = 2 x 3 207 } 208 209 func ExampleQFrame_evalStrConcat() { 210 // Same example as for apply but using Eval instead. 211 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 3}, "COL2": []string{"a", "b", "c"}}) 212 f = f.Eval("COL3", qframe.Expr("+", qframe.Expr("str", types.ColumnName("COL1")), types.ColumnName("COL2"))) 213 fmt.Println(f.Select("COL3")) 214 215 // Output: 216 // COL3(s) 217 // ------- 218 // 1a 219 // 2b 220 // 3c 221 // 222 // Dims = 1 x 3 223 } 224 225 func ExampleQFrame_groupByAggregate() { 226 intSum := func(xx []int) int { 227 result := 0 228 for _, x := range xx { 229 result += x 230 } 231 return result 232 } 233 234 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 2, 3, 3}, "COL2": []string{"a", "b", "c", "a", "b"}}) 235 f = f.GroupBy(groupby.Columns("COL2")).Aggregate(qframe.Aggregation{Fn: intSum, Column: "COL1"}) 236 fmt.Println(f.Sort(qframe.Order{Column: "COL2"})) 237 238 // Output: 239 // COL2(s) COL1(i) 240 // ------- ------- 241 // a 4 242 // b 5 243 // c 2 244 // 245 // Dims = 2 x 3 246 } 247 248 func ExampleQFrame_view() { 249 f := qframe.New(map[string]interface{}{"COL1": []int{1, 2, 3}}) 250 v, _ := f.IntView("COL1") 251 fmt.Println(v.Slice()) 252 253 // Output: 254 // [1 2 3] 255 } 256 257 func ExampleQFrame_iter() { 258 qf := qframe.New(map[string]interface{}{ 259 "COL1": []string{"a", "b", "c"}, 260 "COL2": []int{0, 1, 2}, 261 "COL3": []string{"d", "e", "f"}, 262 "COL4": []int{3, 4, 5}, 263 }) 264 named := qf.ColumnTypeMap() 265 for _, col := range qf.ColumnNames() { 266 if named[col] == types.Int { 267 view := qf.MustIntView(col) 268 for i := 0; i < view.Len(); i++ { 269 fmt.Println(view.ItemAt(i)) 270 } 271 } 272 } 273 274 // Output: 275 // 0 276 // 1 277 // 2 278 // 3 279 // 4 280 // 5 281 } 282 283 func ExampleQFrame_groupByCount() { 284 qf := qframe.New(map[string]interface{}{ 285 "COL1": []string{"a", "b", "a", "b", "b", "c"}, 286 "COL2": []float64{0.1, 0.1, 0.2, 0.4, 0.5, 0.6}, 287 }) 288 289 g := qf.GroupBy(groupby.Columns("COL1")) 290 qf = g.Aggregate(qframe.Aggregation{Fn: "count", Column: "COL2"}).Sort(qframe.Order{Column: "COL1"}) 291 292 fmt.Println(qf) 293 294 // Output: 295 // COL1(s) COL2(i) 296 // ------- ------- 297 // a 2 298 // b 3 299 // c 1 300 // 301 // Dims = 2 x 3 302 } 303 304 func ExampleQFrame_distinct() { 305 qf := qframe.New(map[string]interface{}{ 306 "COL1": []string{"a", "b", "a", "b", "b", "c"}, 307 "COL2": []int{0, 1, 2, 4, 4, 6}, 308 }) 309 310 qf = qf.Distinct(groupby.Columns("COL1", "COL2")).Sort(qframe.Order{Column: "COL1"}, qframe.Order{Column: "COL2"}) 311 312 fmt.Println(qf) 313 314 // Output: 315 // COL1(s) COL2(i) 316 // ------- ------- 317 // a 0 318 // a 2 319 // b 1 320 // b 4 321 // c 6 322 // 323 // Dims = 2 x 5 324 }