Example of qsql.select to filter with Python Datetime
Cambyst opened this issue · 2 comments
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.
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
'))
Hi @rianoc-kx , thank you very much for this, very helpful !