Search code examples
pythonnumpykdbqpython

What type has kdb's xbar time


I'm working on a function (in python, using qPython) that gets history data from a hdb table inside of kdb. I'd like to give the end user as much freedom in this function when it comes to parameters. The query the function builds looks something like this:

select by 10 xbar time.minute from trade where date within('2017-06-31';'2017-07-01'),sym = 'instr1'

Here it is again but this time with the variable placeholders:

'{[a:b;x;y;z]select by a xbar b from trade where date within(x;y),sym in z}',

My question is about the time.something that goes in place of b. Whenever I pass a string, numpy.string_, byte or numpy.bytes_ it throws a 'type exception.

What type is this time.* argument in kdb, and what numpy/qPython type should I use to pass it?

Thanks in advance!


Solution

  • You can use . notation in functions for selects like this e.g.

    q){select count i by time.minute from x}([]time:2#.z.p)
    minute| x
    ------| -
    19:43 | 2
    

    Your problem here with the type error is the function declaration:

    {[a:b;x;y;z]select by a xbar b from trade where date within(x;y),sym in z}
    

    The declaration should be

    {[a;b;x;y;z]select by a xbar b from trade where date within(x;y),sym in z}
    

    i.e. you have a : instead of ;

    The problem with the casts that you are trying to do is that the data will come back with different types, depending on how you cast it. You also can't pass in "b" to the function in the way you are trying to. Personally I would just stick with timestamps (assuming time is a timestamp type) and use the resolution of the parameter to dictate it e.g.

    q){[b;t] select count i by b xbar time from t}[0D01;([]time:0D 0D00:01 0D01+.z.p)]                                                                                                
    
    time                         | x
    -----------------------------| -
    2017.08.01D19:00:00.000000000| 2
    2017.08.01D20:00:00.000000000| 1
    q){[b;t] select count i by b xbar time from t}[0D00:01;([]time:0D 0D00:01 0D01+.z.p)]                                                                                             
    time                         | x
    -----------------------------| -
    2017.08.01D19:48:00.000000000| 1
    2017.08.01D19:49:00.000000000| 1
    2017.08.01D20:48:00.000000000| 1
    q){[b;t] select count i by b xbar time from t}[0D00:00:01;([]time:0D 0D00:01 0D01+.z.p)]                                                                                          
    time                         | x
    -----------------------------| -
    2017.08.01D19:48:30.000000000| 1
    2017.08.01D19:49:30.000000000| 1
    2017.08.01D20:48:30.000000000| 1
    

    As for the python type, not sure but I think it's numpy.datetime64