exxeleron / qPython

interprocess communication between Python and kdb+
http://www.devnet.de
Apache License 2.0
152 stars 89 forks source link

Date column type #26

Closed patmok closed 8 years ago

patmok commented 8 years ago

Came across the following using Python 2.7.10 |Anaconda 2.2.0 (64-bit) querying kdb 3.1 l64

r = query_kdb.query(REGION, '([]2#.z.d)') 20150728 15:31:37.612 INFO IPC version: 3. Is connected: True 20150728 15:31:37.612 INFO Querying KDB. port:[kdbtest], host:[1234], username:[user], timeout:[4.0]. 20150728 15:31:37.612 INFO Query: ([]2#.z.d) r QTable([(5687,), (5687,)], dtype=[('d', '<i4')]) r[0] (5687,) type(r[0]) <class 'numpy.core.records.record'> r0

result = query_kdb.query(REGION, '.z.d') 20150728 15:24:26.910 INFO IPC version: 3. Is connected: True 20150728 15:24:26.910 INFO Querying KDB. port:[kdbtest], host:[1234], username:[user], timeout:[4.0]. 20150728 15:24:26.910 INFO Query: .z.d result <qpython.qtemporal.QTemporal object at 0x7f67c929e3d0>

It seems the type remains an integer from date columns?

maciejlach commented 8 years ago

By default qPython represents temporal vectors as instances of qcollection.QTemporalList class. This class wraps the raw q representation of temporal data and provides accessors which allow to convert raw data to qcollection.QTemporal instances in a lazy fashion. (Read more...)

>>>    v = q.sync('2#.z.d')
>>>    print '%s dtype: %s qtype: %d: %s' % (type(v), v.dtype, v.meta.qtype, v)
<class 'qpython.qcollection.QTemporalList'> dtype: int32 qtype: -14: [2015-08-03 [metadata(qtype=-14)] 2015-08-03 [metadata(qtype=-14)]]

If a temporal vector is part of the kdb+ table, the lazy conversion to the numpy datetime objects is not available.

>>>    v = q.sync('([]2#.z.d)')
>>>    print '%s dtype: %s qtype: %d: %s' % (type(v), v.dtype, v.meta.qtype, v)
<class 'qpython.qcollection.QTable'> dtype: [('d', '<i4')] qtype: 98: [(5693,) (5693,)]

qPython can be instructed (Read more...) to represent temporal vectors as numpy arrays of numpy.datetime64 or numpy.timedelta64. This also works for tables with temporal columns:

>>>    v = q.sync('([]2#.z.d)', numpy_temporals = True)
>>>    print '%s dtype: %s qtype: %d: %s' % (type(v), v.dtype, v.meta.qtype, v)
<class 'qpython.qcollection.QTable'> dtype: [('d', '<M8[D]')] qtype: 98: [(datetime.date(2015, 8, 3),) (datetime.date(2015, 8, 3),)]