0

I've created a pivot table based on: https://code.kx.com/q/kb/pivoting-tables/

I've just replaced the symbols with minutes:

t:([]k:1 2 3 2 3;p:09:00 09:30 10:00 09:00 09:30; v:10 20 30 40 50)
P:asc exec distinct p from t;
exec P#(p!v) by k:k from t

Suffice to say, this doesn't work:

k|
-| -----------------------------
1| `s#09:00 09:30 10:00!10 0N 0N
2| `s#09:00 09:30 10:00!40 20 0N
3| `s#09:00 09:30 10:00!0N 50 30

which I expected, as the docs says P must be a list of symbols.

My question is; can temporal datatypes be used as columns at all in KDB?

1 Answer 1

6

Column names must be symbols. You can use .Q.id to give columns valid names, for example:

q)t:([]k:1 2 3 2 3;p:09:00 09:30 10:00 09:00 09:30; v:10 20 30 40 50)
q)P:.Q.id each asc exec distinct p from t;
q)exec P#.Q.id'[p]!v by k:k from t
k| a0900 a0930 a1000
-| -----------------
1| 10
2| 40    20
3|       50    30

You could convert minutes to their symbolic representation like this of course:

q)P:`$string asc exec distinct p from t;
q)exec P#(`$string p)!v by k:k from t
k| 09:00 09:30 10:00
-| -----------------
1| 10
2| 40    20
3|       50    30

but the result would be confusing at best, I strongly advise against such column names.

1
  • Thanks! It's a shame; surprisingly this actually works in pandas.DataFrame.pivot()
    – cjm2671
    Commented Oct 31, 2022 at 12:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.