KxSystems / pyq

PyQ — Python for kdb+
http://code.kx.com/q/interfaces
Apache License 2.0
190 stars 49 forks source link

datetime type #83

Closed antipisa closed 6 years ago

antipisa commented 6 years ago

PyQ versions

 pyq.versions()
PyQ 4.1.3
NumPy 1.14.3
KDB+ 3.5 (2018.04.25) l64
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:17:00)
[GCC 7.2.0]

I noticed a type compatibility bugs between q and python in PyQ. Numpy cannot read the datetime "z" type. Please see below:

Converting k-arrays of datetime to numpy:

>>> import numpy as np
>>> np.asarray(q('(10?.z.z)'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/andy/.conda/envs/dev1/lib/python3.6/site-packages/numpy/core/numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
  File "/home/andy/.conda/envs/dev1/lib/python3.6/site-packages/pyq/_n.py", line 147, in array
    k2a(a, self)
  File "/home/andy/.conda/envs/dev1/lib/python3.6/site-packages/pyq/_n.py", line 124, in k2a
    unit = get_unit(a)
  File "/home/andy/.conda/envs/dev1/lib/python3.6/site-packages/pyq/_n.py", line 54, in get_unit
    raise TypeError("Expected a datetime64 array, not %s", a.dtype)
TypeError: ('Expected a datetime64 array, not %s', dtype('float64’))
abalkin commented 6 years ago

The "z" type is deprecated in kdb+. We don't currently have plans to support it. If you have legacy data that you need to process, you can access it as doubles:

>>> numpy.asarray(q('(10?.z.z)').data)
array([3349.17476025, 3928.69103492,  569.68153153, 1330.95938586,
       2550.9314647 , 4167.90083294, 3595.66715068, 4696.67444738,
       1559.61554216, 4699.00526901])