-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
77 lines (68 loc) · 1.83 KB
/
example.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
package main
import (
"fmt"
"github.com/DataDog/go-python3"
"github.com/apache/arrow/go/arrow/array"
"github.com/apache/arrow/go/arrow/memory"
"github.com/go-bullseye/bullseye/dataframe"
bridge "github.com/nickpoorman/go-py-arrow-bridge"
"github.com/nickpoorman/pytasks"
)
func main() {
py := pytasks.GetPythonSingleton()
fooModule, err := py.ImportModule("foo")
if err != nil {
panic(err)
}
defer func() {
err := pytasks.GetPythonSingleton().NewTaskSync(func() {
fooModule.DecRef()
})
if err != nil {
panic(err)
}
}()
var table array.Table
taskErr := py.NewTaskSync(func() {
pyTable := genPyTable(fooModule)
table, err = bridge.PyTableToTable(pyTable)
pyTable.DecRef()
})
if taskErr != nil {
panic(taskErr)
}
if err != nil {
panic(err)
}
// Wrapping it in a bullseye dataframe allows us to print it easily
pool := memory.NewGoAllocator()
df, err := dataframe.NewDataFrameFromTable(pool, table)
if err != nil {
panic(err)
}
fmt.Println("\nArrow Table from Python now in Go:")
fmt.Println(df.Display(0))
// Arrow Table from Python now in Go:
// rec[0]["f0"]: [1 2 3 4]
// rec[0]["f1"]: ["foo" "bar" "baz" (null)]
// rec[0]["f2"]: [true (null) false true]
// rec[1]["f0"]: [1 2 3 4]
// rec[1]["f1"]: ["foo" "bar" "baz" (null)]
// rec[1]["f2"]: [true (null) false true]
// rec[2]["f0"]: [1 2 3 4]
// rec[2]["f1"]: ["foo" "bar" "baz" (null)]
// rec[2]["f2"]: [true (null) false true]
// rec[3]["f0"]: [1 2 3 4]
// rec[3]["f1"]: ["foo" "bar" "baz" (null)]
// rec[3]["f2"]: [true (null) false true]
// rec[4]["f0"]: [1 2 3 4]
// rec[4]["f1"]: ["foo" "bar" "baz" (null)]
// rec[4]["f2"]: [true (null) false true]
}
func genPyTable(module *python3.PyObject) *python3.PyObject {
pyTable := bridge.CallPyFunc(module, "zero_copy_chunks")
if pyTable == nil {
panic("pyTable is nil")
}
return pyTable
}