q-lang

KDB+ / Q Syntax optimizations to oneliners

依然范特西╮ 提交于 2020-01-17 01:17:12
问题 I am definitly a q-mortal. I would even say a q-baby. Well I have some question how to put together my code from separate lines to a one-liner. I guess there is a way more elegant solution than mine. How to write the following statements in one line: q)t1:(3#3)?\:`8 q)t1[;0]:`abc In this table creation, how can I add another column which I have as list (like with the command ([]id:id_list;data:data_list;..... )). Till now I am creating another table and doing an inner join on them. I guess

[kdb+/q]: Convert adjacency matrix to adjacency list

喜欢而已 提交于 2020-01-16 08:23:26
问题 Given (rectangular) adjacency matrix m , how to construct adjacency list in q language? In QIdioms wiki I've found solution in the k language which when run through q console with k) command gives me 'vs error: m:(1 0 1;1 0 1) k) (^m)_vs &,/m 'vs Result should be: 0 0 1 1 0 2 0 2 This is what I was able to replicate in q : k) &,/m 0 2 3 5 q) where raze m 0 2 3 5 k 's ^ a.k.a. shape verb is missing in q so I just did: k) (^m) 000b 000b q) 2 3#0b 000b 000b Now, since: q) parse "vs" k) {x\:y} I

[kdb+/q]: Convert adjacency matrix to adjacency list

吃可爱长大的小学妹 提交于 2020-01-16 08:23:11
问题 Given (rectangular) adjacency matrix m , how to construct adjacency list in q language? In QIdioms wiki I've found solution in the k language which when run through q console with k) command gives me 'vs error: m:(1 0 1;1 0 1) k) (^m)_vs &,/m 'vs Result should be: 0 0 1 1 0 2 0 2 This is what I was able to replicate in q : k) &,/m 0 2 3 5 q) where raze m 0 2 3 5 k 's ^ a.k.a. shape verb is missing in q so I just did: k) (^m) 000b 000b q) 2 3#0b 000b 000b Now, since: q) parse "vs" k) {x\:y} I

pandas DataFrame drops index when passing to kdb+ (using qPython API)

别说谁变了你拦得住时间么 提交于 2020-01-15 12:34:07
问题 I am trying to pass time-series data from Python to q/kdb+ . One solution out there is qPython module, offering seamless conversion from q table/dictionary to Pandas. The problem is when trying to pass from Pandas to q , the time index in DataFrame (in the column Date ) doesn't quite make it to the q side. Reproducible code: import pandas.io.data as web import datetime import numpy import qpython.qconnection as qconnection # requires installation of qPython module from https://github.com

[KDB+/Q]: Deparse q parse tree into q expression (string)

若如初见. 提交于 2020-01-15 11:14:03
问题 Let's define deparse 1 as inverse operation to q's native parse , so that the following holds: q)aStringQExpression~deparse parse aStringQExpression 1b Question What's the definition of deparse function so that the above indeed works? For example, in the below update expression, we know that "a*0^b+c-d" expression corresponds to (*;`a;(^;0;(+;`b;(-;`c;`d)))) parse tree: q)-3!parse "update col:a*0^b+c-d from t" "(!;`t;();0b;(,`col)!,(*;`a;(^;0;(+;`b;(-;`c;`d)))))" So the envisaged deparse

q - apply function on table rowwise

我是研究僧i 提交于 2020-01-14 05:30:28
问题 Given a table and a function t:([] c1:1 2 3; c2:`a`b`c; c3:13:00 13:01 13:02) f:{[int;sym;date] symf:{$[x=`a;1;x=`b;2;3]}; datef:{$[x=13:00;1;x=13:01;2;3]}; r:int + symf[sym] + datef[date]; r }; I noticed that when applying the function f onto columns of t , then the entire columns are passed into f and if they can be operated on atomically then the output will be of the same length as the inputs and a new column is produced. However in our example this wont work: update newcol:f[c1;c2;c3]

extract number from string in kdb

前提是你 提交于 2019-12-11 08:41:36
问题 I am quite new to kdb+q. I've come across this problem of extracting a number out of string. Any suggestions? Example: "AZXER_1234_MARKET" should output 1234 //Assume that there is only one number in the string 回答1: Extract the numbers then cast to required type. q){"I"$x inter .Q.n} "AZXER_1234_MARKET" 1234i q){"I"$x inter .Q.n} "AZXER_123411_MARKET" 123411i q){"I"$x inter .Q.n} "AZXER_1234_56_MARKET" 123456i q){"I"$x inter .Q.n} "AR_34_56_MAT" 3456i 回答2: If you have multiple numbers, here

Apply var to the last n numbers of a column in a table

╄→尐↘猪︶ㄣ 提交于 2019-12-11 07:29:04
问题 In KDB I have a table with 4 columns, of which one is a returns column. I would like to create a 5th column that calculates the variance (var) of the past x elements of a specified column. I have managed this with two columns creating the returns of a price column, but am stuck when I need to reference more than one previous element. Example: t:([] td:2001.01.01 2001.01.02 2001.01.03 2001.01.04 2001.01.05 2001.01.06; px:121 125 127 126 129 130) t:update retLogPcnt:100*log px%prev px from t /t

How to match date and string from 2 lists (KDB)?

拜拜、爱过 提交于 2019-12-11 04:19:58
问题 I have two lists: data: dt sym bid ask 2017.01.01D05:00:09.140745000 AAPL 101.20 101.30 2017.01.01D05:00:09.284281800 GOOG 801.00 802.00 2017.01.02D05:00:09.824847299 AAPL 101.30 101.40 info: date sym shares divisor 2017.01.01 AAPL 500 2 2017.01.01 GOOG 100 1 2017.01.02 AAPL 200 2 I need to append from "info" the shares and divisor values for each ticker based on the date. How can I achieve this? Below is an example: result: dt sym bid ask shares divisor 2017.01.01D05:00:09.140745000 AAPL 101