【问题标题】:Return KDB query to a pandas datframe将 KDB 查询返回到 pandas 数据框
【发布时间】:2019-08-22 23:41:24
【问题描述】:

我想从 KDB 数据库中提取数据并放入 datframe。我的查询在 qpad 中运行良好,没有问题;只需要将它写入我的 Pandas 数据框中。我的代码:

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()

df.info() 引发 AttributeError: 'QLambda' object has no attribute 'info' 失败,所以我猜调用不成功。

【问题讨论】:

    标签: pandas kdb qpython qpython3


    【解决方案1】:

    您似乎只发送了一个 lambda,但没有执行该 lambda 的指令。两种选择:

    1. 不要让它成为 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. 执行 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。我选择了返回记录的第二个选项。当我评估变量时,我可以看到 df 中的值,但是在 df.info() 上得到“raise AttributeError(“recarray has no attribute %s”% attr):“AttributeError: recarray has no attribute info”。它看起来像如果数据库中的许多字段已返回到单个字段,那么 df 是 1xm matirx 有没有办法解析值以便从 q.sendSync 返回 anxm 网格?
    • 我对 qpython 的经验不足,不知道会出现什么错误,但我确实记得只是使用 q 进行同步调用而不是 q.sendSync - 也许尝试一下?根据我的经验,我会发出类似df = q('select from table') 的调用,然后我会使用索引逐行遍历结果
    • 谢谢特里会试试的
    猜你喜欢
    • 2023-04-06
    • 2018-04-03
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2021-08-23
    • 2021-10-20
    • 2020-09-03
    • 2013-02-10
    相关资源
    最近更新 更多