You need to enable JavaScript to run this app. 如何通过qPython向kdb传递包含分钟列表的字典参数
优惠活动
大模型
产品
解决方案
定价
更多

如何通过qPython向kdb传递包含分钟列表的字典参数

问题根因

你构造的QDictionary使用Python字符串作为键,传输到kdb后生成的是字符串键字典。kdb侧用符号类型的`one、`two索引时无法匹配字符串键,会默认返回date类型空值,date空值的类型编号就是-14,因此抛出的报错和two参数本身的类型无关,本质是字典键类型不匹配导致取值失败。

解决方案

构造QDictionary时,显式将键列表封装为QSYMBOL_LIST类型的qlist,确保kdb侧收到的是符号键字典,可正常通过符号索引取值。

修改后的代码示例

仅需调整QDictionary构造逻辑即可:

from qpython import qconnection
from qpython.qcollection import QDictionary, qlist
import numpy as np
from qpython.qtype import QLONG_LIST, QSYMBOL_LIST, QMINUTE_LIST, QDATE


query='{[arg_dict] :.test.testfun[arg_dict] }'

kdbconfig = {
        'q_host': 'q_host',
        'q_port': 42,
        'q_usr': 'q_usr',
        'q_pwd': 'q_pwd' 
        }

params = {
    "one" : np.datetime64('2021-01-06', 'D'),
    "two" : qlist([ np.timedelta64(10*60, 'm'), np.timedelta64(10*60+30, 'm')] , qtype = QMINUTE_LIST)
}

# 核心修改:将键转为符号类型列表
keys = qlist(list(params.keys()), qtype=QSYMBOL_LIST)
qparams = QDictionary(keys, list(params.values()))


with qconnection.QConnection(host=kdbconfig['q_host'],
                                     port=kdbconfig['q_port'],
                                     username=kdbconfig['q_usr'],
                                     password=kdbconfig['q_pwd'] ) as q:
        data = q.sendSync(query, qparams, pandas = True)
        if data is None:
            raise ValueError("didnt return any data")

修改后kdb侧type args[two]返回值为预期的17,函数可正常执行返回42。如果仍存在date类型匹配问题,可以显式将one参数封装为QDATE`类型的原子值,避免numpy自动转换偏差。


内容的提问来源于stack exchange,提问作者Will

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 17:27:03