1

I would like to extract data from a KDB database and place into a dataframe. My query runs fine in qpad, no issues; just need to write it into my Pandas dataframe. My code:

from qpython import qconnection

# Create the connection and save the handle to a variable
q  = qconnection.QConnection(host = 'wokplpaxvj003', port = 11503, username = 'pelucas', password = 'Dive2600', timeout = 3.0)
try:
    # initialize connection
    q.open()
    print(q)
    print('IPC version: %s. Is connected: %s' % (q.protocol_version, q.is_connected()))

    df = q.sendSync('{select from quote_flat where date within (2019.08.14;2019.08.14), amendment_no = (max;amendment_no)fby quote_id}')

    df.info()

finally:
    q.close()

It fails on the df.info() raising AttributeError: 'QLambda' object has no attribute 'info' so I guess the call is not successful.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
Peter Lucas
  • 1,979
  • 1
  • 16
  • 27

1 Answers1

3

It looks like you've sent only a lambda but with no instruction to execute that lambda. Two options:

  1. Don't make it a lambda
df = q.sendSync('select from quote_flat where date within (2019.08.14;2019.08.14), amendment_no = (max;amendment_no)fby quote_id')
  1. Execute the lambda
df = q.sendSync('{select from quote_flat where date within (2019.08.14;2019.08.14), amendment_no = (max;amendment_no)fby quote_id}[]')
terrylynch
  • 11,844
  • 13
  • 21
  • Thanks @terrylynch. I went with the second option which returned records. I can see values in the df when I evaluate the variable but am getting "raise AttributeError("recarray has no attribute %s" % attr): "AttributeError: recarray has no attribute info" on df.info(). It looks as if the many fields from the database have been returned to a single field so df is a 1xm matirx Is there a way to parse the values so a n x m grid is returned from q.sendSync? – Peter Lucas Aug 25 '19 at 23:24
  • I'm not experienced enough with qpython to know what that error would be but I do recall simply using `q` for synchronous calls rather than `q.sendSync` - perhaps try that? From my experience I would issue a call like `df = q('select from table')` and then I would loop through the result row by row using indexing – terrylynch Aug 26 '19 at 09:20
  • Thanks Terry will try that – Peter Lucas Aug 26 '19 at 09:30