KxSystems / pykx

PyKX is a Python first interface to the worlds fastest time-series database kdb+ and it's underlying vector programming language q.
https://code.kx.com/pykx
Other
45 stars 10 forks source link

Example of qsql.select to filter with Python Datetime #2

Closed Cambyst closed 11 months ago

Cambyst commented 11 months ago

Is your feature request related to a problem? Please describe. So far all the examples for qsql.select which use the "where" only filter on simple data types like string or integer. I cannot make it work for datetime and unfortunately no examples are provided. Many thanks in advance !

Describe the solution you'd like A few examples which use qsql.select to filter data on datetime columns using "where" parameter.

Describe alternatives you've considered I have tried a lot of possible way to do this using qsql.select, but all of them throw an exception.

rianoc-kx commented 11 months ago

Note https://code.kx.com/q/basics/datatypes/#temporal The 4-byte datetime datatype (15) is deprecated in favour of the 8-byte timestamp datatype (12).

Create q table with datetime column:

>>> kx.q('t:([] a:.z.z+til 10)')
pykx.Identity(pykx.q('::'))
>>> kx.q('t')
pykx.Table(pykx.q('
a
-----------------------
2023.07.31T10:30:48.609
2023.08.01T10:30:48.609
2023.08.02T10:30:48.609
2023.08.03T10:30:48.609
2023.08.04T10:30:48.609
2023.08.05T10:30:48.609
2023.08.06T10:30:48.609
2023.08.07T10:30:48.609
2023.08.08T10:30:48.609
2023.08.09T10:30:48.609
'))

Filter table:

>>> pykx.q.qsql.select('t', where='a within (2023.07.31T00:00;2023.08.03T00:00)')
pykx.Table(pykx.q('
a
-----------------------
2023.07.31T10:30:48.609
2023.08.01T10:30:48.609
2023.08.02T10:30:48.609
'))
Cambyst commented 11 months ago

Hi @rianoc-kx , thank you very much for this, very helpful !