cancel
Showing results for 
Search instead for 
Did you mean: 

Re: [personal kdb+] KDB embedQ use array from Q in Python

cmccarthy1
New Contributor II
New Contributor II
Hi Simon,

Assigning q objects to be accessible from functions defined in the Python side of the API requires you to set the objects. In the case you're describing where you want to run 

p)print(tbl1)

You would need to run this in the following steps:

// Define the q table
mytab:([]5?1f;5?1f)
// set the q table converted to a python dictionary to python variable tbl1
.p.set[`tbl1;mytab]
// Print from the python side of the interface
p)print(tbl1)

With regards to calling the class with the q objects you would either need to do the sets from q side to python as above .i.e

.p.set[`tbl1;tbl1]
.p.set[`tbl2;tbl2]
p)teCalc.addObservations(tbl1, tbl2)

or alternatively 'get' the python callable object and assign it to a q callable function and run directly on the q data without a set

// Load the script containing the required class
\l myclass.p
// Retrieve the class
teCalc:.p.get[`teCalc]
// Access the relevant function
addObservations:teCalc[`:addObservations]
// Run on the relevant q data and return result to q (denoted by trailing `)
addObservations[tbl1;tbl2]`

It should be noted that when embedPy converts q tables to Python it does so as a list of dictionaries (this is the accurate representation of what a q table is but may be counter intuitive) as such if you need the data to be a pandas dataframe for you use case you may need to do the conversion manually. This is done using 2 functions .ml.tab2df/.ml.df2tab which are both available within the Kx Machine learning toolkit here.

More information on embedPy is available at the below links which outline in detail all of the above


All the best,

Conor



1 REPLY 1

simon_watson_sj
New Contributor III
Hi Conor - this is a really good explanation - thank you for taking the time. 

This is really great functionality. I'll have a go both ways and see which is faster. My money is on importing the class being the faster one - I will post results when I have them. 

I hope it's ok - I also posted your response to Stack Overflow.

Thanks again. 

Simon